From b72f0be27c382f2d4e5d272ce9ed2211af584d80 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 15 Feb 2024 10:20:17 +0530 Subject: [PATCH] chore: add DENO_FUTURE env var (#22318) Closes https://github.com/denoland/deno/issues/22315 ``` ~> DENO_FUTURE=1 target/debug/deno > globalThis.window undefined ``` --- cli/args/mod.rs | 4 ++++ cli/factory.rs | 1 + cli/standalone/mod.rs | 1 + cli/worker.rs | 5 +++++ runtime/js/99_main.js | 5 +++++ runtime/worker_bootstrap.rs | 5 +++++ test_util/src/builders.rs | 5 +++++ tests/integration/run_tests.rs | 10 ++++++++++ tests/testdata/run/deno_futures_env.ts | 3 +++ 9 files changed, 39 insertions(+) create mode 100644 tests/testdata/run/deno_futures_env.ts diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 6aafd0ea60..d7ff91b849 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -987,6 +987,10 @@ impl CliOptions { } } + pub fn enable_future_features(&self) -> bool { + std::env::var("DENO_FUTURE").is_ok() + } + pub fn resolve_main_module(&self) -> Result { match &self.flags.subcommand { DenoSubcommand::Bundle(bundle_flags) => { diff --git a/cli/factory.rs b/cli/factory.rs index be2dc677d7..2236e60907 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -775,6 +775,7 @@ impl CliFactory { self.feature_checker().clone(), self.create_cli_main_worker_options()?, self.options.node_ipc_fd(), + self.options.enable_future_features(), // TODO(bartlomieju): temporarily disabled // self.options.disable_deprecated_api_warning, true, diff --git a/cli/standalone/mod.rs b/cli/standalone/mod.rs index ecbb0be823..d1b90eceeb 100644 --- a/cli/standalone/mod.rs +++ b/cli/standalone/mod.rs @@ -551,6 +551,7 @@ pub async fn run( create_coverage_collector: None, }, None, + false, // TODO(bartlomieju): temporarily disabled // metadata.disable_deprecated_api_warning, true, diff --git a/cli/worker.rs b/cli/worker.rs index 3f75ebc5c8..5c252e92ce 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -144,6 +144,7 @@ struct SharedWorkerState { maybe_lockfile: Option>>, feature_checker: Arc, node_ipc: Option, + enable_future_features: bool, disable_deprecated_api_warning: bool, verbose_deprecated_api_warning: bool, } @@ -424,6 +425,7 @@ impl CliMainWorkerFactory { feature_checker: Arc, options: CliMainWorkerOptions, node_ipc: Option, + enable_future_features: bool, disable_deprecated_api_warning: bool, verbose_deprecated_api_warning: bool, ) -> Self { @@ -446,6 +448,7 @@ impl CliMainWorkerFactory { maybe_lockfile, feature_checker, node_ipc, + enable_future_features, disable_deprecated_api_warning, verbose_deprecated_api_warning, }), @@ -612,6 +615,7 @@ impl CliMainWorkerFactory { node_ipc_fd: shared.node_ipc, disable_deprecated_api_warning: shared.disable_deprecated_api_warning, verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning, + future: shared.enable_future_features, }, extensions: custom_extensions, startup_snapshot: crate::js::deno_isolate_init(), @@ -818,6 +822,7 @@ fn create_web_worker_callback( node_ipc_fd: None, disable_deprecated_api_warning: shared.disable_deprecated_api_warning, verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning, + future: false, }, extensions: vec![], startup_snapshot: crate::js::deno_isolate_init(), diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 965af460d3..11c26798b0 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -647,6 +647,7 @@ function bootstrapMainRuntime(runtimeOptions) { 6: maybeBinaryNpmCommandName, 7: shouldDisableDeprecatedApiWarning, 8: shouldUseVerboseDeprecatedApiWarning, + 9: future, } = runtimeOptions; removeImportedOps(); @@ -769,6 +770,10 @@ function bootstrapMainRuntime(runtimeOptions) { if (nodeBootstrap) { nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName); } + + if (future) { + delete globalThis.window; + } } function bootstrapWorkerRuntime( diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index ca2d4d8ece..300829630f 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -62,6 +62,7 @@ pub struct BootstrapOptions { pub node_ipc_fd: Option, pub disable_deprecated_api_warning: bool, pub verbose_deprecated_api_warning: bool, + pub future: bool, } impl Default for BootstrapOptions { @@ -92,6 +93,7 @@ impl Default for BootstrapOptions { node_ipc_fd: None, disable_deprecated_api_warning: false, verbose_deprecated_api_warning: false, + future: false, } } } @@ -125,6 +127,8 @@ struct BootstrapV8<'a>( bool, // verbose_deprecated_api_warning bool, + // future + bool, ); impl BootstrapOptions { @@ -146,6 +150,7 @@ impl BootstrapOptions { self.maybe_binary_npm_command_name.as_deref(), self.disable_deprecated_api_warning, self.verbose_deprecated_api_warning, + self.future, ); bootstrap.serialize(ser).unwrap() diff --git a/test_util/src/builders.rs b/test_util/src/builders.rs index 4c655f1e52..9bbe6693f7 100644 --- a/test_util/src/builders.rs +++ b/test_util/src/builders.rs @@ -166,6 +166,11 @@ impl TestContextBuilder { self } + pub fn add_future_env_vars(mut self) -> Self { + self = self.env("DENO_FUTURE", "1"); + self + } + pub fn add_jsr_env_vars(mut self) -> Self { for (key, value) in env_vars_for_jsr_tests() { self = self.env(key, value); diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index c9508594c9..6f108f7397 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -1711,6 +1711,16 @@ fn type_directives_js_main() { assert_not_contains!(output.combined_output(), "type_reference.d.ts"); } +#[test] +fn test_deno_futures_env() { + let context = TestContextBuilder::new().add_future_env_vars().build(); + let output = context + .new_command() + .args("run --quiet --reload run/deno_futures_env.ts") + .run(); + output.assert_exit_code(0); +} + itest!(type_directives_redirect { args: "run --reload --check run/type_directives_redirect.ts", output: "run/type_directives_redirect.ts.out", diff --git a/tests/testdata/run/deno_futures_env.ts b/tests/testdata/run/deno_futures_env.ts new file mode 100644 index 0000000000..21f76e3675 --- /dev/null +++ b/tests/testdata/run/deno_futures_env.ts @@ -0,0 +1,3 @@ +if (typeof window !== "undefined") { + throw new Error("Window global available"); +}