Skip to content

Commit

Permalink
feat(lockfile): add redirects to the lockfile (denoland#20262)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 29, 2023
1 parent bdc9121 commit c4451d3
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ deno_runtime = { version = "0.125.0", path = "./runtime" }
napi_sym = { version = "0.47.0", path = "./cli/napi/sym" }
deno_bench_util = { version = "0.111.0", path = "./bench_util" }
test_util = { path = "./test_util" }
deno_lockfile = "0.15.0"
deno_lockfile = "0.16.2"
deno_media_type = { version = "0.1.1", features = ["module_specifier"] }
deno_npm = "0.12.0"
deno_npm = "0.13.0"
deno_semver = "0.4.0"

# exts
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions cli/graph_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 'https://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": {
"https://localhost:4546/run/001_hello.js": "https://localhost:4545/run/001_hello.js"
},
"remote": {
"https://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": {
"https://localhost:4546/run/001_hello.js": "https://localhost:4545/echo.ts"
},
"remote": {
"https://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 'https://localhost:4546/run/001_hello.js';\n import 'npm:@denotest/esm-basic';\n",
);