ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)

# Description
Fixes:  #11887
Fixes: #11626

This pr unify the tilde expand behavior over several filesystem relative
commands. It follows the same rule with glob expansion:
|  command  |  result |
| ----------- |  ------ |
| ls ~/aaa  | expand tilde
| ls "~/aaa"  | don't expand tilde
| let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls
($f \| path expand)`
| let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on
`mkdir`, `touch` comamnd.

Actually I'm not sure for 4th item, currently it's expanding is just
because it followes the same rule with glob expansion.

### About the change
It changes `expand_path_with` to accept a new argument called
`expand_tilde`, if it's true, expand it, if not, just keep it as `~`
itself.

# User-Facing Changes
After this change, `ls "~/aaa"` won't expand tilde.

# Tests + Formatting
Done
This commit is contained in:
Wind
2024-03-25 10:08:38 +08:00
committed by GitHub
parent e7bdd08a04
commit 87c5f6e455
22 changed files with 272 additions and 72 deletions

View File

@ -137,7 +137,7 @@ fn exists(path: &Path, span: Span, args: &Arguments) -> Value {
if path.as_os_str().is_empty() {
return Value::bool(false, span);
}
let path = expand_path_with(path, &args.pwd);
let path = expand_path_with(path, &args.pwd, true);
let exists = if args.not_follow_symlink {
// symlink_metadata returns true if the file/folder exists
// whether it is a symbolic link or not. Sorry, but returns Err

View File

@ -152,7 +152,10 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
match canonicalize_with(path, &args.cwd) {
Ok(p) => {
if args.not_follow_symlink {
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
Value::string(
expand_path_with(path, &args.cwd, true).to_string_lossy(),
span,
)
} else {
Value::string(p.to_string_lossy(), span)
}
@ -171,12 +174,18 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
),
}
} else if args.not_follow_symlink {
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
Value::string(
expand_path_with(path, &args.cwd, true).to_string_lossy(),
span,
)
} else {
canonicalize_with(path, &args.cwd)
.map(|p| Value::string(p.to_string_lossy(), span))
.unwrap_or_else(|_| {
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
Value::string(
expand_path_with(path, &args.cwd, true).to_string_lossy(),
span,
)
})
}
}