Don't expanding globs if user pass variables. (#11946)

# Description
Fixes: #11912

# User-Facing Changes
After this change: 
```
let x = '*.nu'; ^echo $x
```
will no longer expand glob.
If users still want to expand glob, there are also 3 ways to do this:
```
# 1. use spread operation with `glob` command
let x = '*.nu'; ^echo ...(glob $x)
```
# Tests + Formatting
Done

# After Submitting
NaN
This commit is contained in:
Wind 2024-02-28 23:05:09 +08:00 committed by GitHub
parent c6cb406a53
commit eaedb30a8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -711,9 +711,11 @@ fn trim_expand_and_apply_arg(
// if arg is quoted, like "aa", 'aa', `aa`, or:
// if arg is a variable or String interpolation, like: $variable_name, $"($variable_name)"
// `as_a_whole` will be true, so nu won't remove the inner quotes.
let (trimmed_args, run_glob_expansion, mut keep_raw) = trim_enclosing_quotes(&arg.item);
let (trimmed_args, mut run_glob_expansion, mut keep_raw) = trim_enclosing_quotes(&arg.item);
if *arg_keep_raw {
keep_raw = true;
// it's a list or a variable, don't run glob expansion either
run_glob_expansion = false;
}
let mut arg = Spanned {
item: if keep_raw {

View file

@ -1,4 +1,6 @@
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use pretty_assertions::assert_eq;
#[cfg(feature = "which-support")]
@ -166,6 +168,35 @@ fn err_pipe_with_failed_external_works() {
assert_eq!(actual.out, "4");
}
#[test]
fn dont_run_glob_if_pass_variable_to_external() {
Playground::setup("dont_run_glob", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
]);
let actual = nu!(cwd: dirs.test(), r#"let f = "*.txt"; nu --testbin nonu $f"#);
assert_eq!(actual.out, "*.txt");
})
}
#[test]
fn run_glob_if_pass_variable_to_external() {
Playground::setup("run_glob_on_external", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
]);
let actual = nu!(cwd: dirs.test(), r#"let f = "*.txt"; nu --testbin nonu ...(glob $f)"#);
assert!(actual.out.contains("jt_likes_cake.txt"));
assert!(actual.out.contains("andres_likes_arepas.txt"));
})
}
mod it_evaluation {
use super::nu;
use nu_test_support::fs::Stub::{EmptyFile, FileWithContent, FileWithContentToBeTrimmed};