fix: ensure no node_modules directory is created when a package.json exists and no npm dependencies are used (#18134)

Closes #18133
Closes #18038
This commit is contained in:
David Sherret 2023-03-13 10:03:19 -04:00 committed by GitHub
parent 983447e860
commit 11f225ef7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View file

@ -127,6 +127,11 @@ mod map_to_vec {
}
impl NpmResolutionSnapshot {
/// Gets if this snapshot is empty.
pub fn is_empty(&self) -> bool {
self.packages.is_empty() && self.pending_unresolved_packages.is_empty()
}
/// Resolve a package from a package requirement.
pub fn resolve_pkg_from_pkg_req(
&self,

View file

@ -236,6 +236,10 @@ async fn sync_resolution_with_fs(
registry_url: &Url,
root_node_modules_dir_path: &Path,
) -> Result<(), AnyError> {
if snapshot.is_empty() {
return Ok(()); // don't create the directory
}
let deno_local_registry_dir = root_node_modules_dir_path.join(".deno");
fs::create_dir_all(&deno_local_registry_dir).with_context(|| {
format!("Creating '{}'", deno_local_registry_dir.display())

View file

@ -2867,6 +2867,44 @@ fn package_json_error_dep_value_test() {
.assert_matches_file("package_json/invalid_value/task.out");
}
#[test]
fn package_json_no_node_modules_dir_created() {
// it should not create a node_modules directory
let context = TestContextBuilder::new()
.add_npm_env_vars()
.use_temp_cwd()
.build();
let temp_dir = context.deno_dir();
temp_dir.write("deno.json", "{}");
temp_dir.write("package.json", "{}");
temp_dir.write("main.ts", "");
context.new_command().args("run main.ts").run();
assert!(!temp_dir.path().join("node_modules").exists());
}
#[test]
fn node_modules_dir_no_npm_specifiers_no_dir_created() {
// it should not create a node_modules directory
let context = TestContextBuilder::new()
.add_npm_env_vars()
.use_temp_cwd()
.build();
let temp_dir = context.deno_dir();
temp_dir.write("deno.json", "{}");
temp_dir.write("main.ts", "");
context
.new_command()
.args("run --node-modules-dir main.ts")
.run();
assert!(!temp_dir.path().join("node_modules").exists());
}
itest!(wasm_streaming_panic_test {
args: "run run/wasm_streaming_panic_test.js",
output: "run/wasm_streaming_panic_test.js.out",