From cc78446ffde7948b7406c58616fccb9889ed0285 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 23 Apr 2022 11:48:10 +1200 Subject: [PATCH] Fix cd - (#5301) --- crates/nu-command/src/filesystem/cd.rs | 67 +++++++++++--------------- crates/nu-engine/src/eval.rs | 19 +++++--- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index fe753e0cca..4ca4a9dbbc 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -1,7 +1,9 @@ use nu_engine::{current_dir, CallExt}; -use nu_protocol::ast::{Call, Expr, Expression}; +use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value}; +use nu_protocol::{ + Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value, +}; #[derive(Clone)] pub struct Cd; @@ -28,17 +30,12 @@ impl Command for Cd { call: &Call, _input: PipelineData, ) -> Result { - let raw_path = call.positional_nth(0); - let path_val: Option = call.opt(engine_state, stack, 0)?; + let path_val: Option> = call.opt(engine_state, stack, 0)?; let cwd = current_dir(engine_state, stack)?; - let (path, span) = match raw_path { - Some(v) => match &v { - Expression { - expr: Expr::Filepath(val), - span, - .. - } if val == "-" => { + let (path, span) = match path_val { + Some(v) => { + if v.item == "-" { let oldpwd = stack.get_env_var(engine_state, "OLDPWD"); if let Some(oldpwd) = oldpwd { @@ -47,42 +44,34 @@ impl Command for Cd { Ok(p) => p, Err(e) => { return Err(ShellError::DirectoryNotFound( - *span, + v.span, Some(format!("IO Error: {:?}", e)), )) } }; - (path.to_string_lossy().to_string(), *span) + (path.to_string_lossy().to_string(), v.span) } else { - (cwd.to_string_lossy().to_string(), *span) + (cwd.to_string_lossy().to_string(), v.span) } - } - _ => match path_val { - Some(v) => { - let path = v.as_path()?; - let path = match nu_path::canonicalize_with(path, &cwd) { - Ok(p) => { - if !p.is_dir() { - return Err(ShellError::NotADirectory(v.span()?)); - } - p + } else { + let path = match nu_path::canonicalize_with(&v.item, &cwd) { + Ok(p) => { + if !p.is_dir() { + return Err(ShellError::NotADirectory(v.span)); } + p + } - Err(e) => { - return Err(ShellError::DirectoryNotFound( - v.span()?, - Some(format!("IO Error: {:?}", e)), - )) - } - }; - (path.to_string_lossy().to_string(), v.span()?) - } - None => { - let path = nu_path::expand_tilde("~"); - (path.to_string_lossy().to_string(), call.head) - } - }, - }, + Err(e) => { + return Err(ShellError::DirectoryNotFound( + v.span, + Some(format!("IO Error: {:?}", e)), + )) + } + }; + (path.to_string_lossy().to_string(), v.span) + } + } None => { let path = nu_path::expand_tilde("~"); (path.to_string_lossy().to_string(), call.head) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 8ac589debf..c04aace2d2 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -531,13 +531,20 @@ pub fn eval_expression( }) } Expr::Directory(s) => { - let cwd = current_dir_str(engine_state, stack)?; - let path = expand_path_with(s, cwd); + if s == "-" { + Ok(Value::String { + val: "-".to_string(), + span: expr.span, + }) + } else { + let cwd = current_dir_str(engine_state, stack)?; + let path = expand_path_with(s, cwd); - Ok(Value::String { - val: path.to_string_lossy().to_string(), - span: expr.span, - }) + Ok(Value::String { + val: path.to_string_lossy().to_string(), + span: expr.span, + }) + } } Expr::GlobPattern(s) => { let cwd = current_dir_str(engine_state, stack)?;