mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 04:15:08 +02:00
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:
@ -328,7 +328,7 @@ fn eval_redirection<D: DebugContext>(
|
||||
let cwd = current_dir(engine_state, stack)?;
|
||||
let value = eval_expression::<D>(engine_state, stack, expr)?;
|
||||
let path = Spanned::<PathBuf>::from_value(value)?.item;
|
||||
let path = expand_path_with(path, cwd);
|
||||
let path = expand_path_with(path, cwd, true);
|
||||
|
||||
let mut options = OpenOptions::new();
|
||||
if *append {
|
||||
@ -634,7 +634,7 @@ impl Eval for EvalRuntime {
|
||||
Ok(Value::string(path, span))
|
||||
} else {
|
||||
let cwd = current_dir_str(engine_state, stack)?;
|
||||
let path = expand_path_with(path, cwd);
|
||||
let path = expand_path_with(path, cwd, true);
|
||||
|
||||
Ok(Value::string(path.to_string_lossy(), span))
|
||||
}
|
||||
@ -653,7 +653,7 @@ impl Eval for EvalRuntime {
|
||||
Ok(Value::string(path, span))
|
||||
} else {
|
||||
let cwd = current_dir_str(engine_state, stack)?;
|
||||
let path = expand_path_with(path, cwd);
|
||||
let path = expand_path_with(path, cwd, true);
|
||||
|
||||
Ok(Value::string(path.to_string_lossy(), span))
|
||||
}
|
||||
|
@ -58,13 +58,13 @@ pub fn glob_from(
|
||||
}
|
||||
|
||||
// Now expand `p` to get full prefix
|
||||
let path = expand_path_with(p, cwd);
|
||||
let path = expand_path_with(p, cwd, pattern.item.is_expand());
|
||||
let escaped_prefix = PathBuf::from(nu_glob::Pattern::escape(&path.to_string_lossy()));
|
||||
|
||||
(Some(path), escaped_prefix.join(just_pattern))
|
||||
} else {
|
||||
let path = PathBuf::from(&pattern.item.as_ref());
|
||||
let path = expand_path_with(path, cwd);
|
||||
let path = expand_path_with(path, cwd, pattern.item.is_expand());
|
||||
let is_symlink = match fs::symlink_metadata(&path) {
|
||||
Ok(attr) => attr.file_type().is_symlink(),
|
||||
Err(_) => false,
|
||||
|
Reference in New Issue
Block a user