From e8f22c076525c2fa55115349157f67085df287bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 13 Mar 2023 13:50:23 -0400 Subject: refactor(core): pass cwd explicitly to resolve_path (#18092) Towards landing #15454 --- cli/lsp/completions.rs | 3 ++- cli/tests/integration/check_tests.rs | 3 ++- core/examples/fs_module_loader.rs | 6 +++++- core/examples/ts_module_loader.rs | 6 +++++- core/module_specifier.rs | 10 ++++++---- ext/node/ops.rs | 12 ++++++++---- runtime/examples/hello_runtime.rs | 6 +++++- 7 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index 0c27500e2..3651fbeec 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -368,6 +368,7 @@ fn get_local_completions( } else { false }; + let cwd = std::env::current_dir().ok()?; if current_path.is_dir() { let items = std::fs::read_dir(current_path).ok()?; Some( @@ -375,7 +376,7 @@ fn get_local_completions( .filter_map(|de| { let de = de.ok()?; let label = de.path().file_name()?.to_string_lossy().to_string(); - let entry_specifier = resolve_path(de.path().to_str()?).ok()?; + let entry_specifier = resolve_path(de.path().to_str()?, &cwd).ok()?; if &entry_specifier == base { return None; } diff --git a/cli/tests/integration/check_tests.rs b/cli/tests/integration/check_tests.rs index 7f9ff3ff5..7e6fbd836 100644 --- a/cli/tests/integration/check_tests.rs +++ b/cli/tests/integration/check_tests.rs @@ -196,7 +196,8 @@ fn typecheck_core() { util::root_path() .join("core/lib.deno_core.d.ts") .to_str() - .unwrap() + .unwrap(), + &std::env::current_dir().unwrap() ) .unwrap() ), diff --git a/core/examples/fs_module_loader.rs b/core/examples/fs_module_loader.rs index a8d33e104..737ff1d5c 100644 --- a/core/examples/fs_module_loader.rs +++ b/core/examples/fs_module_loader.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use anyhow::Context; use deno_core::anyhow::Error; use deno_core::FsModuleLoader; use deno_core::JsRuntime; @@ -24,7 +25,10 @@ fn main() -> Result<(), Error> { .enable_all() .build()?; - let main_module = deno_core::resolve_path(main_url)?; + let main_module = deno_core::resolve_path( + main_url, + &std::env::current_dir().context("Unable to get CWD")?, + )?; let future = async move { let mod_id = js_runtime.load_main_module(&main_module, None).await?; diff --git a/core/examples/ts_module_loader.rs b/core/examples/ts_module_loader.rs index b57b04493..289b43dc9 100644 --- a/core/examples/ts_module_loader.rs +++ b/core/examples/ts_module_loader.rs @@ -9,6 +9,7 @@ use std::rc::Rc; use anyhow::anyhow; use anyhow::bail; +use anyhow::Context; use anyhow::Error; use deno_ast::MediaType; use deno_ast::ParseParams; @@ -106,7 +107,10 @@ fn main() -> Result<(), Error> { ..Default::default() }); - let main_module = resolve_path(main_url)?; + let main_module = resolve_path( + main_url, + &std::env::current_dir().context("Unable to get CWD")?, + )?; let future = async move { let mod_id = js_runtime.load_main_module(&main_module, None).await?; diff --git a/core/module_specifier.rs b/core/module_specifier.rs index c65f34110..72d693723 100644 --- a/core/module_specifier.rs +++ b/core/module_specifier.rs @@ -4,6 +4,7 @@ use crate::normalize_path; use std::env::current_dir; use std::error::Error; use std::fmt; +use std::path::Path; use std::path::PathBuf; use url::ParseError; use url::Url; @@ -128,7 +129,9 @@ pub fn resolve_url_or_path( if specifier_has_uri_scheme(specifier) { resolve_url(specifier) } else { - resolve_path(specifier) + let cwd = current_dir() + .map_err(|_| ModuleResolutionError::InvalidPath(specifier.into()))?; + resolve_path(specifier, &cwd) } } @@ -137,10 +140,9 @@ pub fn resolve_url_or_path( /// working directory. pub fn resolve_path( path_str: &str, + current_dir: &Path, ) -> Result { - let path = current_dir() - .map_err(|_| ModuleResolutionError::InvalidPath(path_str.into()))? - .join(path_str); + let path = current_dir.join(path_str); let path = normalize_path(path); Url::from_file_path(&path) .map_err(|()| ModuleResolutionError::InvalidPath(path)) diff --git a/ext/node/ops.rs b/ext/node/ops.rs index 046578ca5..fc192637a 100644 --- a/ext/node/ops.rs +++ b/ext/node/ops.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use deno_core::anyhow::Context; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::normalize_path; @@ -93,10 +94,13 @@ where P: NodePermissions + 'static, { // Guarantee that "from" is absolute. - let from = deno_core::resolve_path(&from) - .unwrap() - .to_file_path() - .unwrap(); + let from = deno_core::resolve_path( + &from, + &std::env::current_dir().context("Unable to get CWD")?, + ) + .unwrap() + .to_file_path() + .unwrap(); ensure_read_permission::

(state, &from)?; diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index b8b152467..ac872c192 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use deno_core::anyhow::Context; use deno_core::error::AnyError; use deno_core::FsModuleLoader; use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel; @@ -69,7 +70,10 @@ async fn main() -> Result<(), AnyError> { let js_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js"); - let main_module = deno_core::resolve_path(&js_path.to_string_lossy())?; + let main_module = deno_core::resolve_path( + &js_path.to_string_lossy(), + &std::env::current_dir().context("Unable to get CWD")?, + )?; let permissions = PermissionsContainer::allow_all(); let mut worker = MainWorker::bootstrap_from_options( -- cgit v1.2.3