Skip to content

Commit 191f4f1

Browse files
authored
fix(bundle): explicit error when using an npm specifier with deno bundle (#16637)
1 parent 1c373ce commit 191f4f1

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

cli/main.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ use crate::emit::TsConfigType;
6868
use crate::file_fetcher::File;
6969
use crate::file_watcher::ResolutionResult;
7070
use crate::graph_util::graph_lock_or_exit;
71-
use crate::graph_util::graph_valid;
7271
use crate::proc_state::ProcState;
7372
use crate::resolver::CliResolver;
7473
use crate::tools::check;
@@ -89,6 +88,7 @@ use deno_runtime::colors;
8988
use deno_runtime::fmt_errors::format_js_error;
9089
use deno_runtime::permissions::Permissions;
9190
use deno_runtime::tokio_util::run_local;
91+
use graph_util::GraphData;
9292
use log::debug;
9393
use log::info;
9494
use npm::NpmPackageReference;
@@ -150,22 +150,10 @@ async fn compile_command(
150150
generic_error("There should only be one reference to ModuleGraph")
151151
})?;
152152

153-
graph.valid().unwrap();
154-
155153
// at the moment, we don't support npm specifiers in deno_compile, so show an error
156-
let first_npm_specifier = graph
157-
.specifiers()
158-
.values()
159-
.filter_map(|r| match r {
160-
Ok((specifier, kind, _)) if *kind == deno_graph::ModuleKind::External => {
161-
Some(specifier.clone())
162-
}
163-
_ => None,
164-
})
165-
.next();
166-
if let Some(npm_specifier) = first_npm_specifier {
167-
bail!("npm specifiers have not yet been implemented for deno compile (https://github.com/denoland/deno/issues/15960). Found: {}", npm_specifier)
168-
}
154+
error_for_any_npm_specifier(&graph)?;
155+
156+
graph.valid().unwrap();
169157

170158
let parser = ps.parsed_source_cache.as_capturing_parser();
171159
let eszip = eszip::EszipV2::from_graph(graph, &parser, Default::default())?;
@@ -384,11 +372,18 @@ async fn create_graph_and_maybe_check(
384372
);
385373

386374
let check_js = ps.options.check_js();
387-
graph_valid(
388-
&graph,
389-
ps.options.type_check_mode() != TypeCheckMode::None,
390-
check_js,
391-
)?;
375+
let mut graph_data = GraphData::default();
376+
graph_data.add_graph(&graph, false);
377+
graph_data
378+
.check(
379+
&graph.roots,
380+
ps.options.type_check_mode() != TypeCheckMode::None,
381+
check_js,
382+
)
383+
.unwrap()?;
384+
ps.npm_resolver
385+
.add_package_reqs(graph_data.npm_package_reqs().clone())
386+
.await?;
392387
graph_lock_or_exit(&graph);
393388

394389
if ps.options.type_check_mode() != TypeCheckMode::None {
@@ -403,7 +398,7 @@ async fn create_graph_and_maybe_check(
403398
let cache = TypeCheckCache::new(&ps.dir.type_checking_cache_db_file_path());
404399
let check_result = check::check(
405400
&graph.roots,
406-
Arc::new(RwLock::new(graph.as_ref().into())),
401+
Arc::new(RwLock::new(graph_data)),
407402
&cache,
408403
ps.npm_resolver.clone(),
409404
check::CheckOptions {
@@ -500,6 +495,9 @@ async fn bundle_command(
500495
let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
501496
let out_file = bundle_flags.out_file.clone();
502497
async move {
498+
// at the moment, we don't support npm specifiers in deno bundle, so show an error
499+
error_for_any_npm_specifier(&graph)?;
500+
503501
let bundle_output = bundle_module_graph(graph.as_ref(), &ps)?;
504502
debug!(">>>>> bundle END");
505503

@@ -561,6 +559,26 @@ async fn bundle_command(
561559
Ok(0)
562560
}
563561

562+
fn error_for_any_npm_specifier(
563+
graph: &deno_graph::ModuleGraph,
564+
) -> Result<(), AnyError> {
565+
let first_npm_specifier = graph
566+
.specifiers()
567+
.values()
568+
.filter_map(|r| match r {
569+
Ok((specifier, kind, _)) if *kind == deno_graph::ModuleKind::External => {
570+
Some(specifier.clone())
571+
}
572+
_ => None,
573+
})
574+
.next();
575+
if let Some(npm_specifier) = first_npm_specifier {
576+
bail!("npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: {}", npm_specifier)
577+
} else {
578+
Ok(())
579+
}
580+
}
581+
564582
async fn doc_command(
565583
flags: Flags,
566584
doc_flags: DocFlags,

cli/tests/integration/npm_tests.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,16 @@ fn ensure_registry_files_local() {
719719
}
720720

721721
itest!(compile_errors {
722-
args: "compile -A --quiet npm/esm/main.js",
723-
output_str: Some("error: npm specifiers have not yet been implemented for deno compile (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5\n"),
722+
args: "compile -A --quiet npm/cached_only/main.ts",
723+
output_str: Some("error: npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5\n"),
724+
exit_code: 1,
725+
envs: env_vars(),
726+
http_server: true,
727+
});
728+
729+
itest!(bundle_errors {
730+
args: "bundle --quiet npm/esm/main.js",
731+
output_str: Some("error: npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5\n"),
724732
exit_code: 1,
725733
envs: env_vars(),
726734
http_server: true,

0 commit comments

Comments
 (0)