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:
parent
4c2aeb2502
commit
e8f22c0765
7 changed files with 33 additions and 13 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
),
|
||||
|
|
|
@ -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?;
|
||||
|
|
|
@ -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?;
|
||||
|
|
|
@ -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<ModuleSpecifier, ModuleResolutionError> {
|
||||
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))
|
||||
|
|
|
@ -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,7 +94,10 @@ where
|
|||
P: NodePermissions + 'static,
|
||||
{
|
||||
// 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()
|
||||
.to_file_path()
|
||||
.unwrap();
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue