diff --git a/cli/npm/mod.rs b/cli/npm/mod.rs index 3f1a84b67e1b16..3971b1a0b37edd 100644 --- a/cli/npm/mod.rs +++ b/cli/npm/mod.rs @@ -190,6 +190,8 @@ impl NpmFetchResolver { } } +pub const NPM_CONFIG_USER_AGENT_ENV_VAR: &str = "npm_config_user_agent"; + pub fn get_npm_config_user_agent() -> String { format!( "deno/{} npm/? deno/{} {} {}", diff --git a/cli/task_runner.rs b/cli/task_runner.rs index 95e8409143fe79..43840e868de83e 100644 --- a/cli/task_runner.rs +++ b/cli/task_runner.rs @@ -155,6 +155,12 @@ fn prepare_env_vars( initial_cwd.to_string_lossy().to_string(), ); } + if !env_vars.contains_key(crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR) { + env_vars.insert( + crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR.into(), + crate::npm::get_npm_config_user_agent(), + ); + } if let Some(node_modules_dir) = node_modules_dir { prepend_to_path( &mut env_vars, @@ -215,10 +221,6 @@ impl ShellCommand for NpmCommand { args.extend(context.args.iter().skip(1).cloned()); let mut state = context.state; - state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); state.apply_env_var(USE_PKG_JSON_HIDDEN_ENV_VAR_NAME, "1"); return ExecutableCommand::new( "deno".to_string(), @@ -250,12 +252,8 @@ pub struct NodeCommand; impl ShellCommand for NodeCommand { fn execute( &self, - mut context: ShellCommandContext, + context: ShellCommandContext, ) -> LocalBoxFuture<'static, ExecuteResult> { - context.state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); // run with deno if it's a simple invocation, fall back to node // if there are extra flags let mut args = Vec::with_capacity(context.args.len()); @@ -296,13 +294,8 @@ pub struct NodeGypCommand; impl ShellCommand for NodeGypCommand { fn execute( &self, - mut context: ShellCommandContext, + context: ShellCommandContext, ) -> LocalBoxFuture<'static, ExecuteResult> { - context.state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); - // at the moment this shell command is just to give a warning if node-gyp is not found // in the future, we could try to run/install node-gyp for the user with deno if context.state.resolve_command_path("node-gyp").is_err() { @@ -325,14 +318,10 @@ impl ShellCommand for NpxCommand { ) -> LocalBoxFuture<'static, ExecuteResult> { if let Some(first_arg) = context.args.first().cloned() { if let Some(command) = context.state.resolve_custom_command(&first_arg) { - let mut context = ShellCommandContext { + let context = ShellCommandContext { args: context.args.iter().skip(1).cloned().collect::>(), ..context }; - context.state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); command.execute(context) } else { // can't find the command, so fallback to running the real npx command @@ -363,7 +352,7 @@ struct NpmPackageBinCommand { impl ShellCommand for NpmPackageBinCommand { fn execute( &self, - mut context: ShellCommandContext, + context: ShellCommandContext, ) -> LocalBoxFuture<'static, ExecuteResult> { let mut args = vec![ "run".to_string(), @@ -407,10 +396,6 @@ impl ShellCommand for NodeModulesFileRunCommand { "deno".to_string(), std::env::current_exe().unwrap(), ); - context.state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); // set this environment variable so that the launched process knows the npm command name context .state diff --git a/cli/tools/run/mod.rs b/cli/tools/run/mod.rs index 1cf3141986dea7..bebb3f5881bbf6 100644 --- a/cli/tools/run/mod.rs +++ b/cli/tools/run/mod.rs @@ -34,7 +34,7 @@ fn set_npm_user_agent() { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| { std::env::set_var( - "npm_config_user_agent", + crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR, crate::npm::get_npm_config_user_agent(), ); }); diff --git a/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json b/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json index 1b4c39ebf3622f..9071173467f3bf 100644 --- a/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json +++ b/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json @@ -3,5 +3,8 @@ "version": "1.0.0", "bin": { "print-npm-user-agent": "index.js" + }, + "scripts": { + "postinstall": "echo postinstall && node index.js && exit 1" } } \ No newline at end of file diff --git a/tests/specs/npm/user_agent_env_var/__test__.jsonc b/tests/specs/npm/user_agent_env_var/__test__.jsonc index dffe16d75f3feb..a2af970a0e15e5 100644 --- a/tests/specs/npm/user_agent_env_var/__test__.jsonc +++ b/tests/specs/npm/user_agent_env_var/__test__.jsonc @@ -1,7 +1,7 @@ { "tempDir": true, "tests": { - "run_npm_package": { + "set_for_npm_package": { "steps": [ { "args": "install", @@ -13,37 +13,32 @@ } ] }, - "npx": { + "unset_for_local_file": { "steps": [ { - "args": "install", - "output": "[WILDCARD]" - }, - { - "if": "unix", - "args": "task run-with-npx", - "output": "npx.out" + "args": "run -A main.ts", + "output": "Download [WILDCARD]\nnpm_config_user_agent: undefined\n" } ] }, - "bin_command": { + "set_for_tasks": { "steps": [ - { "args": "install", "output": "[WILDCARD]" }, + { + "args": "install", + "output": "[WILDCARD]" + }, { "args": "task run-via-bin", - "output": "Task run-via-bin print-npm-user-agent\nnpm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]\n" + "output": "bin_command.out" } ] }, - "npm_run": { + "set_for_lifecycle_scripts": { "steps": [ { - "args": "install", - "output": "[WILDCARD]" - }, - { - "args": "task run-via-other-task", - "output": "npm_run.out" + "args": "install --allow-scripts", + "output": "postinstall.out", + "exitCode": 1 } ] } diff --git a/tests/specs/npm/user_agent_env_var/bin_command.out b/tests/specs/npm/user_agent_env_var/bin_command.out index a630ac412f2c0e..c8e94130380eb0 100644 --- a/tests/specs/npm/user_agent_env_var/bin_command.out +++ b/tests/specs/npm/user_agent_env_var/bin_command.out @@ -1 +1,2 @@ +Task run-via-bin print-npm-user-agent npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD] diff --git a/tests/specs/npm/user_agent_env_var/deno.jsonc b/tests/specs/npm/user_agent_env_var/deno.jsonc index 28a9877e69274c..fbd70ec480291a 100644 --- a/tests/specs/npm/user_agent_env_var/deno.jsonc +++ b/tests/specs/npm/user_agent_env_var/deno.jsonc @@ -1,3 +1,3 @@ { "nodeModulesDir": "auto" -} \ No newline at end of file +} diff --git a/tests/specs/npm/user_agent_env_var/deno.lock b/tests/specs/npm/user_agent_env_var/deno.lock deleted file mode 100644 index 3d1bdc4cda729e..00000000000000 --- a/tests/specs/npm/user_agent_env_var/deno.lock +++ /dev/null @@ -1,18 +0,0 @@ -{ - "version": "4", - "specifiers": { - "npm:@denotest/print-npm-user-agent@1.0.0": "1.0.0" - }, - "npm": { - "@denotest/print-npm-user-agent@1.0.0": { - "integrity": "sha512-1ZouMMagj2cTA6CHGOtqIg4/P3llK0igFw6qFT3y32GCLqCWTU4t2VfyF6eFs2njLoXlArhSVHHCeQ0QR5/9FQ==" - } - }, - "workspace": { - "packageJson": { - "dependencies": [ - "npm:@denotest/print-npm-user-agent@1.0.0" - ] - } - } -} diff --git a/tests/specs/npm/user_agent_env_var/main.ts b/tests/specs/npm/user_agent_env_var/main.ts new file mode 100644 index 00000000000000..2c51107e3c80ac --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/main.ts @@ -0,0 +1 @@ +console.log(`npm_config_user_agent: ${Deno.env.get("npm_config_user_agent")}`); diff --git a/tests/specs/npm/user_agent_env_var/npm_run.out b/tests/specs/npm/user_agent_env_var/npm_run.out deleted file mode 100644 index e3442fb48ed075..00000000000000 --- a/tests/specs/npm/user_agent_env_var/npm_run.out +++ /dev/null @@ -1,3 +0,0 @@ -Task run-via-other-task npm run run-via-bin -Task run-via-bin print-npm-user-agent -npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD] diff --git a/tests/specs/npm/user_agent_env_var/npx.out b/tests/specs/npm/user_agent_env_var/npx.out deleted file mode 100644 index b866f0aed3d91b..00000000000000 --- a/tests/specs/npm/user_agent_env_var/npx.out +++ /dev/null @@ -1,2 +0,0 @@ -Task run-with-npx npx print-npm-user-agent -npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD] diff --git a/tests/specs/npm/user_agent_env_var/package.json b/tests/specs/npm/user_agent_env_var/package.json index eeb327cd071456..12cc0cc6fb32c3 100644 --- a/tests/specs/npm/user_agent_env_var/package.json +++ b/tests/specs/npm/user_agent_env_var/package.json @@ -1,10 +1,8 @@ { "scripts": { - "run-with-npx": "npx print-npm-user-agent", - "run-via-other-task": "npm run run-via-bin", "run-via-bin": "print-npm-user-agent" }, "dependencies": { "@denotest/print-npm-user-agent": "1.0.0" } -} \ No newline at end of file +} diff --git a/tests/specs/npm/user_agent_env_var/postinstall.out b/tests/specs/npm/user_agent_env_var/postinstall.out new file mode 100644 index 00000000000000..086557cb3a1c5d --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/postinstall.out @@ -0,0 +1,10 @@ +Download http://localhost:4260/@denotest%2fprint-npm-user-agent +Download http://localhost:4260/@denotest/print-npm-user-agent/1.0.0.tgz +Initialize @denotest/print-npm-user-agent@1.0.0 +Initialize @denotest/print-npm-user-agent@1.0.0: running 'postinstall' script +error: script 'postinstall' in '@denotest/print-npm-user-agent@1.0.0' failed with exit code 1 +stdout: +postinstall +npm_config_user_agent: deno/[WILDCARD] npm/? deno/2.0.3 macos aarch64 + +error: failed to run scripts for packages: @denotest/print-npm-user-agent@1.0.0