-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: top level package.json install when node_modules dir is explici…
…tly opted into (#19233) When someone explicitly opts into using the node_modules dir via `--node-modules-dir` or setting `"nodeModulesDir": true` in the deno.json file, we should eagerly ensure a top level package.json install is done on startup. This was initially always done when we added package.json support and a package.json was auto-discovered, but scaled it back to be lazily done when a bare specifier matched an entry in the package.json because of how disruptive it was for people using Deno scripts in Node projects. That said, it does not make sense for someone to opt-into having deno control and use their node_modules directory and not want a package.json install to occur. If such a rare scenario exists, the `DENO_NO_PACKAGE_JSON=1` environment variable can be set. Ideally, we would only ever use a node_modules directory with this explicit opt-in so everything is very clear, but we still have this automatic scenario when there's a package.json in order to make more node projects work out of the box.
- Loading branch information
Showing
4 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use deno_core::serde_json; | ||
use deno_core::serde_json::json; | ||
use deno_core::serde_json::Value; | ||
use pretty_assertions::assert_eq; | ||
use std::process::Stdio; | ||
|
@@ -1924,3 +1925,73 @@ pub fn node_modules_dir_config_file() { | |
.run(); | ||
assert!(node_modules_dir.exists()); | ||
} | ||
|
||
#[test] | ||
fn top_level_install_package_json_explicit_opt_in() { | ||
let test_context = TestContextBuilder::for_npm().use_temp_cwd().build(); | ||
let temp_dir = test_context.temp_dir(); | ||
let node_modules_dir = temp_dir.path().join("node_modules"); | ||
let rm_created_files = || { | ||
std::fs::remove_dir_all(&node_modules_dir).unwrap(); | ||
std::fs::remove_file(temp_dir.path().join("deno.lock")).unwrap(); | ||
}; | ||
|
||
// when the node_modules_dir is explicitly opted into, we should always | ||
// ensure a top level package.json install occurs | ||
temp_dir.write("deno.json", "{ \"nodeModulesDir\": true }"); | ||
temp_dir.write( | ||
"package.json", | ||
"{ \"dependencies\": { \"@denotest/esm-basic\": \"1.0\" }}", | ||
); | ||
|
||
temp_dir.write("main.ts", "console.log(5);"); | ||
let output = test_context.new_command().args("cache main.ts").run(); | ||
output.assert_matches_text( | ||
concat!( | ||
"Download http://localhost:4545/npm/registry/@denotest/esm-basic\n", | ||
"Download http://localhost:4545/npm/registry/@denotest/esm-basic/1.0.0.tgz\n", | ||
"Initialize @denotest/[email protected]\n", | ||
) | ||
); | ||
|
||
rm_created_files(); | ||
let output = test_context | ||
.new_command() | ||
.args_vec(["eval", "console.log(5)"]) | ||
.run(); | ||
output.assert_matches_text(concat!( | ||
"Initialize @denotest/[email protected]\n", | ||
"5\n" | ||
)); | ||
|
||
rm_created_files(); | ||
let output = test_context | ||
.new_command() | ||
.args("run -") | ||
.stdin("console.log(5)") | ||
.run(); | ||
output.assert_matches_text(concat!( | ||
"Initialize @denotest/[email protected]\n", | ||
"5\n" | ||
)); | ||
|
||
// now ensure this is cached in the lsp | ||
rm_created_files(); | ||
let mut client = test_context.new_lsp_command().build(); | ||
client.initialize_default(); | ||
let file_uri = temp_dir.uri().join("file.ts").unwrap(); | ||
client.did_open(json!({ | ||
"textDocument": { | ||
"uri": file_uri, | ||
"languageId": "typescript", | ||
"version": 1, | ||
"text": "", | ||
} | ||
})); | ||
client.write_request( | ||
"deno/cache", | ||
json!({ "referrer": { "uri": file_uri }, "uris": [] }), | ||
); | ||
|
||
assert!(node_modules_dir.join("@denotest").exists()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters