diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 5ebdb60c0..c31b600f2 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -1,4 +1,3 @@ -use nu_engine::env::current_dir_str; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -32,13 +31,7 @@ impl Command for Cd { let path_val: Option = call.opt(engine_state, stack, 0)?; let (path, span) = match path_val { - Some(v) => { - let path = nu_path::canonicalize_with( - v.as_string()?, - current_dir_str(engine_state, stack)?, - )?; - (path.to_string_lossy().to_string(), v.span()?) - } + Some(v) => (v.as_string()?, v.span()?), None => { let path = nu_path::expand_tilde("~"); (path.to_string_lossy().to_string(), call.head) diff --git a/crates/nu-command/src/filesystem/touch.rs b/crates/nu-command/src/filesystem/touch.rs index 21c6b9b49..ece811c97 100644 --- a/crates/nu-command/src/filesystem/touch.rs +++ b/crates/nu-command/src/filesystem/touch.rs @@ -1,8 +1,6 @@ use std::fs::OpenOptions; -use nu_engine::env::current_dir_str; use nu_engine::CallExt; -use nu_path::expand_path_with; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape}; @@ -41,8 +39,7 @@ impl Command for Touch { let rest: Vec = call.rest(engine_state, stack, 1)?; for (index, item) in vec![target].into_iter().chain(rest).enumerate() { - let path = expand_path_with(&item, current_dir_str(engine_state, stack)?); - match OpenOptions::new().write(true).create(true).open(&path) { + match OpenOptions::new().write(true).create(true).open(&item) { Ok(_) => continue, Err(err) => { return Err(ShellError::CreateNotPossible( diff --git a/crates/nu-command/src/shells/enter.rs b/crates/nu-command/src/shells/enter.rs index ee6ae7649..4f95da7a0 100644 --- a/crates/nu-command/src/shells/enter.rs +++ b/crates/nu-command/src/shells/enter.rs @@ -37,9 +37,8 @@ impl Command for Enter { let cwd = current_dir(engine_state, stack)?; - if let Ok(s) = new_path.as_string() { - let path = nu_path::expand_path_with(s, &cwd); - if !path.exists() { + if let Ok(s) = new_path.as_path() { + if !s.exists() { return Err(ShellError::DirectoryNotFound(new_path.span()?)); } } diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 5d75bea00..0b17f766b 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -18,6 +18,7 @@ use sys_locale::get_locale; pub use unit::*; use std::collections::HashMap; +use std::path::PathBuf; use std::{cmp::Ordering, fmt::Debug}; use crate::ast::{CellPath, PathMember}; @@ -173,6 +174,17 @@ impl Value { } } + pub fn as_path(&self) -> Result { + match self { + Value::String { val, .. } => Ok(PathBuf::from(val)), + x => Err(ShellError::CantConvert( + "path".into(), + x.get_type().to_string(), + self.span()?, + )), + } + } + pub fn as_block(&self) -> Result { match self { Value::Block { val, .. } => Ok(*val), diff --git a/src/main.rs b/src/main.rs index 722a188b6..1fe2d1f33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -468,8 +468,11 @@ fn main() -> Result<()> { } // FIXME: permanent state changes like this hopefully in time can be removed // and be replaced by just passing the cwd in where needed - let cwd = nu_engine::env::current_dir_str(&engine_state, &stack)?; - let _ = std::env::set_current_dir(cwd); + if let Some(cwd) = stack.get_env_var(&engine_state, "PWD") { + let path = cwd.as_string()?; + let _ = std::env::set_current_dir(path); + engine_state.env_vars.insert("PWD".into(), cwd); + } } Ok(Signal::CtrlC) => { // `Reedline` clears the line content. New prompt is shown