diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 2 | ||||
-rw-r--r-- | cli/graph_util.rs | 32 | ||||
-rw-r--r-- | cli/tests/integration/run_tests.rs | 95 |
3 files changed, 128 insertions, 1 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 69635141b..dab935b63 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -58,7 +58,7 @@ deno_npm.workspace = true deno_runtime = { workspace = true, features = ["dont_create_runtime_snapshot", "exclude_runtime_main_js", "include_js_files_for_snapshotting"] } deno_semver.workspace = true deno_task_shell = "=0.13.2" -eszip = "=0.50.0" +eszip = "=0.50.1" napi_sym.workspace = true async-trait.workspace = true diff --git a/cli/graph_util.rs b/cli/graph_util.rs index 3623f49d0..fc530032a 100644 --- a/cli/graph_util.rs +++ b/cli/graph_util.rs @@ -320,8 +320,40 @@ impl ModuleGraphBuilder { self.resolver.force_top_level_package_json_install().await?; } + // add the lockfile redirects to the graph if it's the first time executing + if graph.redirects.is_empty() { + if let Some(lockfile) = &self.lockfile { + let lockfile = lockfile.lock(); + for (from, to) in &lockfile.content.redirects { + if let Ok(from) = ModuleSpecifier::parse(from) { + if let Ok(to) = ModuleSpecifier::parse(to) { + if !matches!(from.scheme(), "file" | "npm") + && !matches!(to.scheme(), "file" | "npm") + { + graph.redirects.insert(from, to); + } + } + } + } + } + } + graph.build(roots, loader, options).await; + // add the redirects in the graph to the lockfile + if !graph.redirects.is_empty() { + if let Some(lockfile) = &self.lockfile { + let graph_redirects = graph + .redirects + .iter() + .filter(|(from, _)| !matches!(from.scheme(), "npm" | "file")); + let mut lockfile = lockfile.lock(); + for (from, to) in graph_redirects { + lockfile.insert_redirect(from.to_string(), to.to_string()); + } + } + } + // ensure that the top level package.json is installed if a // specifier was matched in the package.json self diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 3a385f7cd..22096cb60 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -3,6 +3,7 @@ use deno_core::serde_json::json; use deno_core::url; use deno_runtime::deno_fetch::reqwest; +use pretty_assertions::assert_eq; use std::io::Read; use std::io::Write; use std::process::Command; @@ -973,6 +974,100 @@ fn lock_no_declaration_files() { ); } +#[test] +fn lock_redirects() { + let context = TestContextBuilder::new() + .use_temp_cwd() + .use_http_server() + .add_npm_env_vars() + .build(); + let temp_dir = context.temp_dir(); + temp_dir.write("deno.json", "{}"); // cause a lockfile to be created + temp_dir.write( + "main.ts", + "import 'http://localhost:4546/run/001_hello.js';", + ); + context + .new_command() + .args("run main.ts") + .run() + .skip_output_check(); + let initial_lockfile_text = r#"{ + "version": "2", + "redirects": { + "http://localhost:4546/run/001_hello.js": "http://localhost:4545/run/001_hello.js" + }, + "remote": { + "http://localhost:4545/run/001_hello.js": "c479db5ea26965387423ca438bb977d0b4788d5901efcef52f69871e4c1048c5" + } +} +"#; + assert_eq!(temp_dir.read_to_string("deno.lock"), initial_lockfile_text); + context + .new_command() + .args("run main.ts") + .run() + .assert_matches_text("Hello World\n"); + assert_eq!(temp_dir.read_to_string("deno.lock"), initial_lockfile_text); + + // now try changing where the redirect occurs in the lockfile + temp_dir.write("deno.lock", r#"{ + "version": "2", + "redirects": { + "http://localhost:4546/run/001_hello.js": "http://localhost:4545/echo.ts" + }, + "remote": { + "http://localhost:4545/run/001_hello.js": "c479db5ea26965387423ca438bb977d0b4788d5901efcef52f69871e4c1048c5" + } +} +"#); + + // also, add some npm dependency to ensure it doesn't end up in + // the redirects as they're currently stored separately + temp_dir.write( + "main.ts", + "import 'http://localhost:4546/run/001_hello.js';\n import 'npm:@denotest/esm-basic';\n", + ); + + // it should use the echo script instead + context + .new_command() + .args("run main.ts Hi there") + .run() + .assert_matches_text( + concat!( + "Download http://localhost:4545/echo.ts\n", + "Download http://localhost:4545/npm/registry/@denotest/esm-basic\n", + "Download http://localhost:4545/npm/registry/@denotest/esm-basic/1.0.0.tgz\n", + "Hi, there", + )); + util::assertions::assert_wildcard_match( + &temp_dir.read_to_string("deno.lock"), + r#"{ + "version": "2", + "redirects": { + "http://localhost:4546/run/001_hello.js": "http://localhost:4545/echo.ts" + }, + "remote": { + "http://localhost:4545/echo.ts": "829eb4d67015a695d70b2a33c78b631b29eea1dbac491a6bfcf394af2a2671c2", + "http://localhost:4545/run/001_hello.js": "c479db5ea26965387423ca438bb977d0b4788d5901efcef52f69871e4c1048c5" + }, + "npm": { + "specifiers": { + "@denotest/esm-basic": "@denotest/esm-basic@1.0.0" + }, + "packages": { + "@denotest/esm-basic@1.0.0": { + "integrity": "sha512-[WILDCARD]", + "dependencies": {} + } + } + } +} +"#, + ); +} + itest!(mts_dmts_mjs { args: "run subdir/import.mts", output: "run/mts_dmts_mjs.out", |