From e2c1216c1bd0d99bc5a1a26bc8a41256b2e4fcfd Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 13 Dec 2022 22:11:55 +1000 Subject: [PATCH] Fix `du` error message (#7460) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BEFORE: ``` 〉du *.*** Error: × wildcards are either regular `*` or recursive `**` ╭─[entry #6:1:1] 1 │ du *.*** · ─┬ · ╰── glob error ╰──── ``` AFTER: ``` 〉du *.*** Error: × glob error ╭─[entry #8:1:1] 1 │ du *.*** · ──┬── · ╰── wildcards are either regular `*` or recursive `**` ╰──── ``` --- crates/nu-command/src/platform/du.rs | 31 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/nu-command/src/platform/du.rs b/crates/nu-command/src/platform/du.rs index a5a1640d39..681764f695 100644 --- a/crates/nu-command/src/platform/du.rs +++ b/crates/nu-command/src/platform/du.rs @@ -97,8 +97,8 @@ impl Command for Du { let exclude = args.exclude.map_or(Ok(None), move |x| { Pattern::new(&x.item).map(Some).map_err(|e| { ShellError::GenericError( - e.msg.to_string(), "glob error".to_string(), + e.msg.to_string(), Some(x.span), None, Vec::new(), @@ -109,20 +109,25 @@ impl Command for Du { let include_files = args.all; let mut paths = match args.path { Some(p) => { - let p = p.item.to_str().expect("Why isn't this encoded properly?"); - nu_glob::glob_with(p, GLOB_PARAMS) + let item = p.item.to_str().expect("Why isn't this encoded properly?"); + match nu_glob::glob_with(item, GLOB_PARAMS) { + // Convert the PatternError to a ShellError, preserving the span + // of the inputted glob. + Err(e) => { + return Err(ShellError::GenericError( + "glob error".to_string(), + e.msg.to_string(), + Some(p.span), + None, + Vec::new(), + )) + } + Ok(path) => path, + } } - None => nu_glob::glob_with("*", GLOB_PARAMS), + // The * pattern should never fail. + None => nu_glob::glob_with("*", GLOB_PARAMS).expect("du: * pattern failed to glob"), } - .map_err(|e| { - ShellError::GenericError( - e.msg.to_string(), - "glob error".to_string(), - Some(tag), - None, - Vec::new(), - ) - })? .filter(move |p| { if include_files { true