Skip to content

Commit

Permalink
fix: error running wingcli-v2 tests locally (#6689)
Browse files Browse the repository at this point in the history
@hasanaburayyan reported an issue running the wingcli-v2 tests locally:

```
running 2 tests
 Installing Wing SDK
test test::test_compile_sim ... FAILED
test test::test_compile_tfaws ... FAILED

failures:

---- test::test_compile_sim stdout ----
thread 'test::test_compile_sim' panicked at apps/wingcli-v2/src/main.rs:208:27:
Failed to install SDK: "Failed to install SDK. stdout: . stderr: npm ERR! code EUNSUPPORTEDPROTOCOL\nnpm ERR! Unsupported URL Type \"workspace:\": workspace:^\n\nnpm ERR! A complete log of this run can be found in: /Users/hasan/.npm/_logs/2024-06-12T16_06_47_837Z-debug-0.log\n"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- test::test_compile_tfaws stdout ----
thread 'test::test_compile_tfaws' panicked at apps/wingcli-v2/src/main.rs:207:14:
Once instance has previously been poisoned
```

After a few hours we weren't able to diagnose the root cause. But as a workaround, we can avoid the issue by adding the locally built SDK to the NODE_PATH instead of using `npm install` to link the locally built SDK to the cache directory.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
Chriscbr authored Jun 12, 2024
1 parent 73abe7d commit 6646ef0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 167 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 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.59.24"
version = "0.74.53"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 2 additions & 1 deletion apps/wingcli-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"dependencies": {
"@winglang/wingii": "workspace:^",
"@winglang/wingc": "workspace:^"
"@winglang/wingc": "workspace:^",
"@winglang/sdk": "workspace:^"
},
"volta": {
"extends": "../../package.json"
Expand Down
44 changes: 29 additions & 15 deletions apps/wingcli-v2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,18 @@ fn command_build(source_file: Utf8PathBuf, target: Option<Target>) -> Result<(),
print_compiling(source_file.as_str());

let sdk_root = WING_CACHE_DIR.join("node_modules").join("@winglang").join("sdk");
if !sdk_root.exists() {

// 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()?;
} else {
// TODO: check that the SDK version matches the CLI version
if cfg!(test) {
// For now, always reinstall the SDK in tests
install_sdk()?;
}
}
tracing::info!("Using SDK at {}", sdk_root);

// Special pragma used by wingc to find the SDK types
std::env::set_var("WINGSDK_MANIFEST_ROOT", &sdk_root);
if !cfg!(test) {
std::env::set_var("WINGSDK_MANIFEST_ROOT", &sdk_root);
}

let result = compile(&project_dir, &source_file, None, &work_dir);

Expand All @@ -120,23 +119,25 @@ fn install_sdk() -> Result<(), Box<dyn Error>> {
std::fs::create_dir_all(WING_CACHE_DIR.as_str())?;
let mut install_command = std::process::Command::new("npm");
install_command.arg("install").arg("esbuild"); // TODO: should this not be an optional dependency?
if cfg!(test) {
install_command.arg(format!("file:{}/../../libs/wingsdk", env!("CARGO_MANIFEST_DIR")));
} else {

// No need to install the latest verison of SDK from npm in tests
if !cfg!(test) {
install_command.arg(format!("@winglang/sdk@{}", env!("CARGO_PKG_VERSION")));
}

install_command.current_dir(WING_CACHE_DIR.as_str());
install_command.stdout(std::process::Stdio::piped());
install_command.stderr(std::process::Stdio::piped());

tracing::info!("Running command: {:?}", install_command);

let output = install_command.output()?;
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let error_message = format!("Failed to install SDK. stdout: {}. stderr: {}", stdout, stderr);
return Err(error_message.into());
}

Ok(())
}

Expand All @@ -146,7 +147,19 @@ fn run_javascript_node(source_file: &Utf8Path, target_dir: &Utf8Path, target: Ta

let mut command = std::process::Command::new("node");
command.arg(target_dir.join(".wing").join("preflight.cjs"));
command.env("NODE_PATH", WING_CACHE_DIR.join("node_modules").as_str());

let mut node_path = WING_CACHE_DIR.join("node_modules").to_string();

// For tests, add the local version of the SDK to the NODE_PATH
if cfg!(test) {
node_path = format!(
"{}:{}",
Utf8Path::new(env!("CARGO_MANIFEST_DIR")).join("node_modules"),
node_path
);
}

command.env("NODE_PATH", node_path);
command.env("WING_PLATFORMS", target.to_string());
command.env("WING_SOURCE_DIR", source_dir);
command.env("WING_SYNTH_DIR", target_dir);
Expand Down Expand Up @@ -205,6 +218,7 @@ mod test {

fn initialize() {
INIT.call_once(|| {
initialize_logger();
install_sdk().expect("Failed to install SDK");
});
}
Expand All @@ -213,13 +227,13 @@ mod test {
fn test_compile_sim() {
initialize();
let res = command_build("../../examples/tests/valid/hello.test.w".into(), Some(Target::Sim));
assert!(res.is_ok());
res.expect("Failed to compile to sim");
}

#[test]
fn test_compile_tfaws() {
initialize();
let res = command_build("../../examples/tests/valid/hello.test.w".into(), Some(Target::TfAws));
assert!(res.is_ok());
res.expect("Failed to compile to tf-aws");
}
}
Loading

0 comments on commit 6646ef0

Please sign in to comment.