Skip to content

Commit

Permalink
fix issues with wingcli-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Aug 22, 2024
1 parent d0de029 commit 20da018
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 58 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion apps/wingcli-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wingcli"
version = "0.74.53"
version = "0.83.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -16,6 +16,8 @@ clap = { version = "4.5.1", features = ["derive", "color"] }
home = "0.5.9"
# Lazily evaluated static variables
lazy_static = "1.4.0"
# Parsing JSON
serde_json = "1.0.125"
# Enum parsing
strum = { version = "0.26.1", features = ["derive"] }
# Colors in terminal
Expand Down
51 changes: 37 additions & 14 deletions apps/wingcli-v2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
mod cli;

use std::{error::Error, io::Write, path::PathBuf, time::Instant};
use std::{
error::Error,
io::Write,
path::PathBuf,
sync::{Mutex, Once},
time::Instant,
};

use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser;
use cli::{print_compiled, print_compiling, print_installing};
use home::home_dir;
use lazy_static::lazy_static;
use strum::{Display, EnumString};
use wingc::compile;
use wingc::{compile, diagnostic::get_diagnostics};

lazy_static! {
static ref HOME_PATH: PathBuf = home_dir().expect("Could not find home directory");
Expand Down Expand Up @@ -80,17 +86,13 @@ fn command_build(source_file: Utf8PathBuf, target: Option<Target>) -> Result<(),
));
let work_dir = target_dir.join(".wing");

// Print that work is being done
// Print "Compiling..."
print_compiling(source_file.as_str());

let sdk_root = WING_CACHE_DIR.join("node_modules").join("@winglang").join("sdk");

// Skip installing the SDK here if we're in a unit test since tests may run in parallel
// TODO: check if the SDK is up to date
if !sdk_root.exists() && !cfg!(test) {
install_sdk()?;
}
tracing::info!("Using SDK at {}", sdk_root);
install_sdk()?;
tracing::info!("Using Wing SDK at {}", sdk_root);

// Special pragma used by wingc to find the SDK types
if !cfg!(test) {
Expand All @@ -103,6 +105,10 @@ fn command_build(source_file: Utf8PathBuf, target: Option<Target>) -> Result<(),
Ok(_) => {}
Err(error) => {
tracing::error!(error = ?error, "Failed");
let diagnostics = get_diagnostics();
for diagnostic in diagnostics {
eprintln!("{}", diagnostic);
}
return Err("Compiler error".into());
}
}
Expand All @@ -113,7 +119,23 @@ fn command_build(source_file: Utf8PathBuf, target: Option<Target>) -> Result<(),
Ok(())
}

// Create a mutex to ensure that the SDK is only installed once
lazy_static! {
static ref INSTALL_SDK_MUTEX: Mutex<()> = Mutex::new(());
}

static INSTALL_SDK_INIT: Once = Once::new();

fn install_sdk() -> Result<(), Box<dyn Error>> {
let _guard = INSTALL_SDK_MUTEX.lock().unwrap();
let mut result = Ok(());
INSTALL_SDK_INIT.call_once(|| {
result = install_sdk_helper();
});
result
}

