Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge queue: embarking main (6a9ce9d) and #4342 together #4345

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/wing/src/commands/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -45,6 +46,26 @@ 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: 2 additions & 8 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, fs, process};
use std::{env, process};
use wingc::{compile, diagnostic::get_diagnostics};

pub fn main() {
Expand All @@ -13,15 +13,9 @@ 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,
source_text,
Some(&target_dir),
Some(source_path.parent().unwrap()),
);
let results = compile(&source_path, None, &target_dir, source_path.parent().unwrap());
if results.is_err() {
let mut diags = get_diagnostics();
// Sort error messages by line number (ascending)
Expand Down
20 changes: 11 additions & 9 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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 @@ -72,8 +73,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 entrypoint file of the Wing application.
entrypoint_file_path: &'a Utf8Path,
/// The path that compilation started at (file or directory)
compilation_init_path: &'a Utf8Path,
}

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

let mut output = CodeMaker::default();

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

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

if is_entrypoint_file {
if is_entrypoint {
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 @@ -175,7 +177,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 = self.entrypoint_file_path.file_stem().unwrap();
let app_name = source_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 @@ -221,7 +223,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_entrypoint_file {
let preflight_file_name = if is_compilation_init {
PREFLIGHT_FILE_NAME.to_string()
} else {
// remove all non-alphanumeric characters
Expand Down
53 changes: 18 additions & 35 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ 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::{fs, mem};
use std::mem;

use crate::ast::Phase;
use crate::type_check::symbol_env::SymbolEnv;
Expand Down Expand Up @@ -160,38 +161,26 @@ 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>>();
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() {
if split.len() != 3 {
report_diagnostic(Diagnostic {
message: format!("Source file cannot be found: {}", source_file),
message: format!("Expected 3 arguments to wingc_compile, got {}", split.len()),
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_file.is_dir() {
if !source_path.exists() {
report_diagnostic(Diagnostic {
message: format!("Source path must be a file (not a directory): {}", source_file),
message: format!("Source path cannot be found: {}", source_path),
span: None,
});
return WASM_RETURN_ERROR;
}

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);
let results = compile(source_path, None, output_dir, absolute_project_dir);
if results.is_err() {
WASM_RETURN_ERROR
} else {
Expand Down Expand Up @@ -279,13 +268,11 @@ fn add_builtin(name: &str, typ: Type, scope: &mut Scope, types: &mut Types) {

pub fn compile(
source_path: &Utf8Path,
source_text: String,
out_dir: Option<&Utf8Path>,
absolute_project_root: Option<&Utf8Path>,
source_text: Option<String>,
out_dir: &Utf8Path,
absolute_project_root: &Utf8Path,
) -> Result<CompilerOutput, ()> {
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());
let source_path = normalize_path(source_path, None);

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

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

// Verify that the project dir is absolute
if !is_absolute_path(&project_dir) {
Expand Down Expand Up @@ -450,13 +435,11 @@ 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,
test_text,
Some(&out_dir),
Some(test_file.canonicalize_utf8().unwrap().parent().unwrap()),
None,
&out_dir,
test_file.canonicalize_utf8().unwrap().parent().unwrap(),
);

if result.is_err() {
Expand Down
5 changes: 3 additions & 2 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::parse_wing_project;
use crate::parser::{normalize_path, 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,10 +136,11 @@ 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,
source_text,
Some(source_text),
&mut project_data.files,
&mut project_data.file_graph,
&mut project_data.trees,
Expand Down
Loading
Loading