mirror of
https://github.com/denoland/deno.git
synced 2024-12-01 16:51:13 -05:00
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
This commit is contained in:
parent
c125bb1e5b
commit
ddb5f64d02
6 changed files with 8 additions and 38 deletions
|
@ -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,
|
||||
);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const [, errorInfo] = Deno[Deno.internal].core.evalContext(
|
||||
'throw new DOMException("foo")',
|
||||
new URL("..", import.meta.url).href,
|
||||
);
|
||||
console.log(errorInfo);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue