From b2628e4a069ed9fc13f34f6e4fd75f29c657e5a9 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Sun, 5 May 2024 19:46:02 +0530 Subject: [PATCH] fix(ext/node): don't rely on Deno.env to read NODE_DEBUG (#23694) This patch allows implementors to use ext/node without the need to implement Deno.env API. Closes https://github.com/denoland/deno/issues/23687 --- cli/factory.rs | 1 + cli/standalone/mod.rs | 1 + cli/worker.rs | 3 +++ ext/node/polyfills/02_init.js | 20 ++++++++++-------- runtime/js/99_main.js | 40 +++++++++++++++++++++-------------- runtime/worker_bootstrap.rs | 5 +++++ 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/cli/factory.rs b/cli/factory.rs index bfaf96f39cdfe..a9f6bf87e9034 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -885,6 +885,7 @@ impl CliFactory { .options .take_binary_npm_command_name() .or(std::env::args().next()), + node_debug: std::env::var("NODE_DEBUG").ok(), 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 3cfeb4f4ce58c..47b035fc76c98 100644 --- a/cli/standalone/mod.rs +++ b/cli/standalone/mod.rs @@ -565,6 +565,7 @@ pub async fn run( .ok() .map(|req_ref| npm_pkg_req_ref_to_binary_command(&req_ref)) .or(std::env::args().next()), + node_debug: std::env::var("NODE_DEBUG").ok(), origin_data_folder_path: None, seed: metadata.seed, unsafely_ignore_certificate_errors: metadata diff --git a/cli/worker.rs b/cli/worker.rs index edc4ef907a481..302c00e1053d3 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -112,6 +112,7 @@ pub struct CliMainWorkerOptions { pub is_npm_main: bool, pub location: Option, pub argv0: Option, + pub node_debug: Option, pub origin_data_folder_path: Option, pub seed: Option, pub unsafely_ignore_certificate_errors: Option>, @@ -607,6 +608,7 @@ impl CliMainWorkerFactory { inspect: shared.options.is_inspecting, has_node_modules_dir: shared.options.has_node_modules_dir, argv0: shared.options.argv0.clone(), + node_debug: shared.options.node_debug.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, @@ -816,6 +818,7 @@ fn create_web_worker_callback( inspect: shared.options.is_inspecting, has_node_modules_dir: shared.options.has_node_modules_dir, argv0: shared.options.argv0.clone(), + node_debug: shared.options.node_debug.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/polyfills/02_init.js b/ext/node/polyfills/02_init.js index 71eebbc81acc0..7a697abe4908d 100644 --- a/ext/node/polyfills/02_init.js +++ b/ext/node/polyfills/02_init.js @@ -10,14 +10,16 @@ import "node:module"; let initialized = false; -function initialize( - usesLocalNodeModulesDir, - argv0, - runningOnMainThread, - workerId, - maybeWorkerMetadata, - warmup = false, -) { +function initialize(args) { + const { + usesLocalNodeModulesDir, + argv0, + runningOnMainThread, + workerId, + maybeWorkerMetadata, + nodeDebug, + warmup = false, + } = args; if (!warmup) { if (initialized) { throw Error("Node runtime already initialized"); @@ -33,7 +35,7 @@ function initialize( argv0, Deno.args, Deno.version, - Deno.env.get("NODE_DEBUG") ?? "", + nodeDebug ?? "", ); internals.__initWorkerThreads( runningOnMainThread, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 59d2a1434da25..2ea122e341ab2 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -706,12 +706,13 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { 3: inspectFlag, 5: hasNodeModulesDir, 6: argv0, - 7: shouldDisableDeprecatedApiWarning, - 8: shouldUseVerboseDeprecatedApiWarning, - 9: future, - 10: mode, - 11: servePort, - 12: serveHost, + 7: nodeDebug, + 8: shouldDisableDeprecatedApiWarning, + 9: shouldUseVerboseDeprecatedApiWarning, + 10: future, + 11: mode, + 12: servePort, + 13: serveHost, } = runtimeOptions; if (mode === executionModes.run || mode === executionModes.serve) { @@ -859,7 +860,12 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs)); if (nodeBootstrap) { - nodeBootstrap(hasNodeModulesDir, argv0, /* runningOnMainThread */ true); + nodeBootstrap({ + usesLocalNodeModulesDir: hasNodeModulesDir, + runningOnMainThread: true, + argv0, + nodeDebug, + }); } if (future) { delete globalThis.window; @@ -917,9 +923,10 @@ function bootstrapWorkerRuntime( 4: enableTestingFeaturesFlag, 5: hasNodeModulesDir, 6: argv0, - 7: shouldDisableDeprecatedApiWarning, - 8: shouldUseVerboseDeprecatedApiWarning, - 9: future, + 7: nodeDebug, + 8: shouldDisableDeprecatedApiWarning, + 9: shouldUseVerboseDeprecatedApiWarning, + 10: future, } = runtimeOptions; // TODO(iuioiua): remove in Deno v2. This allows us to dynamically delete @@ -1016,13 +1023,14 @@ function bootstrapWorkerRuntime( : undefined; if (nodeBootstrap) { - nodeBootstrap( - hasNodeModulesDir, + nodeBootstrap({ + usesLocalNodeModulesDir: hasNodeModulesDir, + runningOnMainThread: false, argv0, - /* runningOnMainThread */ false, workerId, - workerMetadata, - ); + maybeWorkerMetadata: workerMetadata, + nodeDebug, + }); } if (future) { @@ -1097,4 +1105,4 @@ bootstrapWorkerRuntime( undefined, true, ); -nodeBootstrap(undefined, undefined, undefined, undefined, undefined, true); +nodeBootstrap({ warmup: true }); diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index ee3b81c628a0e..31cb883dbe5c8 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -85,6 +85,7 @@ pub struct BootstrapOptions { pub inspect: bool, pub has_node_modules_dir: bool, pub argv0: Option, + pub node_debug: Option, pub node_ipc_fd: Option, pub disable_deprecated_api_warning: bool, pub verbose_deprecated_api_warning: bool, @@ -120,6 +121,7 @@ impl Default for BootstrapOptions { args: Default::default(), has_node_modules_dir: Default::default(), argv0: None, + node_debug: None, node_ipc_fd: None, disable_deprecated_api_warning: false, verbose_deprecated_api_warning: false, @@ -156,6 +158,8 @@ struct BootstrapV8<'a>( bool, // argv0 Option<&'a str>, + // node_debug + Option<&'a str>, // disable_deprecated_api_warning, bool, // verbose_deprecated_api_warning @@ -187,6 +191,7 @@ impl BootstrapOptions { self.enable_testing_features, self.has_node_modules_dir, self.argv0.as_deref(), + self.node_debug.as_deref(), self.disable_deprecated_api_warning, self.verbose_deprecated_api_warning, self.future,