From 4f530c443a8982c75b30c8e43ee11f6f9a34d57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 1 Oct 2021 17:42:26 +0200 Subject: [PATCH 01/13] feat: add --node-compat flag to provide built-in Node modules --- cli/flags.rs | 32 ++++++++++++++++++++++++++++++++ cli/main.rs | 1 + cli/node_compat.rs | 41 +++++++++++++++++++++++++++++++++++++++++ cli/proc_state.rs | 22 +++++++++++++++++++++- cli/tools/standalone.rs | 1 + 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 cli/node_compat.rs diff --git a/cli/flags.rs b/cli/flags.rs index aaff45388ba866..832271d7368aad 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -222,6 +222,9 @@ pub struct Flags { pub log_level: Option, pub no_check: bool, pub no_remote: bool, + /// If true, a list of Node built-in modules will be injected into + /// the import map. + pub node_compat: bool, pub prompt: bool, pub reload: bool, pub repl: bool, @@ -1490,6 +1493,7 @@ fn runtime_args<'a, 'b>( .arg(v8_flags_arg()) .arg(seed_arg()) .arg(enable_testing_features_arg()) + .arg(node_compat_arg()) } fn inspect_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { @@ -1619,6 +1623,12 @@ fn seed_arg<'a, 'b>() -> Arg<'a, 'b> { }) } +fn node_compat_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name("node-compat") + .long("node-compat") + .help("Provide shim that allows to use built-in Node modules") +} + fn watch_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("watch") .long("watch") @@ -2228,6 +2238,7 @@ fn runtime_args_parse( location_arg_parse(flags, matches); v8_flags_arg_parse(flags, matches); seed_arg_parse(flags, matches); + node_compat_arg_parse(flags, matches); inspect_arg_parse(flags, matches); enable_testing_features_arg_parse(flags, matches); } @@ -2313,6 +2324,12 @@ fn seed_arg_parse(flags: &mut Flags, matches: &ArgMatches) { } } +fn node_compat_arg_parse(flags: &mut Flags, matches: &ArgMatches) { + if matches.is_present("node-compat") { + flags.node_compat = true; + } +} + fn no_check_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { if matches.is_present("no-check") { flags.no_check = true; @@ -4431,4 +4448,19 @@ mod tests { .to_string() .contains("Expected protocol \"http\" or \"https\"")); } + + #[test] + fn node_compat() { + let r = flags_from_vec(svec!["deno", "run", "--node-compat", "foo.js"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Run(RunFlags { + script: "foo.js".to_string(), + }), + node_compat: true, + ..Flags::default() + } + ); + } } diff --git a/cli/main.rs b/cli/main.rs index 9176aca6f0b0bb..59ad77e42dd208 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -23,6 +23,7 @@ mod logger; mod lsp; mod module_graph; mod module_loader; +mod node_compat; mod ops; mod proc_state; mod source_maps; diff --git a/cli/node_compat.rs b/cli/node_compat.rs new file mode 100644 index 00000000000000..6295791f3870f0 --- /dev/null +++ b/cli/node_compat.rs @@ -0,0 +1,41 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +use std::collections::HashMap; + +// TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? +static DENO_STD_URL: &'static str = "https://deno.land/std/node/"; + +static SUPPORTED_MODULES: &[&'static str] = &[ + "assert", + "buffer", + "child_process", + "console", + "constants", + "crypto", + "events", + "fs", + "module", + "os", + "path", + "perf_hooks", + "process", + "querystring", + "stream", + "string_decoder", + "sys", + "timers", + "tty", + "url", + "util", +]; + +pub fn get_mapped_node_builtins() -> HashMap { + let mut mappings = HashMap::new(); + + for module in SUPPORTED_MODULES { + let module_url = format!("{}{}", DENO_STD_URL, module); + mappings.insert(module.to_string(), module_url); + } + + mappings +} diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 2c61ba51f60b0e..6468a0201aef92 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -182,7 +182,7 @@ impl ProcState { None }; - let maybe_import_map: Option = + let mut maybe_import_map: Option = match flags.import_map_path.as_ref() { None => None, Some(import_map_url) => { @@ -204,6 +204,26 @@ impl ProcState { } }; + if flags.node_compat { + let mut import_map = match maybe_import_map { + Some(import_map) => import_map, + None => { + // INFO: we're creating an empty import map, with its specifier pointing + // to `CWD/node_import_map.json` to make sure the map still works as expected. + let import_map_specifier = + std::env::current_dir()?.join("node_import_map.json"); + ImportMap::from_json(import_map_specifier.to_str().unwrap(), "{}") + .unwrap() + } + }; + let _node_builtins = crate::node_compat::get_mapped_node_builtins(); + // TODO(bartlomieju): should this actually be a hard error if user provided partial + // mapping for Node modules in their own import map, or should we just print out + // that some modules were not shimmed because they were already present in import map? + // import_map.update_in_place(node_builtins)?; + maybe_import_map = Some(import_map); + } + let maybe_inspect_host = flags.inspect.or(flags.inspect_brk); let maybe_inspector_server = maybe_inspect_host.map(|host| { Arc::new(InspectorServer::new(host, version::get_user_agent())) diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 33bc7881ed588f..848541f2f6443d 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -227,6 +227,7 @@ pub fn compile_to_runtime_flags( lock: None, log_level: flags.log_level, no_check: false, + node_compat: flags.node_compat, unsafely_ignore_certificate_errors: flags .unsafely_ignore_certificate_errors, no_remote: false, From ac5bc7569271ef873871540ca63914b222f57cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 00:22:43 +0200 Subject: [PATCH 02/13] get some mappings working --- Cargo.lock | 3 +-- cli/Cargo.toml | 2 +- cli/node_compat.rs | 2 +- cli/proc_state.rs | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8284da3c780a7..10c81e06c702db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1721,8 +1721,7 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" [[package]] name = "import_map" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae79a7af71e92e4038963b64884106d28d508da6bf3c7f36294efcb3bb3b003d" +source = "git+https://github.com/denoland/import_map?branch=update_in_place#baee1f6575e9b0049ca5053f723b5874857f827a" dependencies = [ "indexmap", "log", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index cdc3564526eac4..31f8755dcb70cd 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -59,7 +59,7 @@ encoding_rs = "0.8.28" env_logger = "0.8.4" fancy-regex = "0.7.1" http = "0.2.4" -import_map = "0.3.0" +import_map = { version = "0.3.0", git = "https://github.com/denoland/import_map", branch = "update_in_place" } jsonc-parser = { version = "0.17.0", features = ["serde"] } lazy_static = "1.4.0" libc = "0.2.101" diff --git a/cli/node_compat.rs b/cli/node_compat.rs index 6295791f3870f0..d4f53c0690b4e3 100644 --- a/cli/node_compat.rs +++ b/cli/node_compat.rs @@ -33,7 +33,7 @@ pub fn get_mapped_node_builtins() -> HashMap { let mut mappings = HashMap::new(); for module in SUPPORTED_MODULES { - let module_url = format!("{}{}", DENO_STD_URL, module); + let module_url = format!("{}{}.ts", DENO_STD_URL, module); mappings.insert(module.to_string(), module_url); } diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 6468a0201aef92..78b727e31f5e9d 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -216,11 +216,11 @@ impl ProcState { .unwrap() } }; - let _node_builtins = crate::node_compat::get_mapped_node_builtins(); + let node_builtins = crate::node_compat::get_mapped_node_builtins(); // TODO(bartlomieju): should this actually be a hard error if user provided partial // mapping for Node modules in their own import map, or should we just print out // that some modules were not shimmed because they were already present in import map? - // import_map.update_in_place(node_builtins)?; + import_map.update_imports(node_builtins)?; maybe_import_map = Some(import_map); } From 2ff2fd5ef6ba4f5556b37faafe79b6ebb6c37394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 16:00:14 +0200 Subject: [PATCH 03/13] --node-compat -> --compat --- cli/flags.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/flags.rs b/cli/flags.rs index 832271d7368aad..fc2a5d77c270b0 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -1624,8 +1624,8 @@ fn seed_arg<'a, 'b>() -> Arg<'a, 'b> { } fn node_compat_arg<'a, 'b>() -> Arg<'a, 'b> { - Arg::with_name("node-compat") - .long("node-compat") + Arg::with_name("compat") + .long("compat") .help("Provide shim that allows to use built-in Node modules") } @@ -2325,7 +2325,7 @@ fn seed_arg_parse(flags: &mut Flags, matches: &ArgMatches) { } fn node_compat_arg_parse(flags: &mut Flags, matches: &ArgMatches) { - if matches.is_present("node-compat") { + if matches.is_present("compat") { flags.node_compat = true; } } @@ -4451,7 +4451,7 @@ mod tests { #[test] fn node_compat() { - let r = flags_from_vec(svec!["deno", "run", "--node-compat", "foo.js"]); + let r = flags_from_vec(svec!["deno", "run", "--compat", "foo.js"]); assert_eq!( r.unwrap(), Flags { From 60e6949cb7cb7e113270144816c6799868c062ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 16:07:27 +0200 Subject: [PATCH 04/13] lint --- Cargo.lock | 5 +++-- cli/Cargo.toml | 2 +- cli/node_compat.rs | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10c81e06c702db..2bc48c16585cbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1720,8 +1720,9 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" [[package]] name = "import_map" -version = "0.3.0" -source = "git+https://github.com/denoland/import_map?branch=update_in_place#baee1f6575e9b0049ca5053f723b5874857f827a" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14541d9b0d1dfc66d5a71d07d1911114dd6532e5fc1e1ef66006eb5f0435fd97" dependencies = [ "indexmap", "log", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 31f8755dcb70cd..5d84087347e5e9 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -59,7 +59,7 @@ encoding_rs = "0.8.28" env_logger = "0.8.4" fancy-regex = "0.7.1" http = "0.2.4" -import_map = { version = "0.3.0", git = "https://github.com/denoland/import_map", branch = "update_in_place" } +import_map = "0.3.1" jsonc-parser = { version = "0.17.0", features = ["serde"] } lazy_static = "1.4.0" libc = "0.2.101" diff --git a/cli/node_compat.rs b/cli/node_compat.rs index d4f53c0690b4e3..b0fe8f5e81b00d 100644 --- a/cli/node_compat.rs +++ b/cli/node_compat.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; // TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? -static DENO_STD_URL: &'static str = "https://deno.land/std/node/"; +static DENO_STD_URL: &str = "https://deno.land/std/node/"; -static SUPPORTED_MODULES: &[&'static str] = &[ +static SUPPORTED_MODULES: &[&str] = &[ "assert", "buffer", "child_process", From 2e55a29e4664b3357484101ac3e00faaa01b444e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 2 Oct 2021 12:56:48 -0400 Subject: [PATCH 05/13] s/node_compat/compat --- cli/{node_compat.rs => compat.rs} | 0 cli/flags.rs | 18 +++++++++--------- cli/main.rs | 2 +- cli/proc_state.rs | 4 ++-- cli/tools/standalone.rs | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) rename cli/{node_compat.rs => compat.rs} (100%) diff --git a/cli/node_compat.rs b/cli/compat.rs similarity index 100% rename from cli/node_compat.rs rename to cli/compat.rs diff --git a/cli/flags.rs b/cli/flags.rs index fc2a5d77c270b0..dc0e932eff46f8 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -224,7 +224,7 @@ pub struct Flags { pub no_remote: bool, /// If true, a list of Node built-in modules will be injected into /// the import map. - pub node_compat: bool, + pub compat: bool, pub prompt: bool, pub reload: bool, pub repl: bool, @@ -1493,7 +1493,7 @@ fn runtime_args<'a, 'b>( .arg(v8_flags_arg()) .arg(seed_arg()) .arg(enable_testing_features_arg()) - .arg(node_compat_arg()) + .arg(compat_arg()) } fn inspect_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { @@ -1623,10 +1623,10 @@ fn seed_arg<'a, 'b>() -> Arg<'a, 'b> { }) } -fn node_compat_arg<'a, 'b>() -> Arg<'a, 'b> { +fn compat_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("compat") .long("compat") - .help("Provide shim that allows to use built-in Node modules") + .help("Node compatibility mode. Currently only enables built-in node modules like 'fs'.") } fn watch_arg<'a, 'b>() -> Arg<'a, 'b> { @@ -2238,7 +2238,7 @@ fn runtime_args_parse( location_arg_parse(flags, matches); v8_flags_arg_parse(flags, matches); seed_arg_parse(flags, matches); - node_compat_arg_parse(flags, matches); + compat_arg_parse(flags, matches); inspect_arg_parse(flags, matches); enable_testing_features_arg_parse(flags, matches); } @@ -2324,9 +2324,9 @@ fn seed_arg_parse(flags: &mut Flags, matches: &ArgMatches) { } } -fn node_compat_arg_parse(flags: &mut Flags, matches: &ArgMatches) { +fn compat_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if matches.is_present("compat") { - flags.node_compat = true; + flags.compat = true; } } @@ -4450,7 +4450,7 @@ mod tests { } #[test] - fn node_compat() { + fn compat() { let r = flags_from_vec(svec!["deno", "run", "--compat", "foo.js"]); assert_eq!( r.unwrap(), @@ -4458,7 +4458,7 @@ mod tests { subcommand: DenoSubcommand::Run(RunFlags { script: "foo.js".to_string(), }), - node_compat: true, + compat: true, ..Flags::default() } ); diff --git a/cli/main.rs b/cli/main.rs index 59ad77e42dd208..52e3f6e955fcdd 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -3,6 +3,7 @@ mod ast; mod auth_tokens; mod checksum; +mod compat; mod config_file; mod deno_dir; mod diagnostics; @@ -23,7 +24,6 @@ mod logger; mod lsp; mod module_graph; mod module_loader; -mod node_compat; mod ops; mod proc_state; mod source_maps; diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 78b727e31f5e9d..bc18eb5ed0bf1d 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -204,7 +204,7 @@ impl ProcState { } }; - if flags.node_compat { + if flags.compat { let mut import_map = match maybe_import_map { Some(import_map) => import_map, None => { @@ -216,7 +216,7 @@ impl ProcState { .unwrap() } }; - let node_builtins = crate::node_compat::get_mapped_node_builtins(); + let node_builtins = crate::compat::get_mapped_node_builtins(); // TODO(bartlomieju): should this actually be a hard error if user provided partial // mapping for Node modules in their own import map, or should we just print out // that some modules were not shimmed because they were already present in import map? diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 848541f2f6443d..fb07254aaf8b12 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -227,7 +227,7 @@ pub fn compile_to_runtime_flags( lock: None, log_level: flags.log_level, no_check: false, - node_compat: flags.node_compat, + compat: flags.compat, unsafely_ignore_certificate_errors: flags .unsafely_ignore_certificate_errors, no_remote: false, From d1ccdf3d854ec610d90da7f35e5d01a23043f7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 19:00:33 +0200 Subject: [PATCH 06/13] add support for promises modules --- cli/compat.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/compat.rs b/cli/compat.rs index b0fe8f5e81b00d..f2a0f7bd3cdf0f 100644 --- a/cli/compat.rs +++ b/cli/compat.rs @@ -7,6 +7,7 @@ static DENO_STD_URL: &str = "https://deno.land/std/node/"; static SUPPORTED_MODULES: &[&str] = &[ "assert", + "assert/strict", "buffer", "child_process", "console", @@ -14,19 +15,26 @@ static SUPPORTED_MODULES: &[&str] = &[ "crypto", "events", "fs", + "fs/promises", "module", "os", "path", + "path/posix", + "path/win32", "perf_hooks", "process", "querystring", "stream", + "stream/promises", + "stream/web", "string_decoder", "sys", "timers", + "timers/promises", "tty", "url", "util", + "util/types", ]; pub fn get_mapped_node_builtins() -> HashMap { From 5e2c2add9c221c2738f25702cb1f1cce2f34677a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 19:05:11 +0200 Subject: [PATCH 07/13] print out diagnotics from updated import map --- cli/proc_state.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/proc_state.rs b/cli/proc_state.rs index bc18eb5ed0bf1d..fdd4b6e998b1bd 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -217,10 +217,12 @@ impl ProcState { } }; let node_builtins = crate::compat::get_mapped_node_builtins(); - // TODO(bartlomieju): should this actually be a hard error if user provided partial - // mapping for Node modules in their own import map, or should we just print out - // that some modules were not shimmed because they were already present in import map? - import_map.update_imports(node_builtins)?; + let diagnostics = import_map.update_imports(node_builtins)?; + + if !diagnostics.is_empty() { + info!(" - {}", diagnotic); + } + maybe_import_map = Some(import_map); } From 5c7a0751e24b2f66f1d0cc81cba3a90bc5f44443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 19:05:55 +0200 Subject: [PATCH 08/13] don't store url in static --- cli/compat.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cli/compat.rs b/cli/compat.rs index f2a0f7bd3cdf0f..ec27c0871c157a 100644 --- a/cli/compat.rs +++ b/cli/compat.rs @@ -2,9 +2,6 @@ use std::collections::HashMap; -// TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? -static DENO_STD_URL: &str = "https://deno.land/std/node/"; - static SUPPORTED_MODULES: &[&str] = &[ "assert", "assert/strict", @@ -41,7 +38,9 @@ pub fn get_mapped_node_builtins() -> HashMap { let mut mappings = HashMap::new(); for module in SUPPORTED_MODULES { - let module_url = format!("{}{}.ts", DENO_STD_URL, module); + // TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? + let module_url = + format!("https://deno.land/std/node/{}.ts", DENO_STD_URL, module); mappings.insert(module.to_string(), module_url); } From 947fc4eaac5d7360697cfda63d1b9f4cfd41993e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 19:09:27 +0200 Subject: [PATCH 09/13] fix --- cli/compat.rs | 3 +-- cli/proc_state.rs | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/compat.rs b/cli/compat.rs index ec27c0871c157a..49589d58515629 100644 --- a/cli/compat.rs +++ b/cli/compat.rs @@ -39,8 +39,7 @@ pub fn get_mapped_node_builtins() -> HashMap { for module in SUPPORTED_MODULES { // TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? - let module_url = - format!("https://deno.land/std/node/{}.ts", DENO_STD_URL, module); + let module_url = format!("https://deno.land/std/node/{}.ts", module); mappings.insert(module.to_string(), module_url); } diff --git a/cli/proc_state.rs b/cli/proc_state.rs index fdd4b6e998b1bd..46711f0ea355f8 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -36,6 +36,7 @@ use deno_tls::rustls_native_certs::load_native_certs; use deno_tls::webpki_roots::TLS_SERVER_ROOTS; use import_map::ImportMap; use log::debug; +use log::info; use log::warn; use std::collections::HashMap; use std::collections::HashSet; @@ -219,8 +220,8 @@ impl ProcState { let node_builtins = crate::compat::get_mapped_node_builtins(); let diagnostics = import_map.update_imports(node_builtins)?; - if !diagnostics.is_empty() { - info!(" - {}", diagnotic); + for diagnostic in diagnostics { + info!(" - {}", diagnostic); } maybe_import_map = Some(import_map); From 154f94964e6c462136692738c51019c0122d0cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 19:19:05 +0200 Subject: [PATCH 10/13] add integration test --- cli/tests/integration/compat_tests.rs | 8 ++++++++ cli/tests/integration/mod.rs | 2 ++ cli/tests/testdata/compat/fs_promises.js | 3 +++ cli/tests/testdata/compat/fs_promises.out | 2 ++ cli/tests/testdata/compat/test.txt | 1 + 5 files changed, 16 insertions(+) create mode 100644 cli/tests/integration/compat_tests.rs create mode 100644 cli/tests/testdata/compat/fs_promises.js create mode 100644 cli/tests/testdata/compat/fs_promises.out create mode 100644 cli/tests/testdata/compat/test.txt diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs new file mode 100644 index 00000000000000..98ac2e749089f3 --- /dev/null +++ b/cli/tests/integration/compat_tests.rs @@ -0,0 +1,8 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +use crate::itest; + +itest!(compat_fs_promises { + args: "run --compat --unstable -A compat/fs_promises.js", + output: "compat/fs_promises.out", +}); diff --git a/cli/tests/integration/mod.rs b/cli/tests/integration/mod.rs index e3450c8a1245b8..65a8a7516135e0 100644 --- a/cli/tests/integration/mod.rs +++ b/cli/tests/integration/mod.rs @@ -54,6 +54,8 @@ macro_rules! itest_flaky( mod bundle; #[path = "cache_tests.rs"] mod cache; +#[path = "compat_tests.rs"] +mod compat; #[path = "compile_tests.rs"] mod compile; #[path = "coverage_tests.rs"] diff --git a/cli/tests/testdata/compat/fs_promises.js b/cli/tests/testdata/compat/fs_promises.js new file mode 100644 index 00000000000000..3f7b4c93505eb2 --- /dev/null +++ b/cli/tests/testdata/compat/fs_promises.js @@ -0,0 +1,3 @@ +import fs from "fs/promises"; +const data = await fs.readFile("compat/test.txt", "utf-8"); +console.log(data); diff --git a/cli/tests/testdata/compat/fs_promises.out b/cli/tests/testdata/compat/fs_promises.out new file mode 100644 index 00000000000000..368d06776cf2a0 --- /dev/null +++ b/cli/tests/testdata/compat/fs_promises.out @@ -0,0 +1,2 @@ +[WILDCARD] +This is some example text that will be read using compatiblity mode. diff --git a/cli/tests/testdata/compat/test.txt b/cli/tests/testdata/compat/test.txt new file mode 100644 index 00000000000000..422e7b0c0c152a --- /dev/null +++ b/cli/tests/testdata/compat/test.txt @@ -0,0 +1 @@ +This is some example text that will be read using compatiblity mode. \ No newline at end of file From 24923bcea92696665fe1d27002b0ccf79d5d3145 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 2 Oct 2021 13:22:49 -0400 Subject: [PATCH 11/13] Add more supported modules --- cli/compat.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cli/compat.rs b/cli/compat.rs index 49589d58515629..a3d16538dc0ca4 100644 --- a/cli/compat.rs +++ b/cli/compat.rs @@ -5,15 +5,23 @@ use std::collections::HashMap; static SUPPORTED_MODULES: &[&str] = &[ "assert", "assert/strict", + "async_hooks", "buffer", "child_process", + "cluster", "console", "constants", "crypto", + "dgram", + "dns", + "domain", "events", "fs", "fs/promises", + "http", + "https", "module", + "net", "os", "path", "path/posix", @@ -21,6 +29,7 @@ static SUPPORTED_MODULES: &[&str] = &[ "perf_hooks", "process", "querystring", + "readline", "stream", "stream/promises", "stream/web", @@ -28,10 +37,14 @@ static SUPPORTED_MODULES: &[&str] = &[ "sys", "timers", "timers/promises", + "tls", "tty", "url", "util", "util/types", + "v8", + "vm", + "zlib", ]; pub fn get_mapped_node_builtins() -> HashMap { From 71ca413e4b11a25582b192a138cda5a0d0b74fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 2 Oct 2021 21:56:31 +0200 Subject: [PATCH 12/13] add test for existing import map --- Cargo.lock | 4 ++-- cli/Cargo.toml | 2 +- cli/proc_state.rs | 7 +++++-- cli/tests/integration/compat_tests.rs | 8 +++++++- cli/tests/testdata/compat/existing_import_map.json | 5 +++++ cli/tests/testdata/compat/existing_import_map.out | 4 ++++ 6 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 cli/tests/testdata/compat/existing_import_map.json create mode 100644 cli/tests/testdata/compat/existing_import_map.out diff --git a/Cargo.lock b/Cargo.lock index 2bc48c16585cbe..dc0deec738ef20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1720,9 +1720,9 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" [[package]] name = "import_map" -version = "0.3.1" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14541d9b0d1dfc66d5a71d07d1911114dd6532e5fc1e1ef66006eb5f0435fd97" +checksum = "d315210af92bcde7a84672d5554fc2b4268c4d40dc9c930ae1d1ed765a8f6381" dependencies = [ "indexmap", "log", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5d84087347e5e9..c7cc426fc843a2 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -59,7 +59,7 @@ encoding_rs = "0.8.28" env_logger = "0.8.4" fancy-regex = "0.7.1" http = "0.2.4" -import_map = "0.3.1" +import_map = "0.3.3" jsonc-parser = { version = "0.17.0", features = ["serde"] } lazy_static = "1.4.0" libc = "0.2.101" diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 46711f0ea355f8..ebf1641b090d77 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -220,8 +220,11 @@ impl ProcState { let node_builtins = crate::compat::get_mapped_node_builtins(); let diagnostics = import_map.update_imports(node_builtins)?; - for diagnostic in diagnostics { - info!(" - {}", diagnostic); + if !diagnostics.is_empty() { + info!("The were problems adding Node built-ins to import map:"); + for diagnostic in diagnostics { + info!(" - {}", diagnostic); + } } maybe_import_map = Some(import_map); diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs index 98ac2e749089f3..4d93d370d25abc 100644 --- a/cli/tests/integration/compat_tests.rs +++ b/cli/tests/integration/compat_tests.rs @@ -2,7 +2,13 @@ use crate::itest; -itest!(compat_fs_promises { +itest!(fs_promises { args: "run --compat --unstable -A compat/fs_promises.js", output: "compat/fs_promises.out", }); + +itest!(existing_import_map { + args: "run --compat --import-map compat/existing_import_map.json compat/fs_promises.js", + output: "compat/existing_import_map.out", + exit_code: 1, +}); diff --git a/cli/tests/testdata/compat/existing_import_map.json b/cli/tests/testdata/compat/existing_import_map.json new file mode 100644 index 00000000000000..db59c0cc2400d7 --- /dev/null +++ b/cli/tests/testdata/compat/existing_import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "fs/promises": "./non_existent_file.js" + } +} diff --git a/cli/tests/testdata/compat/existing_import_map.out b/cli/tests/testdata/compat/existing_import_map.out new file mode 100644 index 00000000000000..818d571d439bc5 --- /dev/null +++ b/cli/tests/testdata/compat/existing_import_map.out @@ -0,0 +1,4 @@ +[WILDCARD] +The were problems adding Node built-ins to import map: + - "fs/promises" already exists and is mapped to "[WILDCARD]non_existent_file.js" +error: Cannot resolve module [WILDCARD] \ No newline at end of file From 17668f93b1516d4862c506a339927b27a5956826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 3 Oct 2021 15:56:43 +0200 Subject: [PATCH 13/13] clarify import map diagnostics --- cli/proc_state.rs | 3 ++- cli/tests/testdata/compat/existing_import_map.out | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/proc_state.rs b/cli/proc_state.rs index ebf1641b090d77..48dc335f0bf00d 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -221,10 +221,11 @@ impl ProcState { let diagnostics = import_map.update_imports(node_builtins)?; if !diagnostics.is_empty() { - info!("The were problems adding Node built-ins to import map:"); + info!("Some Node built-ins were not added to the import map:"); for diagnostic in diagnostics { info!(" - {}", diagnostic); } + info!("If you want to use Node built-ins provided by Deno remove listed specifiers from \"imports\" mapping in the import map file."); } maybe_import_map = Some(import_map); diff --git a/cli/tests/testdata/compat/existing_import_map.out b/cli/tests/testdata/compat/existing_import_map.out index 818d571d439bc5..0e319b11509f50 100644 --- a/cli/tests/testdata/compat/existing_import_map.out +++ b/cli/tests/testdata/compat/existing_import_map.out @@ -1,4 +1,5 @@ [WILDCARD] -The were problems adding Node built-ins to import map: +Some Node built-ins were not added to the import map: - "fs/promises" already exists and is mapped to "[WILDCARD]non_existent_file.js" +If you want to use Node built-ins provided by Deno remove listed specifiers from "imports" mapping in the import map file. error: Cannot resolve module [WILDCARD] \ No newline at end of file