diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 5b0db96741..7099b70ba8 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -336,6 +336,7 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) { .with_cwd(Some( engine_state .cwd(None) + .map(|cwd| cwd.into_std_path_buf()) .unwrap_or_default() .to_string_lossy() .to_string(), diff --git a/crates/nu-cmd-base/src/util.rs b/crates/nu-cmd-base/src/util.rs index 905c990a9e..e485243877 100644 --- a/crates/nu-cmd-base/src/util.rs +++ b/crates/nu-cmd-base/src/util.rs @@ -1,3 +1,4 @@ +use nu_path::AbsolutePathBuf; use nu_protocol::{ engine::{EngineState, Stack}, Range, ShellError, Span, Value, @@ -15,6 +16,7 @@ pub fn get_init_cwd() -> PathBuf { pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf { engine_state .cwd(Some(stack)) + .map(AbsolutePathBuf::into_std_path_buf) .unwrap_or(crate::util::get_init_cwd()) } diff --git a/crates/nu-command/src/env/config/config_env.rs b/crates/nu-command/src/env/config/config_env.rs index 32bd7bba90..777b6f3aac 100644 --- a/crates/nu-command/src/env/config/config_env.rs +++ b/crates/nu-command/src/env/config/config_env.rs @@ -60,12 +60,13 @@ impl Command for ConfigEnv { let (editor_name, editor_args) = get_editor(engine_state, stack, call.head)?; let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; let cwd = engine_state.cwd(Some(stack))?; - let editor_executable = - crate::which(&editor_name, &paths, &cwd).ok_or(ShellError::ExternalCommand { + let editor_executable = crate::which(&editor_name, &paths, cwd.as_ref()).ok_or( + ShellError::ExternalCommand { label: format!("`{editor_name}` not found"), help: "Failed to find the editor executable".into(), span: call.head, - })?; + }, + )?; let Some(env_path) = engine_state.get_config_path("env-path") else { return Err(ShellError::GenericError { diff --git a/crates/nu-command/src/env/config/config_nu.rs b/crates/nu-command/src/env/config/config_nu.rs index 08695ca5bb..80f0bbc012 100644 --- a/crates/nu-command/src/env/config/config_nu.rs +++ b/crates/nu-command/src/env/config/config_nu.rs @@ -64,12 +64,13 @@ impl Command for ConfigNu { let (editor_name, editor_args) = get_editor(engine_state, stack, call.head)?; let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; let cwd = engine_state.cwd(Some(stack))?; - let editor_executable = - crate::which(&editor_name, &paths, &cwd).ok_or(ShellError::ExternalCommand { + let editor_executable = crate::which(&editor_name, &paths, cwd.as_ref()).ok_or( + ShellError::ExternalCommand { label: format!("`{editor_name}` not found"), help: "Failed to find the editor executable".into(), span: call.head, - })?; + }, + )?; let Some(config_path) = engine_state.get_config_path("config-path") else { return Err(ShellError::GenericError { diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 4e09d900d2..23faf6f67b 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -1,5 +1,6 @@ use nu_cmd_base::util::get_init_cwd; use nu_engine::command_prelude::*; +use nu_path::AbsolutePathBuf; use nu_utils::filesystem::{have_permission, PermissionResult}; #[derive(Clone)] @@ -43,7 +44,10 @@ impl Command for Cd { // If getting PWD failed, default to the initial directory. This way, the // user can use `cd` to recover PWD to a good state. - let cwd = engine_state.cwd(Some(stack)).unwrap_or(get_init_cwd()); + let cwd = engine_state + .cwd(Some(stack)) + .map(AbsolutePathBuf::into_std_path_buf) + .unwrap_or(get_init_cwd()); let path_val = { if let Some(path) = path_val { diff --git a/crates/nu-command/src/path/type.rs b/crates/nu-command/src/path/type.rs index bf66c8cb3b..010fd295ad 100644 --- a/crates/nu-command/src/path/type.rs +++ b/crates/nu-command/src/path/type.rs @@ -1,13 +1,11 @@ use super::PathSubcommandArguments; use nu_engine::command_prelude::*; +use nu_path::AbsolutePathBuf; use nu_protocol::engine::StateWorkingSet; -use std::{ - io, - path::{Path, PathBuf}, -}; +use std::{io, path::Path}; struct Arguments { - pwd: PathBuf, + pwd: AbsolutePathBuf, } impl PathSubcommandArguments for Arguments {} diff --git a/crates/nu-command/src/system/exec.rs b/crates/nu-command/src/system/exec.rs index 35ab9428be..018017cfb2 100644 --- a/crates/nu-command/src/system/exec.rs +++ b/crates/nu-command/src/system/exec.rs @@ -39,7 +39,7 @@ On Windows based systems, Nushell will wait for the command to finish and then e let name: Spanned = call.req(engine_state, stack, 0)?; let executable = { let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; - let Some(executable) = crate::which(&name.item, &paths, &cwd) else { + let Some(executable) = crate::which(&name.item, &paths, cwd.as_ref()) else { return Err(crate::command_not_found( &name.item, call.head, diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index e475833747..06bc5a69ca 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -79,7 +79,7 @@ impl Command for External { // Determine the PATH to be used and then use `which` to find it - though this has no // effect if it's an absolute path already let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; - let Some(executable) = which(expanded_name, &paths, &cwd) else { + let Some(executable) = which(expanded_name, &paths, cwd.as_ref()) else { return Err(command_not_found(&name_str, call.head, engine_state, stack)); }; executable @@ -228,7 +228,7 @@ pub fn eval_arguments_from_call( match arg { // Expand globs passed to run-external Value::Glob { val, no_expand, .. } if !no_expand => args.extend( - expand_glob(&val, &cwd, expr.span, engine_state.signals())? + expand_glob(&val, cwd.as_ref(), expr.span, engine_state.signals())? .into_iter() .map(|s| s.into_spanned(expr.span)), ), diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 8b39bf5649..cd73c1ca71 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -83,7 +83,7 @@ prints out the list properly."# separator_param, env_str, use_grid_icons, - &cwd, + cwd.as_ref(), )?) } else { Ok(PipelineData::empty()) @@ -101,7 +101,7 @@ prints out the list properly."# separator_param, env_str, use_grid_icons, - &cwd, + cwd.as_ref(), )?) } else { // dbg!(data); @@ -124,7 +124,7 @@ prints out the list properly."# separator_param, env_str, use_grid_icons, - &cwd, + cwd.as_ref(), )?) } x => { diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 1460dec464..b495589011 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1,6 +1,6 @@ #[allow(deprecated)] use crate::{current_dir, get_config, get_full_help}; -use nu_path::expand_path_with; +use nu_path::{expand_path_with, AbsolutePathBuf}; use nu_protocol::{ ast::{ Assignment, Block, Call, Expr, Expression, ExternalArgument, PathMember, PipelineElement, @@ -679,7 +679,10 @@ impl Eval for EvalRuntime { } else if quoted { Ok(Value::string(path, span)) } else { - let cwd = engine_state.cwd(Some(stack)).unwrap_or_default(); + let cwd = engine_state + .cwd(Some(stack)) + .map(AbsolutePathBuf::into_std_path_buf) + .unwrap_or_default(); let path = expand_path_with(path, cwd, true); Ok(Value::string(path.to_string_lossy(), span)) diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index e393d78d41..d45fbafb88 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -12,6 +12,7 @@ use crate::{ }; use fancy_regex::Regex; use lru::LruCache; +use nu_path::AbsolutePathBuf; use std::{ collections::HashMap, num::NonZeroUsize, @@ -922,58 +923,44 @@ impl EngineState { /// /// If `stack` is supplied, also considers modifications to the working /// directory on the stack that have yet to be merged into the engine state. - pub fn cwd(&self, stack: Option<&Stack>) -> Result { + pub fn cwd(&self, stack: Option<&Stack>) -> Result { // Helper function to create a simple generic error. - fn error(msg: &str, cwd: impl AsRef) -> Result { - Err(ShellError::GenericError { + fn error(msg: &str, cwd: impl AsRef) -> ShellError { + ShellError::GenericError { error: msg.into(), msg: format!("$env.PWD = {}", cwd.as_ref().display()), span: None, help: Some("Use `cd` to reset $env.PWD into a good state".into()), inner: vec![], - }) - } - - // Helper function to check if a path is a root path. - fn is_root(path: &Path) -> bool { - path.parent().is_none() - } - - // Helper function to check if a path has trailing slashes. - fn has_trailing_slash(path: &Path) -> bool { - nu_path::components(path).last() - == Some(std::path::Component::Normal(std::ffi::OsStr::new(""))) + } } // Retrieve $env.PWD from the stack or the engine state. let pwd = if let Some(stack) = stack { stack.get_env_var(self, "PWD") } else { - self.get_env_var("PWD").map(ToOwned::to_owned) + self.get_env_var("PWD").cloned() }; - if let Some(pwd) = pwd { - if let Value::String { val, .. } = pwd { - let path = PathBuf::from(val); + let pwd = pwd.ok_or_else(|| error("$env.PWD not found", ""))?; - // Technically, a root path counts as "having trailing slashes", but - // for the purpose of PWD, a root path is acceptable. - if !is_root(&path) && has_trailing_slash(&path) { - error("$env.PWD contains trailing slashes", path) - } else if !path.is_absolute() { - error("$env.PWD is not an absolute path", path) - } else if !path.exists() { - error("$env.PWD points to a non-existent directory", path) - } else if !path.is_dir() { - error("$env.PWD points to a non-directory", path) - } else { - Ok(path) - } + if let Value::String { val, .. } = pwd { + let path = AbsolutePathBuf::try_from(val) + .map_err(|path| error("$env.PWD is not an absolute path", path))?; + + // Technically, a root path counts as "having trailing slashes", but + // for the purpose of PWD, a root path is acceptable. + if path.parent().is_some() && nu_path::has_trailing_slash(path.as_ref()) { + Err(error("$env.PWD contains trailing slashes", &path)) + } else if !path.exists() { + Err(error("$env.PWD points to a non-existent directory", &path)) + } else if !path.is_dir() { + Err(error("$env.PWD points to a non-directory", &path)) } else { - error("$env.PWD is not a string", format!("{pwd:?}")) + Ok(path) } } else { - error("$env.PWD not found", "") + Err(error("$env.PWD is not a string", format!("{pwd:?}"))) } }