diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-07-15 12:11:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-15 12:11:09 -0700 |
commit | 04f9db5b2217fe06f88e76146aac6362ff0b0b86 (patch) | |
tree | aa4becb2529141de3e6bb8d3ba430f3c7e036902 /cli/npm/managed | |
parent | 29186d7e5963f2398b28ee2c043b27e4881075ef (diff) |
fix(node): Fix `--allow-scripts` with no `deno.json` (#24533)
We would resolve the wrong package.json, resulting in an inability to
run CJS (or other node-mode) scripts
Diffstat (limited to 'cli/npm/managed')
-rw-r--r-- | cli/npm/managed/mod.rs | 29 | ||||
-rw-r--r-- | cli/npm/managed/resolvers/local.rs | 19 |
2 files changed, 32 insertions, 16 deletions
diff --git a/cli/npm/managed/mod.rs b/cli/npm/managed/mod.rs index 1020a57e9..a18ad4d7f 100644 --- a/cli/npm/managed/mod.rs +++ b/cli/npm/managed/mod.rs @@ -518,22 +518,25 @@ impl ManagedCliNpmResolver { } } +fn npm_process_state( + snapshot: ValidSerializedNpmResolutionSnapshot, + node_modules_path: Option<&Path>, +) -> String { + serde_json::to_string(&NpmProcessState { + kind: NpmProcessStateKind::Snapshot(snapshot.into_serialized()), + local_node_modules_path: node_modules_path + .map(|p| p.to_string_lossy().to_string()), + }) + .unwrap() +} + impl NpmResolver for ManagedCliNpmResolver { /// Gets the state of npm for the process. fn get_npm_process_state(&self) -> String { - serde_json::to_string(&NpmProcessState { - kind: NpmProcessStateKind::Snapshot( - self - .resolution - .serialized_valid_snapshot() - .into_serialized(), - ), - local_node_modules_path: self - .fs_resolver - .node_modules_path() - .map(|p| p.to_string_lossy().to_string()), - }) - .unwrap() + npm_process_state( + self.resolution.serialized_valid_snapshot(), + self.fs_resolver.node_modules_path().map(|p| p.as_path()), + ) } fn resolve_package_folder_from_package( diff --git a/cli/npm/managed/resolvers/local.rs b/cli/npm/managed/resolvers/local.rs index 937b2a899..cbd820320 100644 --- a/cli/npm/managed/resolvers/local.rs +++ b/cli/npm/managed/resolvers/local.rs @@ -282,8 +282,12 @@ fn resolve_baseline_custom_commands( custom_commands .insert("npm".to_string(), Rc::new(crate::task_runner::NpmCommand)); - custom_commands - .insert("node".to_string(), Rc::new(crate::task_runner::NodeCommand)); + custom_commands.insert( + "node".to_string(), + Rc::new(crate::task_runner::NodeCommand { + force_node_modules_dir: true, + }), + ); custom_commands.insert( "node-gyp".to_string(), @@ -687,7 +691,16 @@ async fn sync_resolution_with_fs( &deno_local_registry_dir, )?; let init_cwd = lifecycle_scripts.initial_cwd.as_deref().unwrap(); + let process_state = crate::npm::managed::npm_process_state( + snapshot.as_valid_serialized(), + Some(root_node_modules_dir_path), + ); + let mut env_vars = crate::task_runner::real_env_vars(); + env_vars.insert( + crate::args::NPM_RESOLUTION_STATE_ENV_VAR_NAME.to_string(), + process_state, + ); for (package, package_path, scripts_run_path) in packages_with_scripts { // add custom commands for binaries from the package's dependencies. this will take precedence over the // baseline commands, so if the package relies on a bin that conflicts with one higher in the dependency tree, the @@ -710,7 +723,7 @@ async fn sync_resolution_with_fs( task_name: script_name, script, cwd: &package_path, - env_vars: crate::task_runner::real_env_vars(), + env_vars: env_vars.clone(), custom_commands: custom_commands.clone(), init_cwd, argv: &[], |