From 0a75502bd48bd849e064d100a0d4c39b8959b012 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Tue, 29 Oct 2024 20:58:06 -0700 Subject: [PATCH] cleanup + test --- cli/task_runner.rs | 7 +-- cli/tools/run/mod.rs | 18 +++++++ cli/worker.rs | 4 -- .../print-npm-user-agent/1.0.0/index.js | 2 + .../print-npm-user-agent/1.0.0/package.json | 7 +++ .../npm/user_agent_env_var/__test__.jsonc | 51 +++++++++++++++++++ .../npm/user_agent_env_var/bin_command.out | 1 + tests/specs/npm/user_agent_env_var/deno.jsonc | 3 ++ tests/specs/npm/user_agent_env_var/deno.lock | 18 +++++++ .../specs/npm/user_agent_env_var/npm_run.out | 3 ++ tests/specs/npm/user_agent_env_var/npx.out | 2 + .../specs/npm/user_agent_env_var/package.json | 10 ++++ tests/specs/npm/user_agent_env_var/run.out | 1 + tests/specs/npm/user_agent_env_var/test.mjs | 1 + 14 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/index.js create mode 100644 tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json create mode 100644 tests/specs/npm/user_agent_env_var/__test__.jsonc create mode 100644 tests/specs/npm/user_agent_env_var/bin_command.out create mode 100644 tests/specs/npm/user_agent_env_var/deno.jsonc create mode 100644 tests/specs/npm/user_agent_env_var/deno.lock create mode 100644 tests/specs/npm/user_agent_env_var/npm_run.out create mode 100644 tests/specs/npm/user_agent_env_var/npx.out create mode 100644 tests/specs/npm/user_agent_env_var/package.json create mode 100644 tests/specs/npm/user_agent_env_var/run.out create mode 100644 tests/specs/npm/user_agent_env_var/test.mjs diff --git a/cli/task_runner.rs b/cli/task_runner.rs index 6be763e846fe8a..95e8409143fe79 100644 --- a/cli/task_runner.rs +++ b/cli/task_runner.rs @@ -204,7 +204,7 @@ impl ShellCommand for NpmCommand { mut context: ShellCommandContext, ) -> LocalBoxFuture<'static, ExecuteResult> { if context.args.first().map(|s| s.as_str()) == Some("run") - && context.args.len() > 2 + && context.args.len() >= 2 // for now, don't run any npm scripts that have a flag because // we don't handle stuff like `--workspaces` properly && !context.args.iter().any(|s| s.starts_with('-')) @@ -375,11 +375,6 @@ impl ShellCommand for NpmPackageBinCommand { }, ]; - context.state.apply_env_var( - "npm_config_user_agent", - &crate::npm::get_npm_config_user_agent(), - ); - args.extend(context.args); let executable_command = deno_task_shell::ExecutableCommand::new( "deno".to_string(), diff --git a/cli/tools/run/mod.rs b/cli/tools/run/mod.rs index 152e2650bd7a69..1cf3141986dea7 100644 --- a/cli/tools/run/mod.rs +++ b/cli/tools/run/mod.rs @@ -30,6 +30,16 @@ To grant permissions, set them before the script argument. For example: } } +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::get_npm_config_user_agent(), + ); + }); +} + pub async fn run_script( mode: WorkerExecutionMode, flags: Arc, @@ -58,6 +68,10 @@ pub async fn run_script( let main_module = cli_options.resolve_main_module()?; + if main_module.scheme() == "npm" { + set_npm_user_agent(); + } + maybe_npm_install(&factory).await?; let worker_factory = factory.create_cli_main_worker_factory().await?; @@ -119,6 +133,10 @@ async fn run_with_watch( let cli_options = factory.cli_options()?; let main_module = cli_options.resolve_main_module()?; + if main_module.scheme() == "npm" { + set_npm_user_agent(); + } + maybe_npm_install(&factory).await?; let _ = watcher_communicator.watch_paths(cli_options.watch_paths()); diff --git a/cli/worker.rs b/cli/worker.rs index 74139e895db964..e230197d2b56b5 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -491,10 +491,6 @@ impl CliMainWorkerFactory { custom_extensions: Vec, stdio: deno_runtime::deno_io::Stdio, ) -> Result { - std::env::set_var( - "npm_config_user_agent", - crate::npm::get_npm_config_user_agent(), - ); let shared = &self.shared; let ModuleLoaderAndSourceMapGetter { module_loader } = shared .module_loader_factory diff --git a/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/index.js b/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/index.js new file mode 100644 index 00000000000000..b835b8e25eeaa5 --- /dev/null +++ b/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/index.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log(`npm_config_user_agent: ${process.env["npm_config_user_agent"]}`); \ No newline at end of file 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 new file mode 100644 index 00000000000000..1b4c39ebf3622f --- /dev/null +++ b/tests/registry/npm/@denotest/print-npm-user-agent/1.0.0/package.json @@ -0,0 +1,7 @@ +{ + "name": "@denotest/print-npm-user-agent", + "version": "1.0.0", + "bin": { + "print-npm-user-agent": "index.js" + } +} \ 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 new file mode 100644 index 00000000000000..dffe16d75f3feb --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/__test__.jsonc @@ -0,0 +1,51 @@ +{ + "tempDir": true, + "tests": { + "run_npm_package": { + "steps": [ + { + "args": "install", + "output": "[WILDCARD]" + }, + { + "args": "run -A npm:@denotest/print-npm-user-agent", + "output": "run.out" + } + ] + }, + "npx": { + "steps": [ + { + "args": "install", + "output": "[WILDCARD]" + }, + { + "if": "unix", + "args": "task run-with-npx", + "output": "npx.out" + } + ] + }, + "bin_command": { + "steps": [ + { "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" + } + ] + }, + "npm_run": { + "steps": [ + { + "args": "install", + "output": "[WILDCARD]" + }, + { + "args": "task run-via-other-task", + "output": "npm_run.out" + } + ] + } + } +} diff --git a/tests/specs/npm/user_agent_env_var/bin_command.out b/tests/specs/npm/user_agent_env_var/bin_command.out new file mode 100644 index 00000000000000..a630ac412f2c0e --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/bin_command.out @@ -0,0 +1 @@ +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 new file mode 100644 index 00000000000000..28a9877e69274c --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/deno.jsonc @@ -0,0 +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 new file mode 100644 index 00000000000000..3d1bdc4cda729e --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/deno.lock @@ -0,0 +1,18 @@ +{ + "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/npm_run.out b/tests/specs/npm/user_agent_env_var/npm_run.out new file mode 100644 index 00000000000000..e3442fb48ed075 --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/npm_run.out @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000000000..b866f0aed3d91b --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/npx.out @@ -0,0 +1,2 @@ +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 new file mode 100644 index 00000000000000..eeb327cd071456 --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/package.json @@ -0,0 +1,10 @@ +{ + "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/run.out b/tests/specs/npm/user_agent_env_var/run.out new file mode 100644 index 00000000000000..a630ac412f2c0e --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/run.out @@ -0,0 +1 @@ +npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD] diff --git a/tests/specs/npm/user_agent_env_var/test.mjs b/tests/specs/npm/user_agent_env_var/test.mjs new file mode 100644 index 00000000000000..ae035d40ef9b07 --- /dev/null +++ b/tests/specs/npm/user_agent_env_var/test.mjs @@ -0,0 +1 @@ +console.log(process.env.npm_config_user_agent);