fn install_sdk_helper() -> Result<(), Box<dyn Error>> {
print_installing("Wing SDK");

std::fs::create_dir_all(WING_CACHE_DIR.as_str())?;
Expand Down Expand Up @@ -173,14 +195,16 @@ fn run_javascript_node(source_file: &Utf8Path, target_dir: &Utf8Path, target: Ta
}

fn initialize_logger() {
let enable_logs = std::env::var("WING_LOG").is_ok_and(|x| !x.is_empty() && x != "off" && x != "0");
let enable_logs = std::env::var("DEBUG").is_ok_and(|x| !x.is_empty() && x != "off" && x != "0");
let enable_colours = std::env::var("WING_LOG_NOCOLOR").is_err();
let show_thread_names = cfg!(test);
if enable_logs {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_target(false)
.with_ansi(enable_colours)
.without_time()
.with_thread_names(show_thread_names)
.init();
}
}
Expand Down Expand Up @@ -216,23 +240,22 @@ mod test {

static INIT: Once = Once::new();

fn initialize() {
fn setup() {
INIT.call_once(|| {
initialize_logger();
install_sdk().expect("Failed to install SDK");
});
}

#[test]
fn test_compile_sim() {
initialize();
setup();
let res = command_build("../../examples/tests/valid/hello.test.w".into(), Some(Target::Sim));
res.expect("Failed to compile to sim");
}

#[test]
fn test_compile_tfaws() {
initialize();
setup();
let res = command_build("../../examples/tests/valid/hello.test.w".into(), Some(Target::TfAws));
res.expect("Failed to compile to tf-aws");
}
Expand Down
16 changes: 2 additions & 14 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ pub unsafe extern "C" fn wingc_compile(ptr: u32, len: u32) -> u64 {
let args = ptr_to_str(ptr, len);

let split = args.split(";").collect::<Vec<&str>>();
if split.len() != 3 {
if split.len() != 2 {
report_diagnostic(Diagnostic {
message: format!("Expected 3 arguments to wingc_compile, got {}", split.len()),
message: format!("Expected 2 arguments to wingc_compile, got {}", split.len()),
span: None,
annotations: vec![],
hints: vec![],
Expand Down Expand Up @@ -387,18 +387,6 @@ pub fn compile(source_path: &Utf8Path, source_text: Option<String>, out_dir: &Ut
asts.insert(file.path.to_owned(), scope);
}

// Verify that the project dir is absolute
if !is_absolute_path(&project_dir) {
report_diagnostic(Diagnostic {
message: format!("Project directory must be absolute: {}", project_dir),
span: None,
annotations: vec![],
hints: vec![],
severity: DiagnosticSeverity::Error,
});
return Err(());
}

let mut jsifier = JSifier::new(&mut types, &files, &file_graph, &source_path, &out_dir);

// -- LIFTING PHASE --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class $Root extends $stdlib.std.Resource {
let $preflightTypesMap = {};
const lib = $helpers.bringJs(`${__dirname}/preflight.extendnonentrypoint-1.cjs`, $preflightTypesMap);
$helpers.nodeof(this).root.$preflightTypesMap = $preflightTypesMap;
const f = new lib.Foo(this, "Foo");
const f = globalThis.$ClassFactory.new("rootpkg.Foo", lib.Foo, this, "Foo");
}
}
const $APP = $PlatformManager.createApp({ outdir: $outdir, name: "bring_extend_non_entry.test", rootConstruct: $Root, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,19 @@ class $Root extends $stdlib.std.Resource {
return `
require("${$helpers.normalPath(__dirname)}/inflight.$Closure2-3.cjs")({
$expect_Util: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglang/sdk.expect.Util") ?? expect.Util, "@winglang/sdk/expect", "Util"))},
$file2_Q: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(file2.Q, "", "Q"))},
$file2_Q: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir.Q") ?? file2.Q, "", "Q"))},
})
`;
}
get _liftMap() {
return ({
"handle": [
[$stdlib.core.toLiftableModuleType(file2.Q, "", "Q"), ["greet"]],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglang/sdk.expect.Util") ?? expect.Util, "@winglang/sdk/expect", "Util"), ["equal"]],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir.Q") ?? file2.Q, "", "Q"), ["greet"]],
],
"$inflight_init": [
[$stdlib.core.toLiftableModuleType(file2.Q, "", "Q"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglang/sdk.expect.Util") ?? expect.Util, "@winglang/sdk/expect", "Util"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir.Q") ?? file2.Q, "", "Q"), []],
],
});
}
Expand Down Expand Up @@ -405,8 +405,8 @@ class $Root extends $stdlib.std.Resource {
});
}
}
const store = new file1.Store(this, "Store");
const q = new file2.Q(this, "Q");
const store = globalThis.$ClassFactory.new("rootpkg.Store", file1.Store, this, "Store");
const q = globalThis.$ClassFactory.new("rootpkg.subdir.Q", file2.Q, this, "Q");
(expect.Util.equal((file2.Q.preflightGreet("foo")), "Hello foo"));
globalThis.$ClassFactory.new("@winglang/sdk.std.Test", std.Test, this, "test:add data to store", new $Closure1(this, "$Closure1"));
globalThis.$ClassFactory.new("@winglang/sdk.std.Test", std.Test, this, "test:greet", new $Closure2(this, "$Closure2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ class $Root extends $stdlib.std.Resource {
const w = $helpers.bringJs(`${__dirname}/preflight.widget-1.cjs`, $preflightTypesMap);
const subdir = $helpers.bringJs(`${__dirname}/preflight.subdir2-6.cjs`, $preflightTypesMap);
$helpers.nodeof(this).root.$preflightTypesMap = $preflightTypesMap;
const widget1 = new w.Widget(this, "widget1");
const widget1 = globalThis.$ClassFactory.new("rootpkg.subdir2.inner.Widget", w.Widget, this, "widget1");
$helpers.assert($helpers.eq((widget1.compute()), 42), "widget1.compute() == 42");
const foo = new subdir.Foo(this, "Foo");
const foo = globalThis.$ClassFactory.new("rootpkg.subdir2.Foo", subdir.Foo, this, "Foo");
$helpers.assert($helpers.eq((foo.foo()), "foo"), "foo.foo() == \"foo\"");
const bar = new subdir.Bar(this, "Bar");
const bar = globalThis.$ClassFactory.new("rootpkg.subdir2.Bar", subdir.Bar, this, "Bar");
$helpers.assert($helpers.eq((bar.bar()), "bar"), "bar.bar() == \"bar\"");
const widget2 = new subdir.inner.Widget(this, "widget2");
const widget2 = globalThis.$ClassFactory.new("rootpkg.subdir2.inner.Widget", subdir.inner.Widget, this, "widget2");
$helpers.assert($helpers.eq((widget2.compute()), 42), "widget2.compute() == 42");
$helpers.assert($helpers.eq((foo.checkWidget(widget2)), 1379), "foo.checkWidget(widget2) == 1379");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ class $Root extends $stdlib.std.Resource {
static _toInflightType() {
return `
require("${$helpers.normalPath(__dirname)}/inflight.$Closure1-3.cjs")({
$fixture_Store: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(fixture.Store, "", "Store"))},
$fixture_Store: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.Store") ?? fixture.Store, "", "Store"))},
})
`;
}
get _liftMap() {
return ({
"handle": [
[$stdlib.core.toLiftableModuleType(fixture.Store, "", "Store"), ["makeKeyInflight"]],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.Store") ?? fixture.Store, "", "Store"), ["makeKeyInflight"]],
],
"$inflight_init": [
[$stdlib.core.toLiftableModuleType(fixture.Store, "", "Store"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.Store") ?? fixture.Store, "", "Store"), []],
],
});
}
}
new fixture.Store(this, "Store");
globalThis.$ClassFactory.new("@winglibs/testfixture.Store", fixture.Store, this, "Store");
const fave_num = fixture.FavoriteNumbers.SEVEN;
const fave_num2 = testfixture.FavoriteNumbers.SEVEN;
const fave_num3 = testfixture2.FavoriteNumbers.SEVEN;
Expand Down Expand Up @@ -237,7 +237,7 @@ class Store extends $stdlib.std.Resource {
static _toInflightType() {
return `
require("${$helpers.normalPath(__dirname)}/inflight.Store-2.cjs")({
$myutil_Util: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(myutil.Util, "", "Util"))},
$myutil_Util: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.subdir.Util") ?? myutil.Util, "", "Util"))},
})
`;
}
Expand All @@ -251,12 +251,12 @@ class Store extends $stdlib.std.Resource {
get _liftMap() {
return ({
"set": [
[$stdlib.core.toLiftableModuleType(myutil.Util, "", "Util"), ["double"]],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.subdir.Util") ?? myutil.Util, "", "Util"), ["double"]],
[this.data, ["put"]],
[this.handlers, []],
],
"$inflight_init": [
[$stdlib.core.toLiftableModuleType(myutil.Util, "", "Util"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("@winglibs/testfixture.subdir.Util") ?? myutil.Util, "", "Util"), []],
[this.data, []],
[this.handlers, []],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,19 @@ class $Root extends $stdlib.std.Resource {
static _toInflightType() {
return `
require("${$helpers.normalPath(__dirname)}/inflight.$Closure6-5.cjs")({
$subdir_InflightClass: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(subdir.InflightClass, "", "InflightClass"))},
$subdir_InflightClass: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir2.InflightClass") ?? subdir.InflightClass, "", "InflightClass"))},
})
`;
}
get _liftMap() {
return ({
"handle": [
[$helpers.preflightClassSingleton(this, 5), ["method"]],
[$stdlib.core.toLiftableModuleType(subdir.InflightClass, "", "InflightClass"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir2.InflightClass") ?? subdir.InflightClass, "", "InflightClass"), []],
],
"$inflight_init": [
[$helpers.preflightClassSingleton(this, 5), []],
[$stdlib.core.toLiftableModuleType(subdir.InflightClass, "", "InflightClass"), []],
[$stdlib.core.toLiftableModuleType(globalThis.$ClassFactory.resolveType("rootpkg.subdir2.InflightClass") ?? subdir.InflightClass, "", "InflightClass"), []],
],
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ class $Root extends $stdlib.std.Resource {
}
}
const bucket = globalThis.$ClassFactory.new("@winglang/sdk.cloud.Bucket", cloud.Bucket, this, "Bucket");
const b1 = new MyBucket(this, "b1", bucket);
const b2 = new MyBucket(this, "b2", bucket);
const b1 = globalThis.$ClassFactory.new("rootpkg.MyBucket", MyBucket, this, "b1", bucket);
const b2 = globalThis.$ClassFactory.new("rootpkg.MyBucket", MyBucket, this, "b2", bucket);
const api = globalThis.$ClassFactory.new("@winglang/sdk.cloud.Api", cloud.Api, this, "Api");
(api.get("/", new $Closure1(this, "$Closure1")));
globalThis.$ClassFactory.new("@winglang/sdk.std.Test", std.Test, this, "test:call endpoint", new $Closure2(this, "$Closure2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class LibClass extends $stdlib.std.Resource {
super($scope, $id);
}
static createFoo($scope, id) {
return new Foo($scope, id);
return globalThis.$ClassFactory.new("rootpkg.Foo", Foo, $scope, id);
}
static _toInflightType() {
return `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class LibClass extends $stdlib.std.Resource {
super($scope, $id);
}
static createFoo($scope, id) {
return new Foo($scope, id);
return globalThis.$ClassFactory.new("rootpkg.Foo", Foo, $scope, id);
}
static _toInflightType() {
return `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ class $Root extends $stdlib.std.Resource {
(std.Number.fromJson("cool", { unsafe: true }));
$macros.__Struct_fromJson(false, Student, ({"obviously": "not a student"}), { unsafe: true });
globalThis.$ClassFactory.new("@winglang/sdk.std.Test", std.Test, this, "test:unsafe flight", new $Closure5(this, "$Closure5"));
new otherExternalStructs.UsesStructInImportedFile(this, "UsesStructInImportedFile");
globalThis.$ClassFactory.new("rootpkg.subdir.UsesStructInImportedFile", otherExternalStructs.UsesStructInImportedFile, this, "UsesStructInImportedFile");
}
}
const $APP = $PlatformManager.createApp({ outdir: $outdir, name: "struct_from_json.test", rootConstruct: $Root, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] });
Expand Down
2 changes: 1 addition & 1 deletion tools/hangar/src/unsupported.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test("unsupported resource in target", async ({ expect }) => {
},
});

expect(sanitizeOutput(result.stderr)).toMatchInlineSnapshot('"Error: A Google Cloud region must be specified through the GOOGLE_REGION environment variable."');
expect(sanitizeOutput(result.stderr)).toMatchInlineSnapshot(`"Error: A Google Cloud region must be specified through the GOOGLE_REGION environment variable."`);
});

function sanitizeOutput(inputString: string): string {
Expand Down

0 comments on commit 20da018

Please sign in to comment.