summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-03-14 16:47:09 -0400
committerGitHub <noreply@github.com>2023-03-14 16:47:09 -0400
commit96ea5b1d3d7b4ddec386d8708dc8a01c5fa946e9 (patch)
tree6c0209413506bce00494dafda93f7c9026e917b6
parenteb990efcce1e5390e4ed76b858c05c6f9b3b6c11 (diff)
refactor: deno_core not using std::env::current_dir (#18173)
This commit changes "deno_core" to not rely on implicitly calling "std::env::current_dir()" when resolving module specifiers using APIs from "deno_core::modules_specifier". Supersedes https://github.com/denoland/deno/pull/15454
-rw-r--r--cli/tests/testdata/run/042_dyn_import_evalcontext.ts1
-rw-r--r--cli/tests/testdata/run/eval_context_throw_dom_exception.js1
-rw-r--r--core/lib.rs1
-rw-r--r--core/module_specifier.rs26
-rw-r--r--core/ops_builtin_v8.rs15
-rw-r--r--ext/node/02_require.js2
6 files changed, 8 insertions, 38 deletions
diff --git a/cli/tests/testdata/run/042_dyn_import_evalcontext.ts b/cli/tests/testdata/run/042_dyn_import_evalcontext.ts
index 6a9ffb971..386ae48ec 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 31baeedfc..99eaa0f4a 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 08df6e44d..7751be6e8 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 94ccd298c..20358e79c 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 = "<unknown>";
-
/// 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 <unknown> 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("<unknown>");
- 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", "<unknown>", awesome.as_str()),
- ("/service/awesome.ts", "<unknown>", 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 c66e4d3c2..05250dc73 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<String>,
+ specifier: String,
) -> Result<EvalContextResult<'a>, Error> {
let tc_scope = &mut v8::TryCatch::new(scope);
let source = v8::Local::<v8::String>::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 d334a60a5..43343a21a 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);