From f0ec4fe1b89215ce9e62ebf47d8907dd6336f35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 7 Mar 2024 13:59:57 +0000 Subject: [PATCH] fix(publish): silence warnings for sloppy imports and node builtins with env var (#22760) An undocumented "DENO_DISABLE_PEDANTIC_NODE_WARNINGS" env var can be used to silence warnings for sloppy imports and node builtins without `node:` prefix. --- cli/args/mod.rs | 6 ++++ cli/graph_util.rs | 5 ++- cli/resolver.rs | 30 +++++++++------- tests/integration/publish_tests.rs | 34 +++++++++++++++++++ .../bare_node_builtins_no_warnings.out | 9 +++++ .../publish/sloppy_imports_no_warnings.out | 8 +++++ 6 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 tests/testdata/publish/bare_node_builtins_no_warnings.out create mode 100644 tests/testdata/publish/sloppy_imports_no_warnings.out diff --git a/cli/args/mod.rs b/cli/args/mod.rs index af681104ce..acdc965263 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -101,6 +101,12 @@ pub fn npm_registry_url() -> &'static Url { &NPM_REGISTRY_DEFAULT_URL } +pub static DENO_DISABLE_PEDANTIC_NODE_WARNINGS: Lazy = Lazy::new(|| { + std::env::var("DENO_DISABLE_PEDANTIC_NODE_WARNINGS") + .ok() + .is_some() +}); + pub fn jsr_url() -> &'static Url { static JSR_URL: Lazy = Lazy::new(|| { let env_var_name = "JSR_URL"; diff --git a/cli/graph_util.rs b/cli/graph_util.rs index f1aa443288..b84cb3bb64 100644 --- a/cli/graph_util.rs +++ b/cli/graph_util.rs @@ -4,6 +4,7 @@ use crate::args::jsr_url; use crate::args::CliOptions; use crate::args::Lockfile; use crate::args::TsTypeLib; +use crate::args::DENO_DISABLE_PEDANTIC_NODE_WARNINGS; use crate::cache; use crate::cache::GlobalHttpCache; use crate::cache::ModuleInfoCache; @@ -681,9 +682,11 @@ pub fn enhanced_resolution_error_message(error: &ResolutionError) -> String { let mut message = format!("{error}"); if let Some(specifier) = get_resolution_error_bare_node_specifier(error) { - message.push_str(&format!( + if !*DENO_DISABLE_PEDANTIC_NODE_WARNINGS { + message.push_str(&format!( "\nIf you want to use a built-in Node module, add a \"node:\" prefix (ex. \"node:{specifier}\")." )); + } } message diff --git a/cli/resolver.rs b/cli/resolver.rs index e1a2145d3b..8043e1ede1 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -41,6 +41,7 @@ use std::sync::Arc; use crate::args::package_json::PackageJsonDeps; use crate::args::JsxImportSourceConfig; use crate::args::PackageJsonDepsProvider; +use crate::args::DENO_DISABLE_PEDANTIC_NODE_WARNINGS; use crate::colors; use crate::node::CliNodeCodeTranslator; use crate::npm::ByonmCliNpmResolver; @@ -725,17 +726,20 @@ fn sloppy_imports_resolve( }; // show a warning when this happens in order to drive // the user towards correcting these specifiers - log::warn!( - "{} Sloppy module resolution {}\n at {}", - crate::colors::yellow("Warning"), - crate::colors::gray(format!("(hint: {})", hint_message)).to_string(), - if referrer_range.end == deno_graph::Position::zeroed() { - // not worth showing the range in this case - crate::colors::cyan(referrer_range.specifier.as_str()).to_string() - } else { - format_range_with_colors(referrer_range) - }, - ); + if !*DENO_DISABLE_PEDANTIC_NODE_WARNINGS { + log::warn!( + "{} Sloppy module resolution {}\n at {}", + crate::colors::yellow("Warning"), + crate::colors::gray(format!("(hint: {})", hint_message)).to_string(), + if referrer_range.end == deno_graph::Position::zeroed() { + // not worth showing the range in this case + crate::colors::cyan(referrer_range.specifier.as_str()).to_string() + } else { + format_range_with_colors(referrer_range) + }, + ); + } + resolution.into_specifier().into_owned() } @@ -788,7 +792,9 @@ impl NpmResolver for CliGraphResolver { } = range; let line = start.line + 1; let column = start.character + 1; - log::warn!("Warning: Resolving \"{module_name}\" as \"node:{module_name}\" at {specifier}:{line}:{column}. If you want to use a built-in Node module, add a \"node:\" prefix.") + if !*DENO_DISABLE_PEDANTIC_NODE_WARNINGS { + log::warn!("Warning: Resolving \"{module_name}\" as \"node:{module_name}\" at {specifier}:{line}:{column}. If you want to use a built-in Node module, add a \"node:\" prefix.") + } } fn load_and_cache_npm_package_info( diff --git a/tests/integration/publish_tests.rs b/tests/integration/publish_tests.rs index ec53455534..ce93ccda3f 100644 --- a/tests/integration/publish_tests.rs +++ b/tests/integration/publish_tests.rs @@ -250,6 +250,23 @@ itest!(bare_node_builtins { http_server: true, }); +itest!(bare_node_builtins_warning_no_warnings { + args: "publish --token 'sadfasdf' --dry-run --unstable-bare-node-builtins", + output: "publish/bare_node_builtins_no_warnings.out", + cwd: Some("publish/bare_node_builtins"), + envs: env_vars_for_jsr_npm_tests() + .into_iter() + .chain( + vec![( + "DENO_DISABLE_PEDANTIC_NODE_WARNINGS".to_string(), + "1".to_string() + )] + .into_iter() + ) + .collect(), + http_server: true, +}); + itest!(sloppy_imports { args: "publish --token 'sadfasdf' --dry-run --unstable-sloppy-imports", output: "publish/sloppy_imports.out", @@ -258,6 +275,23 @@ itest!(sloppy_imports { http_server: true, }); +itest!(sloppy_imports_no_warnings { + args: "publish --token 'sadfasdf' --dry-run --unstable-sloppy-imports", + output: "publish/sloppy_imports_no_warnings.out", + cwd: Some("publish/sloppy_imports"), + envs: env_vars_for_jsr_tests() + .into_iter() + .chain( + vec![( + "DENO_DISABLE_PEDANTIC_NODE_WARNINGS".to_string(), + "1".to_string() + )] + .into_iter() + ) + .collect(), + http_server: true, +}); + itest!(jsr_jsonc { args: "publish --token 'sadfasdf'", cwd: Some("publish/jsr_jsonc"), diff --git a/tests/testdata/publish/bare_node_builtins_no_warnings.out b/tests/testdata/publish/bare_node_builtins_no_warnings.out new file mode 100644 index 0000000000..c45c357cec --- /dev/null +++ b/tests/testdata/publish/bare_node_builtins_no_warnings.out @@ -0,0 +1,9 @@ +Download http://localhost:4545/npm/registry/@types/node +Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz +Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts +Checking for slow types in the public API... +Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts +Simulating publish of @foo/bar@1.0.0 with files: + file:///[WILDCARD]/publish/bare_node_builtins/deno.json (87B) + file:///[WILDCARD]/publish/bare_node_builtins/mod.ts (121B) +Warning Aborting due to --dry-run diff --git a/tests/testdata/publish/sloppy_imports_no_warnings.out b/tests/testdata/publish/sloppy_imports_no_warnings.out new file mode 100644 index 0000000000..8659010b7c --- /dev/null +++ b/tests/testdata/publish/sloppy_imports_no_warnings.out @@ -0,0 +1,8 @@ +Check file:///[WILDCARD]/publish/sloppy_imports/mod.ts +Checking for slow types in the public API... +Check file:///[WILDCARD]/publish/sloppy_imports/mod.ts +Simulating publish of @foo/bar@1.0.0 with files: + file:///[WILDCARD]/publish/sloppy_imports/b/index.ts (27B) + file:///[WILDCARD]/publish/sloppy_imports/deno.json (87B) + file:///[WILDCARD]/publish/sloppy_imports/mod.ts (35B) +Warning Aborting due to --dry-run