From bc891d25b1415fc426d6ffc2566eae5c6a354f19 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Fri, 23 Feb 2024 12:36:08 +0530 Subject: [PATCH 1/3] fix(ext/node): set correct process.argv0 --- ext/node/lib.rs | 1 + ext/node/ops/os/mod.rs | 6 ++++++ ext/node/polyfills/process.ts | 15 +++++++-------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 98b927ba83624c..d99126b57a8454 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -297,6 +297,7 @@ deno_core::extension!(deno_node, ops::os::op_geteuid

, ops::os::op_cpus

, ops::os::op_process_abort, + ops::os::op_process_argv0, op_node_build_os, op_node_is_promise_rejected, op_npm_process_state, diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs index 1d3de797bbe711..c5a7011d4a963a 100644 --- a/ext/node/ops/os/mod.rs +++ b/ext/node/ops/os/mod.rs @@ -80,6 +80,12 @@ pub fn op_process_abort() { std::process::abort(); } +#[op2] +#[string] +pub fn op_process_argv0() -> String { + std::env::args().next().unwrap_or("deno".to_string()) +} + #[op2] #[serde] pub fn op_cpus

(state: &mut OpState) -> Result, AnyError> diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index a8da35f70d28fe..5a14877aca46bf 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -5,7 +5,12 @@ // deno-lint-ignore-file prefer-primordials import { core, internals } from "ext:core/mod.js"; -import { op_geteuid, op_process_abort, op_set_exit_code } from "ext:core/ops"; +import { + op_geteuid, + op_process_abort, + op_process_argv0, + op_set_exit_code, +} from "ext:core/ops"; import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts"; import { EventEmitter } from "node:events"; @@ -45,9 +50,6 @@ import { isWindows } from "ext:deno_node/_util/os.ts"; import * as io from "ext:deno_io/12_io.js"; import { Command } from "ext:runtime/40_process.js"; -let argv0Getter = () => ""; -export let argv0 = "deno"; - // TODO(kt3k): This should be set at start up time export let arch = ""; @@ -395,10 +397,7 @@ class Process extends EventEmitter { argv = argv; get argv0() { - if (!argv0) { - argv0 = argv0Getter(); - } - return argv0; + return op_process_argv0(); } set argv0(_val) {} From 6d8b1fc3212fb8d6b69bc6be2ca06ba92c3deedd Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Fri, 23 Feb 2024 12:36:50 +0530 Subject: [PATCH 2/3] update test to match exact value too --- tests/unit_node/process_test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/unit_node/process_test.ts b/tests/unit_node/process_test.ts index bff3c66418ccbe..42d37fecacd466 100644 --- a/tests/unit_node/process_test.ts +++ b/tests/unit_node/process_test.ts @@ -268,7 +268,17 @@ Deno.test({ Deno.test({ name: "process.argv0", - fn() { + async fn() { + const { stdout } = await new Deno.Command(Deno.execPath(), { + args: [ + "eval", + `import process from "node:process";console.log(process.argv0);`, + ], + stdout: "piped", + stderr: "null", + }).output(); + assertEquals(new TextDecoder().decode(stdout).trim(), Deno.execPath()); + assertEquals(typeof process.argv0, "string"); assert( process.argv0.match(/[^/\\]*deno[^/\\]*$/), From 0aeef4adc0bfb7d8a3da150d11addf16d433b64f Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Fri, 23 Feb 2024 13:21:39 +0530 Subject: [PATCH 3/3] fix: set argv0 during bootstrap --- cli/factory.rs | 5 +++-- cli/standalone/mod.rs | 9 ++++----- cli/worker.rs | 12 +++--------- ext/node/lib.rs | 1 - ext/node/ops/os/mod.rs | 6 ------ ext/node/polyfills/process.ts | 20 ++++++-------------- runtime/js/99_main.js | 8 ++++---- runtime/worker_bootstrap.rs | 8 ++++---- 8 files changed, 24 insertions(+), 45 deletions(-) diff --git a/cli/factory.rs b/cli/factory.rs index 9f3719510f37b9..22bc6cdaf091bb 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -855,9 +855,10 @@ impl CliFactory { location: self.options.location_flag().clone(), // if the user ran a binary command, we'll need to set process.argv[0] // to be the name of the binary command instead of deno - maybe_binary_npm_command_name: self + argv0: self .options - .take_binary_npm_command_name(), + .take_binary_npm_command_name() + .or(std::env::args().next()), origin_data_folder_path: Some(self.deno_dir()?.origin_data_folder_path()), seed: self.options.seed(), unsafely_ignore_certificate_errors: self diff --git a/cli/standalone/mod.rs b/cli/standalone/mod.rs index d1b90eceeb5079..312a1841dbe680 100644 --- a/cli/standalone/mod.rs +++ b/cli/standalone/mod.rs @@ -536,11 +536,10 @@ pub async fn run( is_npm_main: main_module.scheme() == "npm", skip_op_registration: true, location: metadata.location, - maybe_binary_npm_command_name: NpmPackageReqReference::from_specifier( - main_module, - ) - .ok() - .map(|req_ref| npm_pkg_req_ref_to_binary_command(&req_ref)), + argv0: NpmPackageReqReference::from_specifier(main_module) + .ok() + .map(|req_ref| npm_pkg_req_ref_to_binary_command(&req_ref)) + .or(std::env::args().next()), origin_data_folder_path: None, seed: metadata.seed, unsafely_ignore_certificate_errors: metadata diff --git a/cli/worker.rs b/cli/worker.rs index 5c252e92ce8ffd..302303f2869778 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -115,7 +115,7 @@ pub struct CliMainWorkerOptions { pub is_inspecting: bool, pub is_npm_main: bool, pub location: Option, - pub maybe_binary_npm_command_name: Option, + pub argv0: Option, pub origin_data_folder_path: Option, pub seed: Option, pub unsafely_ignore_certificate_errors: Option>, @@ -608,10 +608,7 @@ impl CliMainWorkerFactory { user_agent: version::get_user_agent().to_string(), inspect: shared.options.is_inspecting, has_node_modules_dir: shared.options.has_node_modules_dir, - maybe_binary_npm_command_name: shared - .options - .maybe_binary_npm_command_name - .clone(), + argv0: shared.options.argv0.clone(), node_ipc_fd: shared.node_ipc, disable_deprecated_api_warning: shared.disable_deprecated_api_warning, verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning, @@ -815,10 +812,7 @@ fn create_web_worker_callback( user_agent: version::get_user_agent().to_string(), inspect: shared.options.is_inspecting, has_node_modules_dir: shared.options.has_node_modules_dir, - maybe_binary_npm_command_name: shared - .options - .maybe_binary_npm_command_name - .clone(), + argv0: shared.options.argv0.clone(), node_ipc_fd: None, disable_deprecated_api_warning: shared.disable_deprecated_api_warning, verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning, diff --git a/ext/node/lib.rs b/ext/node/lib.rs index d99126b57a8454..98b927ba83624c 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -297,7 +297,6 @@ deno_core::extension!(deno_node, ops::os::op_geteuid

, ops::os::op_cpus

, ops::os::op_process_abort, - ops::os::op_process_argv0, op_node_build_os, op_node_is_promise_rejected, op_npm_process_state, diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs index c5a7011d4a963a..1d3de797bbe711 100644 --- a/ext/node/ops/os/mod.rs +++ b/ext/node/ops/os/mod.rs @@ -80,12 +80,6 @@ pub fn op_process_abort() { std::process::abort(); } -#[op2] -#[string] -pub fn op_process_argv0() -> String { - std::env::args().next().unwrap_or("deno".to_string()) -} - #[op2] #[serde] pub fn op_cpus

(state: &mut OpState) -> Result, AnyError> diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index 5a14877aca46bf..c378f15a585d4a 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -5,12 +5,7 @@ // deno-lint-ignore-file prefer-primordials import { core, internals } from "ext:core/mod.js"; -import { - op_geteuid, - op_process_abort, - op_process_argv0, - op_set_exit_code, -} from "ext:core/ops"; +import { op_geteuid, op_process_abort, op_set_exit_code } from "ext:core/ops"; import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts"; import { EventEmitter } from "node:events"; @@ -50,6 +45,8 @@ import { isWindows } from "ext:deno_node/_util/os.ts"; import * as io from "ext:deno_io/12_io.js"; import { Command } from "ext:runtime/40_process.js"; +export let argv0 = ""; + // TODO(kt3k): This should be set at start up time export let arch = ""; @@ -397,7 +394,7 @@ class Process extends EventEmitter { argv = argv; get argv0() { - return op_process_argv0(); + return argv0; } set argv0(_val) {} @@ -886,19 +883,14 @@ internals.__bootstrapNodeProcess = function ( ) { // Overwrites the 1st item with getter. if (typeof argv0Val === "string") { + argv0 = argv0Val; Object.defineProperty(argv, "0", { get: () => { return argv0Val; }, }); - argv0Getter = () => argv0Val; } else { - Object.defineProperty(argv, "0", { - get: () => { - return Deno.execPath(); - }, - }); - argv0Getter = () => Deno.execPath(); + Object.defineProperty(argv, "0", { get: () => argv0 }); } // Overwrites the 2st item with getter. diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 11c26798b00d08..21c74aeff13e39 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -644,7 +644,7 @@ function bootstrapMainRuntime(runtimeOptions) { 2: unstableFeatures, 3: inspectFlag, 5: hasNodeModulesDir, - 6: maybeBinaryNpmCommandName, + 6: argv0, 7: shouldDisableDeprecatedApiWarning, 8: shouldUseVerboseDeprecatedApiWarning, 9: future, @@ -768,7 +768,7 @@ function bootstrapMainRuntime(runtimeOptions) { ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs)); if (nodeBootstrap) { - nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName); + nodeBootstrap(hasNodeModulesDir, argv0); } if (future) { @@ -793,7 +793,7 @@ function bootstrapWorkerRuntime( 2: unstableFeatures, 4: enableTestingFeaturesFlag, 5: hasNodeModulesDir, - 6: maybeBinaryNpmCommandName, + 6: argv0, 7: shouldDisableDeprecatedApiWarning, 8: shouldUseVerboseDeprecatedApiWarning, } = runtimeOptions; @@ -897,7 +897,7 @@ function bootstrapWorkerRuntime( ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs)); if (nodeBootstrap) { - nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName); + nodeBootstrap(hasNodeModulesDir, argv0); } } diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index 300829630fbee0..c019dae1ac04df 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -58,7 +58,7 @@ pub struct BootstrapOptions { pub user_agent: String, pub inspect: bool, pub has_node_modules_dir: bool, - pub maybe_binary_npm_command_name: Option, + pub argv0: Option, pub node_ipc_fd: Option, pub disable_deprecated_api_warning: bool, pub verbose_deprecated_api_warning: bool, @@ -89,7 +89,7 @@ impl Default for BootstrapOptions { inspect: Default::default(), args: Default::default(), has_node_modules_dir: Default::default(), - maybe_binary_npm_command_name: None, + argv0: None, node_ipc_fd: None, disable_deprecated_api_warning: false, verbose_deprecated_api_warning: false, @@ -121,7 +121,7 @@ struct BootstrapV8<'a>( bool, // has_node_modules_dir bool, - // maybe_binary_npm_command_name + // argv0 Option<&'a str>, // disable_deprecated_api_warning, bool, @@ -147,7 +147,7 @@ impl BootstrapOptions { self.inspect, self.enable_testing_features, self.has_node_modules_dir, - self.maybe_binary_npm_command_name.as_deref(), + self.argv0.as_deref(), self.disable_deprecated_api_warning, self.verbose_deprecated_api_warning, self.future,