Do not propagate glob creation error for external args (#12955)

# Description
Instead of returning an error, this PR changes `expand_glob` in
`run_external.rs` to return the original string arg if glob creation
failed. This makes it so that, e.g.,
```nushell
^echo `[`
^echo `***`
```
no longer fail with a shell error. (This follows from #12921.)
This commit is contained in:
Ian Manske 2024-05-25 00:59:36 +00:00 committed by GitHub
parent c5d716951f
commit 95977faf2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -295,19 +295,15 @@ fn expand_tilde(arg: &str) -> String {
nu_path::expand_tilde(arg).to_string_lossy().to_string() nu_path::expand_tilde(arg).to_string_lossy().to_string()
} }
/// Performs glob expansion on `arg`. If the expansion found no matches, returns /// Performs glob expansion on `arg`. If the expansion found no matches or the pattern
/// the original string as the expansion result. /// is not a valid glob, then this returns the original string as the expansion result.
/// ///
/// Note: This matches the default behavior of Bash, but is known to be /// Note: This matches the default behavior of Bash, but is known to be
/// error-prone. We might want to change this behavior in the future. /// error-prone. We might want to change this behavior in the future.
fn expand_glob(arg: &str, cwd: &Path, span: Span) -> Result<Vec<String>, ShellError> { fn expand_glob(arg: &str, cwd: &Path, span: Span) -> Result<Vec<String>, ShellError> {
let paths = let Ok(paths) = nu_glob::glob_with_parent(arg, nu_glob::MatchOptions::default(), cwd) else {
nu_glob::glob_with_parent(arg, nu_glob::MatchOptions::default(), cwd).map_err(|err| { return Ok(vec![arg.into()]);
ShellError::InvalidGlobPattern { };
msg: err.msg.to_string(),
span,
}
})?;
let mut result = vec![]; let mut result = vec![];
for path in paths { for path in paths {
@ -647,7 +643,9 @@ mod test {
let expected = &["'*.txt'"]; let expected = &["'*.txt'"];
assert_eq!(actual, expected); assert_eq!(actual, expected);
expand_glob("[*.txt", cwd, Span::unknown()).unwrap_err(); let actual = expand_glob("[*.txt", cwd, Span::unknown()).unwrap();
let expected = &["[*.txt"];
assert_eq!(actual, expected);
} }
#[test] #[test]