From 89b61b5d05b7441d42e4e88fe1180d9d3dc1727e Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Tue, 4 May 2021 17:57:20 +0530 Subject: [PATCH] fix(cli): give context when failed to load import map (#10478) --- cli/ops/runtime_compiler.rs | 8 +++++++- cli/program_state.rs | 6 +++++- cli/tests/compiler_api_test.ts | 19 +++++++++++++++++++ cli/tests/error_import_map_unable_to_load.out | 4 ++++ cli/tests/integration_tests.rs | 11 +++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 cli/tests/error_import_map_unable_to_load.out diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index 4865c10452..8e3ca80b50 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -87,7 +87,13 @@ async fn op_emit( let file = program_state .file_fetcher .fetch(&import_map_specifier, &mut runtime_permissions) - .await?; + .await + .map_err(|e| { + generic_error(format!( + "Unable to load '{}' import map: {}", + import_map_specifier, e + )) + })?; ImportMap::from_json(import_map_specifier.as_str(), &file.source)? }; Some(import_map) diff --git a/cli/program_state.rs b/cli/program_state.rs index de6331b893..e8d2c163e5 100644 --- a/cli/program_state.rs +++ b/cli/program_state.rs @@ -101,7 +101,11 @@ impl ProgramState { )?; let file = file_fetcher .fetch(&import_map_specifier, &mut Permissions::allow_all()) - .await?; + .await + .context(format!( + "Unable to load '{}' import map", + import_map_specifier + ))?; let import_map = ImportMap::from_json(import_map_specifier.as_str(), &file.source)?; Some(import_map) diff --git a/cli/tests/compiler_api_test.ts b/cli/tests/compiler_api_test.ts index 04a4675b81..c6e7de6515 100644 --- a/cli/tests/compiler_api_test.ts +++ b/cli/tests/compiler_api_test.ts @@ -338,3 +338,22 @@ Deno.test({ assert(files["deno:///bundle.js"].endsWith("})();\n")); }, }); + +Deno.test({ + name: `Deno.emit() - throws descriptive error when unable to load import map`, + async fn() { + await assertThrowsAsync( + async () => { + await Deno.emit("/a.ts", { + bundle: "classic", + sources: { + "/a.ts": `console.log("hello");`, + }, + importMapPath: "file:///import_map_does_not_exist.json", + }); + }, + Error, + "Unable to load 'file:///import_map_does_not_exist.json' import map", + ); + }, +}); diff --git a/cli/tests/error_import_map_unable_to_load.out b/cli/tests/error_import_map_unable_to_load.out new file mode 100644 index 0000000000..50760e4386 --- /dev/null +++ b/cli/tests/error_import_map_unable_to_load.out @@ -0,0 +1,4 @@ +error: Unable to load '[WILDCARD]' import map + +Caused by: + [WILDCARD] \ No newline at end of file diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 5cbe00302b..f2d1c17707 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -3841,6 +3841,17 @@ console.log("finish"); http_server: true, }); + // This test ensures that a descriptive error is shown when we're unable to load + // the import map. Even though this tests only the `run` subcommand, we can be sure + // that the error message is similar for other subcommands as they all use + // `program_state.maybe_import_map` to access the import map underneath. + itest!(error_import_map_unable_to_load { + args: + "run --import-map=import_maps/does_not_exist.json import_maps/test.ts", + output: "error_import_map_unable_to_load.out", + exit_code: 1, + }); + #[test] fn no_validate_asm() { let output = util::deno_cmd()