1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(npm): add more context to errors when file doesn't exist (#15749)

This commit is contained in:
Bartek Iwańczuk 2022-09-03 15:43:35 +02:00 committed by GitHub
parent 223403e899
commit fc4025c878
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 4 deletions

View file

@ -11,6 +11,7 @@ use crate::text_encoding::source_map_from_code;
use deno_ast::MediaType;
use deno_core::anyhow::anyhow;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::futures::future::FutureExt;
use deno_core::futures::Future;
@ -130,10 +131,19 @@ impl CliModuleLoader {
fn load_sync(
&self,
specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
) -> Result<ModuleSource, AnyError> {
let code_source = if self.ps.npm_resolver.in_npm_package(specifier) {
let file_path = specifier.to_file_path().unwrap();
let code = std::fs::read_to_string(file_path)?;
let code = std::fs::read_to_string(&file_path).with_context(|| {
let mut msg = "Unable to load ".to_string();
msg.push_str(&*file_path.to_string_lossy());
if let Some(referrer) = maybe_referrer {
msg.push_str(" imported from ");
msg.push_str(referrer.as_str());
}
msg
})?;
let is_cjs = self.ps.cjs_resolutions.lock().contains(specifier);
let code = if is_cjs {
@ -149,7 +159,6 @@ impl CliModuleLoader {
// only inject node globals for esm
node::esm_code_with_node_globals(specifier, code)?
};
ModuleCodeSource {
code,
found_url: specifier.clone(),
@ -192,13 +201,15 @@ impl ModuleLoader for CliModuleLoader {
fn load(
&self,
specifier: &ModuleSpecifier,
_maybe_referrer: Option<ModuleSpecifier>,
maybe_referrer: Option<ModuleSpecifier>,
_is_dynamic: bool,
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
// NOTE: this block is async only because of `deno_core` interface
// requirements; module was already loaded when constructing module graph
// during call to `prepare_load` so we can load it synchronously.
Box::pin(deno_core::futures::future::ready(self.load_sync(specifier)))
Box::pin(deno_core::futures::future::ready(
self.load_sync(specifier, maybe_referrer),
))
}
fn prepare_load(

View file

@ -131,6 +131,14 @@ itest!(tarball_with_global_header {
http_server: true,
});
itest!(nonexistent_file {
args: "run --unstable -A --quiet npm/nonexistent_file/main.js",
output: "npm/nonexistent_file/main.out",
envs: env_vars(),
http_server: true,
exit_code: 1,
});
#[test]
fn parallel_downloading() {
let (out, _err) = util::run_and_collect_output_with_args(

View file

@ -0,0 +1 @@
import hmacSHA512 from "npm:crypto-js/hmac-sha512";

View file

@ -0,0 +1,4 @@
error: Unable to load [WILDCARD]hmac-sha512 imported from [WILDCARD]/testdata/npm/nonexistent_file/main.js
Caused by:
[WILDCARD]

Binary file not shown.

File diff suppressed because one or more lines are too long