diff --git a/cli/tests/testdata/run/042_dyn_import_evalcontext.ts b/cli/tests/testdata/run/042_dyn_import_evalcontext.ts index 6a9ffb9715..386ae48ec7 100644 --- a/cli/tests/testdata/run/042_dyn_import_evalcontext.ts +++ b/cli/tests/testdata/run/042_dyn_import_evalcontext.ts @@ -1,4 +1,5 @@ // @ts-expect-error "Deno[Deno.internal].core" is not a public interface Deno[Deno.internal].core.evalContext( "(async () => console.log(await import('./subdir/mod4.js')))()", + new URL("..", import.meta.url).href, ); diff --git a/cli/tests/testdata/run/eval_context_throw_dom_exception.js b/cli/tests/testdata/run/eval_context_throw_dom_exception.js index 31baeedfc3..99eaa0f4a2 100644 --- a/cli/tests/testdata/run/eval_context_throw_dom_exception.js +++ b/cli/tests/testdata/run/eval_context_throw_dom_exception.js @@ -1,4 +1,5 @@ const [, errorInfo] = Deno[Deno.internal].core.evalContext( 'throw new DOMException("foo")', + new URL("..", import.meta.url).href, ); console.log(errorInfo); diff --git a/core/lib.rs b/core/lib.rs index 08df6e44dc..7751be6e8a 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -74,7 +74,6 @@ pub use crate::module_specifier::resolve_url; pub use crate::module_specifier::resolve_url_or_path; pub use crate::module_specifier::ModuleResolutionError; pub use crate::module_specifier::ModuleSpecifier; -pub use crate::module_specifier::DUMMY_SPECIFIER; pub use crate::modules::ExtModuleLoader; pub use crate::modules::ExtModuleLoaderCb; pub use crate::modules::FsModuleLoader; diff --git a/core/module_specifier.rs b/core/module_specifier.rs index 94ccd298c4..20358e79c8 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -1,7 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use crate::normalize_path; -use std::env::current_dir; use std::error::Error; use std::fmt; use std::path::Path; @@ -9,8 +8,6 @@ use std::path::PathBuf; use url::ParseError; use url::Url; -pub const DUMMY_SPECIFIER: &str = ""; - /// Error indicating the reason resolving a module specifier failed. #[derive(Clone, Debug, Eq, PartialEq)] pub enum ModuleResolutionError { @@ -85,18 +82,7 @@ pub fn resolve_import( // 3. Return the result of applying the URL parser to specifier with base // URL as the base URL. Err(ParseError::RelativeUrlWithoutBase) => { - let base = if base == DUMMY_SPECIFIER { - // Handle case, happening under e.g. repl. - // Use CWD for such case. - - // Forcefully join base to current dir. - // Otherwise, later joining in Url would be interpreted in - // the parent directory (appending trailing slash does not work) - let path = current_dir().unwrap().join(base); - Url::from_file_path(path).unwrap() - } else { - Url::parse(base).map_err(InvalidBaseUrl)? - }; + let base = Url::parse(base).map_err(InvalidBaseUrl)?; base.join(specifier).map_err(InvalidUrl)? } @@ -181,20 +167,12 @@ mod tests { use super::*; use crate::serde_json::from_value; use crate::serde_json::json; + use std::env::current_dir; use std::path::Path; #[test] fn test_resolve_import() { - fn get_path(specifier: &str) -> Url { - let base_path = current_dir().unwrap().join(""); - let base_url = Url::from_file_path(base_path).unwrap(); - base_url.join(specifier).unwrap() - } - let awesome = get_path("/awesome.ts"); - let awesome_srv = get_path("/service/awesome.ts"); let tests = vec![ - ("/awesome.ts", "", awesome.as_str()), - ("/service/awesome.ts", "", awesome_srv.as_str()), ( "./005_more_imports.ts", "http://deno.land/core/tests/006_url_imports.ts", diff --git a/core/ops_builtin_v8.rs b/core/ops_builtin_v8.rs index c66e4d3c2d..05250dc73c 100644 --- a/core/ops_builtin_v8.rs +++ b/core/ops_builtin_v8.rs @@ -6,14 +6,13 @@ use crate::error::range_error; use crate::error::type_error; use crate::error::JsError; use crate::ops_builtin::WasmStreamingResource; -use crate::resolve_url_or_path; +use crate::resolve_url; use crate::serde_v8::from_v8; use crate::source_map::apply_source_map as apply_source_map_; use crate::JsRealm; use crate::JsRuntime; use crate::OpDecl; use crate::ZeroCopyBuf; -use anyhow::Context; use anyhow::Error; use deno_ops::op; use serde::Deserialize; @@ -160,20 +159,12 @@ struct EvalContextResult<'s>( fn op_eval_context<'a>( scope: &mut v8::HandleScope<'a>, source: serde_v8::Value<'a>, - specifier: Option, + specifier: String, ) -> Result, Error> { let tc_scope = &mut v8::TryCatch::new(scope); let source = v8::Local::::try_from(source.v8_value) .map_err(|_| type_error("Invalid source"))?; - let specifier = match specifier { - Some(s) => { - // TODO(bartlomieju): ideally we shouldn't need to call `current_dir()` on each - // call - maybe it should be caller's responsibility to pass fully resolved URL? - let cwd = std::env::current_dir().context("Unable to get CWD")?; - resolve_url_or_path(&s, &cwd)?.to_string() - } - None => crate::DUMMY_SPECIFIER.to_string(), - }; + let specifier = resolve_url(&specifier)?.to_string(); let specifier = v8::String::new(tc_scope, &specifier).unwrap(); let origin = script_origin(tc_scope, specifier); diff --git a/ext/node/02_require.js b/ext/node/02_require.js index d334a60a50..43343a21ae 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.js @@ -715,7 +715,7 @@ function wrapSafe( cjsModuleInstance, ) { const wrapper = Module.wrap(content); - const [f, err] = core.evalContext(wrapper, filename); + const [f, err] = core.evalContext(wrapper, `file://${filename}`); if (err) { if (node.globalThis.process.mainModule === cjsModuleInstance) { enrichCJSError(err.thrown);