From 96ea5b1d3d7b4ddec386d8708dc8a01c5fa946e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 14 Mar 2023 16:47:09 -0400 Subject: 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 --- core/module_specifier.rs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) (limited to 'core/module_specifier.rs') 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 = ""; - /// 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", -- cgit v1.2.3