diff --git a/cli/module_loader.rs b/cli/module_loader.rs index d3293c9574..2a7a69dca9 100644 --- a/cli/module_loader.rs +++ b/cli/module_loader.rs @@ -63,6 +63,7 @@ impl CliModuleLoader { fn load_prepared_module( &self, specifier: &ModuleSpecifier, + maybe_referrer: Option, ) -> Result { if specifier.as_str() == "node:module" { return Ok(ModuleCodeSource { @@ -121,10 +122,13 @@ impl CliModuleLoader { media_type: *media_type, }) } - _ => Err(anyhow!( - "Loading unprepared module: {}", - specifier.to_string() - )), + _ => { + let mut msg = format!("Loading unprepared module: {}", specifier); + if let Some(referrer) = maybe_referrer { + msg = format!("{}, imported from: {}", msg, referrer.as_str()); + } + Err(anyhow!(msg)) + } } } @@ -164,7 +168,7 @@ impl CliModuleLoader { media_type: MediaType::from(specifier), } } else { - self.load_prepared_module(specifier)? + self.load_prepared_module(specifier, maybe_referrer)? }; let code = if self.ps.options.is_inspecting() { // we need the code with the source map in order for @@ -261,7 +265,7 @@ impl SourceMapGetter for CliModuleLoader { "wasm" | "file" | "http" | "https" | "data" | "blob" => (), _ => return None, } - let source = self.load_prepared_module(&specifier).ok()?; + let source = self.load_prepared_module(&specifier, None).ok()?; source_map_from_code(&source.code) } diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 690a211818..cd803cc4f3 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -138,7 +138,10 @@ static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[ }, NodeModulePolyfill { name: "module", - specifier: "node/module.ts", + // NOTE(bartlomieju): `module` is special, because we don't want to use + // `deno_std/node/module.ts`, but instead use a special shim that we + // provide in `ext/node`. + specifier: "[USE `deno_node::MODULE_ES_SHIM` to get this module]", }, NodeModulePolyfill { name: "net", @@ -265,6 +268,13 @@ fn is_builtin_node_module(specifier: &str) -> bool { } pub fn resolve_builtin_node_module(specifier: &str) -> Result { + // NOTE(bartlomieju): `module` is special, because we don't want to use + // `deno_std/node/module.ts`, but instead use a special shim that we + // provide in `ext/node`. + if specifier == "module" { + return Ok(Url::parse("node:module").unwrap()); + } + if let Some(module) = find_builtin_node_module(specifier) { let module_url = NODE_COMPAT_URL.join(module.specifier).unwrap(); return Ok(module_url); diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index 8bbb89d65e..85c31786ce 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -402,6 +402,13 @@ itest!(deno_run_non_existent { exit_code: 1, }); +itest!(builtin_module_module { + args: "run --allow-read --quiet --unstable npm/builtin_module_module/main.js", + output: "npm/builtin_module_module/main.out", + envs: env_vars(), + http_server: true, +}); + #[test] fn ensure_registry_files_local() { // ensures the registry files all point at local tarballs diff --git a/cli/tests/testdata/npm/builtin_module_module/main.js b/cli/tests/testdata/npm/builtin_module_module/main.js new file mode 100644 index 0000000000..9a036791b3 --- /dev/null +++ b/cli/tests/testdata/npm/builtin_module_module/main.js @@ -0,0 +1 @@ +import "npm:@denotest/builtin-module-module"; diff --git a/cli/tests/testdata/npm/builtin_module_module/main.out b/cli/tests/testdata/npm/builtin_module_module/main.out new file mode 100644 index 0000000000..b0a370638c --- /dev/null +++ b/cli/tests/testdata/npm/builtin_module_module/main.out @@ -0,0 +1,2 @@ +function +function diff --git a/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/index.js new file mode 100644 index 0000000000..cb610a613e --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/index.js @@ -0,0 +1,5 @@ +import m1 from "node:module"; +import m2 from "module"; + +console.log(typeof m1.Module); +console.log(typeof m2.Module); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/package.json new file mode 100644 index 0000000000..5167f18a34 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/builtin-module-module/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/builtin-module-module", + "version": "1.0.0", + "type": "module" +}