Skip to content

Commit

Permalink
revert(cli): compile directories (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMcCulloh authored Sep 29, 2023
1 parent 8cda22d commit 65124f1
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 378 deletions.
21 changes: 0 additions & 21 deletions apps/wing/src/commands/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Target } from "@winglang/compiler";
import { generateTmpDir } from "src/util";

const exampleDir = resolve("../../examples/tests/valid");
const exampleSmallDir = resolve("../../examples/tests/valid/subdir2");
const exampleFilePath = join(exampleDir, "captures.test.w");

describe(
Expand Down Expand Up @@ -46,26 +45,6 @@ describe(
);
});

test("should be able to compile a directory", async () => {
const artifactDir = await compile(exampleSmallDir, {
target: Target.SIM,
targetDir: `${await generateTmpDir()}/target`,
});

const stats = await stat(artifactDir);
expect(stats.isDirectory()).toBeTruthy();
});

test("should be able to compile a directory to tf-aws", async () => {
const artifactDir = await compile(exampleSmallDir, {
target: Target.TF_AWS,
targetDir: `${await generateTmpDir()}/target`,
});

const stats = await stat(artifactDir);
expect(stats.isDirectory()).toBeTruthy();
});

// https://github.com/winglang/wing/issues/2081
test("should be able to compile extern file from same directory", async () => {
// temporarily change cwd to the example directory
Expand Down
10 changes: 8 additions & 2 deletions libs/wingc/examples/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This should only be used for testing wingc directly.

use camino::{Utf8Path, Utf8PathBuf};
use std::{env, process};
use std::{env, fs, process};
use wingc::{compile, diagnostic::get_diagnostics};

pub fn main() {
Expand All @@ -13,9 +13,15 @@ pub fn main() {
}

let source_path = Utf8Path::new(&args[1]).canonicalize_utf8().unwrap();
let source_text = fs::read_to_string(&source_path).unwrap();
let target_dir: Utf8PathBuf = env::current_dir().unwrap().join("target").try_into().unwrap();

let results = compile(&source_path, None, &target_dir, source_path.parent().unwrap());
let results = compile(
&source_path,
source_text,
Some(&target_dir),
Some(source_path.parent().unwrap()),
);
if results.is_err() {
let mut diags = get_diagnostics();
// Sort error messages by line number (ascending)
Expand Down
20 changes: 9 additions & 11 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::{
diagnostic::{report_diagnostic, Diagnostic, WingSpan},
file_graph::FileGraph,
files::Files,
parser::is_entrypoint_file,
type_check::{
is_udt_struct_type,
lifts::{Liftable, Lifts},
Expand Down Expand Up @@ -73,8 +72,8 @@ pub struct JSifier<'a> {
source_file_graph: &'a FileGraph,
/// Root of the project, used for resolving extern modules
absolute_project_root: &'a Utf8Path,
/// The path that compilation started at (file or directory)
compilation_init_path: &'a Utf8Path,
/// The entrypoint file of the Wing application.
entrypoint_file_path: &'a Utf8Path,
}

/// Preflight classes have two types of host binding methods:
Expand All @@ -90,15 +89,15 @@ impl<'a> JSifier<'a> {
types: &'a mut Types,
source_files: &'a Files,
source_file_graph: &'a FileGraph,
compilation_init_path: &'a Utf8Path,
entrypoint_file_path: &'a Utf8Path,
absolute_project_root: &'a Utf8Path,
) -> Self {
let output_files = Files::default();
Self {
types,
source_files,
source_file_graph,
compilation_init_path,
entrypoint_file_path,
absolute_project_root,
referenced_struct_schemas: RefCell::new(BTreeMap::new()),
inflight_file_counter: RefCell::new(0),
Expand Down Expand Up @@ -142,11 +141,10 @@ impl<'a> JSifier<'a> {

let mut output = CodeMaker::default();

let is_compilation_init = source_path == self.compilation_init_path;
let is_entrypoint = is_entrypoint_file(source_path);
let is_entrypoint_file = source_path == self.entrypoint_file_path;
let is_directory = source_path.is_dir();

if is_entrypoint {
if is_entrypoint_file {
output.line(format!("const {} = require('{}');", STDLIB, STDLIB_MODULE));
output.line(format!(
"const {} = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLUGIN_PATHS);",
Expand All @@ -165,7 +163,7 @@ impl<'a> JSifier<'a> {
output.line(format!("const std = {STDLIB}.{WINGSDK_STD_MODULE};"));
output.add_code(imports);

if is_entrypoint {
if is_entrypoint_file {
let mut root_class = CodeMaker::default();
root_class.open(format!("class {} extends {} {{", ROOT_CLASS, STDLIB_CORE_RESOURCE));
root_class.open(format!("{JS_CONSTRUCTOR}(scope, id) {{"));
Expand All @@ -177,7 +175,7 @@ impl<'a> JSifier<'a> {

output.add_code(root_class);
output.line("const $App = $stdlib.core.App.for(process.env.WING_TARGET);".to_string());
let app_name = source_path.file_stem().unwrap();
let app_name = self.entrypoint_file_path.file_stem().unwrap();
output.line(format!(
"new $App({{ outdir: {}, name: \"{}\", rootConstruct: {}, plugins: {}, isTestEnvironment: {}, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] }}).synth();",
OUTDIR_VAR, app_name, ROOT_CLASS, PLUGINS_VAR, ENV_WING_IS_TEST
Expand Down Expand Up @@ -223,7 +221,7 @@ impl<'a> JSifier<'a> {
}

// Generate a name for the JS file this preflight code will be written to
let preflight_file_name = if is_compilation_init {
let preflight_file_name = if is_entrypoint_file {
PREFLIGHT_FILE_NAME.to_string()
} else {
// remove all non-alphanumeric characters
Expand Down
53 changes: 35 additions & 18 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ use wasm_util::{ptr_to_string, string_to_combined_ptr, WASM_RETURN_ERROR};
use wingii::type_system::TypeSystem;

use crate::docs::Docs;
use crate::parser::normalize_path;
use std::alloc::{alloc, dealloc, Layout};

use std::mem;
use std::{fs, mem};

use crate::ast::Phase;
use crate::type_check::symbol_env::SymbolEnv;
Expand Down Expand Up @@ -161,26 +160,38 @@ pub unsafe extern "C" fn wingc_compile(ptr: u32, len: u32) -> u64 {
let args = ptr_to_string(ptr, len);

let split = args.split(";").collect::<Vec<&str>>();
if split.len() != 3 {
let source_file = Utf8Path::new(split[0]);
let output_dir = split.get(1).map(|s| Utf8Path::new(s));
let absolute_project_dir = split.get(2).map(|s| Utf8Path::new(s));

if !source_file.exists() {
report_diagnostic(Diagnostic {
message: format!("Expected 3 arguments to wingc_compile, got {}", split.len()),
message: format!("Source file cannot be found: {}", source_file),
span: None,
});
return WASM_RETURN_ERROR;
}
let source_path = Utf8Path::new(split[0]);
let output_dir = split.get(1).map(|s| Utf8Path::new(s)).unwrap();
let absolute_project_dir = split.get(2).map(|s| Utf8Path::new(s)).unwrap();

if !source_path.exists() {
if source_file.is_dir() {
report_diagnostic(Diagnostic {
message: format!("Source path cannot be found: {}", source_path),
message: format!("Source path must be a file (not a directory): {}", source_file),
span: None,
});
return WASM_RETURN_ERROR;
}

let results = compile(source_path, None, output_dir, absolute_project_dir);
let source_text = match fs::read_to_string(&source_file) {
Ok(text) => text,
Err(e) => {
report_diagnostic(Diagnostic {
message: format!("Could not read file \"{}\": {}", source_file, e),
span: None,
});
return WASM_RETURN_ERROR;
}
};

let results = compile(source_file, source_text, output_dir, absolute_project_dir);
if results.is_err() {
WASM_RETURN_ERROR
} else {
Expand Down Expand Up @@ -268,11 +279,13 @@ fn add_builtin(name: &str, typ: Type, scope: &mut Scope, types: &mut Types) {

pub fn compile(
source_path: &Utf8Path,
source_text: Option<String>,
out_dir: &Utf8Path,
absolute_project_root: &Utf8Path,
source_text: String,
out_dir: Option<&Utf8Path>,
absolute_project_root: Option<&Utf8Path>,
) -> Result<CompilerOutput, ()> {
let source_path = normalize_path(source_path, None);
let file_name = source_path.file_name().unwrap();
let default_out_dir = Utf8PathBuf::from(format!("{}.out", file_name));
let out_dir = out_dir.unwrap_or(default_out_dir.as_ref());

// -- PARSING PHASE --
let mut files = Files::new();
Expand Down Expand Up @@ -331,7 +344,9 @@ pub fn compile(
json_checker.check(&scope);
}

let project_dir = absolute_project_root;
let project_dir = absolute_project_root
.unwrap_or(source_path.parent().unwrap())
.to_path_buf();

// Verify that the project dir is absolute
if !is_absolute_path(&project_dir) {
Expand Down Expand Up @@ -435,11 +450,13 @@ mod sanity {
fs::remove_dir_all(&out_dir).expect("remove out dir");
}

let test_text = fs::read_to_string(&test_file).expect("read test file");

let result = compile(
&test_file,
None,
&out_dir,
test_file.canonicalize_utf8().unwrap().parent().unwrap(),
test_text,
Some(&out_dir),
Some(test_file.canonicalize_utf8().unwrap().parent().unwrap()),
);

if result.is_err() {
Expand Down
5 changes: 2 additions & 3 deletions libs/wingc/src/lsp/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::files::Files;
use crate::fold::Fold;
use crate::jsify::JSifier;
use crate::lifting::LiftVisitor;
use crate::parser::{normalize_path, parse_wing_project};
use crate::parser::parse_wing_project;
use crate::type_check;
use crate::type_check::jsii_importer::JsiiImportSpec;
use crate::type_check_assert::TypeCheckAssert;
Expand Down Expand Up @@ -136,11 +136,10 @@ fn partial_compile(
reset_diagnostics();

let source_path = Utf8Path::from_path(source_path).expect("invalid unicide path");
let source_path = normalize_path(source_path, None);

let topo_sorted_files = parse_wing_project(
&source_path,
Some(source_text),
source_text,
&mut project_data.files,
&mut project_data.file_graph,
&mut project_data.trees,
Expand Down
Loading

0 comments on commit 65124f1

Please sign in to comment.