From 4e171203cc047771caee21d28c4444bfe654ceef Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu <71919805+onthebridgetonowhere@users.noreply.github.com> Date: Sun, 23 Jan 2022 14:02:12 +0100 Subject: [PATCH] Fix cd-ing into a file (#831) * Add custom error for path not being a directory * Fix cd issue with cd-ing into a file * Keep formatting style as before * Check if path is not a directory and return error if that's the case --- crates/nu-command/src/filesystem/cd.rs | 8 +++++++- crates/nu-protocol/src/shell_error.rs | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 2ad57b505..2ad032db8 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -61,7 +61,13 @@ impl Command for Cd { Some(v) => { let path = v.as_path()?; let path = match nu_path::canonicalize_with(path, &cwd) { - Ok(p) => p, + Ok(p) => { + if !p.is_dir() { + return Err(ShellError::NotADirectory(v.span()?)); + } + p + } + Err(e) => { return Err(ShellError::DirectoryNotFoundHelp( v.span()?, diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index af163fa0a..c3b46becb 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -185,6 +185,10 @@ pub enum ShellError { #[diagnostic(code(nu::shell::io_error), url(docsrs), help("{0}"))] IOError(String), + #[error("Cannot change to directory")] + #[diagnostic(code(nu::shell::cannot_cd_to_directory), url(docsrs))] + NotADirectory(#[label("is not a directory")] Span), + #[error("Directory not found")] #[diagnostic(code(nu::shell::directory_not_found), url(docsrs))] DirectoryNotFound(#[label("directory not found")] Span),