1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

refactor(core): pass cwd explicitly to resolve_path (#18092)

Towards landing #15454
This commit is contained in:
Bartek Iwańczuk 2023-03-13 13:50:23 -04:00 committed by GitHub
parent 4c2aeb2502
commit e8f22c0765
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 13 deletions

View file

@ -368,6 +368,7 @@ fn get_local_completions(
} else { } else {
false false
}; };
let cwd = std::env::current_dir().ok()?;
if current_path.is_dir() { if current_path.is_dir() {
let items = std::fs::read_dir(current_path).ok()?; let items = std::fs::read_dir(current_path).ok()?;
Some( Some(
@ -375,7 +376,7 @@ fn get_local_completions(
.filter_map(|de| { .filter_map(|de| {
let de = de.ok()?; let de = de.ok()?;
let label = de.path().file_name()?.to_string_lossy().to_string(); 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 { if &entry_specifier == base {
return None; return None;
} }

View file

@ -196,7 +196,8 @@ fn typecheck_core() {
util::root_path() util::root_path()
.join("core/lib.deno_core.d.ts") .join("core/lib.deno_core.d.ts")
.to_str() .to_str()
.unwrap() .unwrap(),
&std::env::current_dir().unwrap()
) )
.unwrap() .unwrap()
), ),

View file

@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use anyhow::Context;
use deno_core::anyhow::Error; use deno_core::anyhow::Error;
use deno_core::FsModuleLoader; use deno_core::FsModuleLoader;
use deno_core::JsRuntime; use deno_core::JsRuntime;
@ -24,7 +25,10 @@ fn main() -> Result<(), Error> {
.enable_all() .enable_all()
.build()?; .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 future = async move {
let mod_id = js_runtime.load_main_module(&main_module, None).await?; let mod_id = js_runtime.load_main_module(&main_module, None).await?;

View file

@ -9,6 +9,7 @@ use std::rc::Rc;
use anyhow::anyhow; use anyhow::anyhow;
use anyhow::bail; use anyhow::bail;
use anyhow::Context;
use anyhow::Error; use anyhow::Error;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_ast::ParseParams; use deno_ast::ParseParams;
@ -106,7 +107,10 @@ fn main() -> Result<(), Error> {
..Default::default() ..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 future = async move {
let mod_id = js_runtime.load_main_module(&main_module, None).await?; let mod_id = js_runtime.load_main_module(&main_module, None).await?;

View file

@ -4,6 +4,7 @@ use crate::normalize_path;
use std::env::current_dir; use std::env::current_dir;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use url::ParseError; use url::ParseError;
use url::Url; use url::Url;
@ -128,7 +129,9 @@ pub fn resolve_url_or_path(
if specifier_has_uri_scheme(specifier) { if specifier_has_uri_scheme(specifier) {
resolve_url(specifier) resolve_url(specifier)
} else { } 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. /// working directory.
pub fn resolve_path( pub fn resolve_path(
path_str: &str, path_str: &str,
current_dir: &Path,
) -> Result<ModuleSpecifier, ModuleResolutionError> { ) -> Result<ModuleSpecifier, ModuleResolutionError> {
let path = current_dir() let path = current_dir.join(path_str);
.map_err(|_| ModuleResolutionError::InvalidPath(path_str.into()))?
.join(path_str);
let path = normalize_path(path); let path = normalize_path(path);
Url::from_file_path(&path) Url::from_file_path(&path)
.map_err(|()| ModuleResolutionError::InvalidPath(path)) .map_err(|()| ModuleResolutionError::InvalidPath(path))

View file

@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::normalize_path; use deno_core::normalize_path;
@ -93,7 +94,10 @@ where
P: NodePermissions + 'static, P: NodePermissions + 'static,
{ {
// Guarantee that "from" is absolute. // Guarantee that "from" is absolute.
let from = deno_core::resolve_path(&from) let from = deno_core::resolve_path(
&from,
&std::env::current_dir().context("Unable to get CWD")?,
)
.unwrap() .unwrap()
.to_file_path() .to_file_path()
.unwrap(); .unwrap();

View file

@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::anyhow::Context;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::FsModuleLoader; use deno_core::FsModuleLoader;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel; use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
@ -69,7 +70,10 @@ async fn main() -> Result<(), AnyError> {
let js_path = let js_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js"); 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 permissions = PermissionsContainer::allow_all();
let mut worker = MainWorker::bootstrap_from_options( let mut worker = MainWorker::bootstrap_from_options(