From 6dd7a7ecd9d9b09f5112c73c67aa5f94480c8196 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Tue, 1 Jun 2021 07:45:37 +0100 Subject: [PATCH] fix(cli): represent bare imports as module graph error slots (#10804) Fixes #10795 --- cli/module_graph.rs | 44 ++++++++++++------- ...error.ts => 095_cache_with_bare_import.ts} | 0 cli/tests/095_cache_with_bare_import.ts.out | 1 + cli/tests/error_027_bare_import_error.ts.out | 8 ---- .../error_027_bundle_with_bare_import.ts | 1 + .../error_027_bundle_with_bare_import.ts.out | 1 + cli/tests/integration_tests.rs | 12 +++-- 7 files changed, 41 insertions(+), 26 deletions(-) rename cli/tests/{error_027_bare_import_error.ts => 095_cache_with_bare_import.ts} (100%) create mode 100644 cli/tests/095_cache_with_bare_import.ts.out delete mode 100644 cli/tests/error_027_bare_import_error.ts.out create mode 100644 cli/tests/error_027_bundle_with_bare_import.ts create mode 100644 cli/tests/error_027_bundle_with_bare_import.ts.out diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 5bfa52e892..2ae112c8d3 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -38,6 +38,7 @@ use deno_core::serde::Serialize; use deno_core::serde::Serializer; use deno_core::serde_json::json; use deno_core::serde_json::Value; +use deno_core::url::Url; use deno_core::ModuleResolutionError; use deno_core::ModuleSource; use deno_core::ModuleSpecifier; @@ -398,9 +399,13 @@ impl Module { Ok(specifier) => Some(specifier), Err(any_error) => { match any_error.downcast_ref::() { - Some(ModuleResolutionError::ImportPrefixMissing(..)) => None, + Some(ModuleResolutionError::ImportPrefixMissing(..)) => { + Some(Url::parse(&format!("bare:{}", &desc.specifier)).unwrap()) + } _ => match any_error.downcast_ref::() { - Some(ImportMapError::UnmappedBareSpecifier(..)) => None, + Some(ImportMapError::UnmappedBareSpecifier(..)) => Some( + Url::parse(&format!("bare:{}", &desc.specifier)).unwrap(), + ), _ => { return Err(any_error); } @@ -1908,19 +1913,28 @@ impl GraphBuilder { } for (_, dep) in module.dependencies.iter() { let maybe_referrer = Some(dep.location.clone()); - if let Some(specifier) = dep.maybe_code.as_ref() { - self.fetch( - specifier, - &maybe_referrer, - is_root_dynamic || dep.is_dynamic, - ); - } - if let Some(specifier) = dep.maybe_type.as_ref() { - self.fetch( - specifier, - &maybe_referrer, - is_root_dynamic || dep.is_dynamic, - ); + for maybe_specifier in &[dep.maybe_code.as_ref(), dep.maybe_type.as_ref()] + { + if let Some(&dep_specifier) = maybe_specifier.as_ref() { + if dep_specifier.scheme() == "bare" { + self.graph.modules.insert( + dep_specifier.clone(), + ModuleSlot::Err(Arc::new( + ModuleResolutionError::ImportPrefixMissing( + dep_specifier.path().to_string(), + Some(specifier.to_string()), + ) + .into(), + )), + ); + } else { + self.fetch( + dep_specifier, + &maybe_referrer, + is_root_dynamic || dep.is_dynamic, + ); + } + } } } if let Some((_, specifier)) = module.maybe_types.as_ref() { diff --git a/cli/tests/error_027_bare_import_error.ts b/cli/tests/095_cache_with_bare_import.ts similarity index 100% rename from cli/tests/error_027_bare_import_error.ts rename to cli/tests/095_cache_with_bare_import.ts diff --git a/cli/tests/095_cache_with_bare_import.ts.out b/cli/tests/095_cache_with_bare_import.ts.out new file mode 100644 index 0000000000..f424f4c3e1 --- /dev/null +++ b/cli/tests/095_cache_with_bare_import.ts.out @@ -0,0 +1 @@ +[WILDCARD]error: Relative import path "foo" not prefixed with / or ./ or ../ from "file:///[WILDCARD]/095_cache_with_bare_import.ts" diff --git a/cli/tests/error_027_bare_import_error.ts.out b/cli/tests/error_027_bare_import_error.ts.out deleted file mode 100644 index b52873d89e..0000000000 --- a/cli/tests/error_027_bare_import_error.ts.out +++ /dev/null @@ -1,8 +0,0 @@ -[WILDCARD]error: Unable to output bundle during Graph::bundle(). - -Caused by: - 0: load_transformed failed - 1: failed to analyze module - 2: failed to resolve foo from - 3: The graph is missing a dependency. - Specifier: foo from file:///[WILDCARD]/error_027_bare_import_error.ts diff --git a/cli/tests/error_027_bundle_with_bare_import.ts b/cli/tests/error_027_bundle_with_bare_import.ts new file mode 100644 index 0000000000..c0748305d5 --- /dev/null +++ b/cli/tests/error_027_bundle_with_bare_import.ts @@ -0,0 +1 @@ +import "foo"; diff --git a/cli/tests/error_027_bundle_with_bare_import.ts.out b/cli/tests/error_027_bundle_with_bare_import.ts.out new file mode 100644 index 0000000000..3aa4a42a2d --- /dev/null +++ b/cli/tests/error_027_bundle_with_bare_import.ts.out @@ -0,0 +1 @@ +[WILDCARD]error: Relative import path "foo" not prefixed with / or ./ or ../ from "file:///[WILDCARD]/error_027_bundle_with_bare_import.ts" diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 5af533dab2..6c5626666a 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -3139,6 +3139,12 @@ console.log("finish"); exit_code: 1, }); + itest!(_095_cache_with_bare_import { + args: "cache 095_cache_with_bare_import.ts", + output: "095_cache_with_bare_import.ts.out", + exit_code: 1, + }); + itest!(dynamic_import_permissions_remote_remote { args: "run --quiet --reload --allow-net=localhost:4545 dynamic_import/permissions_remote_remote.ts", output: "dynamic_import/permissions_remote_remote.ts.out", @@ -3481,9 +3487,9 @@ console.log("finish"); http_server: true, }); - itest!(error_027_bare_import_error { - args: "bundle error_027_bare_import_error.ts", - output: "error_027_bare_import_error.ts.out", + itest!(error_027_bundle_with_bare_import { + args: "bundle error_027_bundle_with_bare_import.ts", + output: "error_027_bundle_with_bare_import.ts.out", exit_code: 1, });