From 944d941dec0459a910a6cbf0f6f60688e94554e6 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Mon, 10 Jun 2024 21:40:09 +0000 Subject: [PATCH] `path type` error and not found changes (#13007) # Description Instead of an empty string, this PR changes `path type` to return null if the path does not exist. If some other IO error is encountered, then that error is bubbled up instead of treating it as a "not found" case. # User-Facing Changes - `path type` will now return null instead of an empty string, which is technically a breaking change. In most cases though, I think this shouldn't affect the behavior of scripts too much. - `path type` can now error instead of returning an empty string if some other IO error besides a "not found" error occurs. Since this PR introduces breaking changes, it should be merged after the 0.94.1 patch. --- crates/nu-command/src/path/type.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/path/type.rs b/crates/nu-command/src/path/type.rs index 0897593109..e58e697604 100644 --- a/crates/nu-command/src/path/type.rs +++ b/crates/nu-command/src/path/type.rs @@ -1,7 +1,10 @@ use super::PathSubcommandArguments; use nu_engine::command_prelude::*; use nu_protocol::engine::StateWorkingSet; -use std::path::{Path, PathBuf}; +use std::{ + io, + path::{Path, PathBuf}, +}; struct Arguments { pwd: PathBuf, @@ -36,7 +39,7 @@ impl Command for SubCommand { fn extra_usage(&self) -> &str { r#"This checks the file system to confirm the path's object type. -If nothing is found, an empty string will be returned."# +If the path does not exist, null will be returned."# } fn is_const(&self) -> bool { @@ -104,9 +107,11 @@ If nothing is found, an empty string will be returned."# fn path_type(path: &Path, span: Span, args: &Arguments) -> Value { let path = nu_path::expand_path_with(path, &args.pwd, true); - let meta = path.symlink_metadata(); - let ty = meta.as_ref().map(get_file_type).unwrap_or(""); - Value::string(ty, span) + match path.symlink_metadata() { + Ok(metadata) => Value::string(get_file_type(&metadata), span), + Err(err) if err.kind() == io::ErrorKind::NotFound => Value::nothing(span), + Err(err) => Value::error(err.into_spanned(span).into(), span), + } } fn get_file_type(md: &std::fs::Metadata) -> &str {