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
This commit is contained in:
Stefan Stanciulescu 2022-01-23 14:02:12 +01:00 committed by GitHub
parent be0d221d56
commit 4e171203cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -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()?,

View File

@ -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),