I don't personally agree with this; I'd prefer less magic,
and not expanding _anything_ except `~` as an initial path element

Co-authored-by: nicole mazzuca <mazzucan@outlook.com>
This commit is contained in:
nicole mazzuca 2022-04-11 01:05:39 -07:00 committed by GitHub
parent a30930324d
commit 521e28dcdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -397,15 +397,26 @@ impl ExternalCommand {
/// Spawn a command without shelling out to an external shell
pub fn spawn_simple_command(&self, cwd: &str) -> Result<std::process::Command, ShellError> {
let head = trim_enclosing_quotes(&self.name.item);
let head = if head.starts_with('~') || head.starts_with("..") {
nu_path::expand_path_with(head, cwd)
let is_path = |arg: &str| {
let head = match arg.split_once(::std::path::is_separator) {
Some((x, _)) => x,
None => arg,
};
head == "~" || head.chars().all(|ch| ch == '.')
};
let expand_path = |arg: String| {
if is_path(&arg) {
nu_path::expand_path_with(arg, cwd)
.to_string_lossy()
.to_string()
} else {
head
arg
}
};
let head = trim_enclosing_quotes(&self.name.item);
let head = expand_path(head);
let mut process = std::process::Command::new(&head);
for arg in self.args.iter() {
@ -413,13 +424,7 @@ impl ExternalCommand {
item: trim_enclosing_quotes(&arg.item),
span: arg.span,
};
arg.item = if arg.item.starts_with('~') || arg.item.starts_with("..") {
nu_path::expand_path_with(&arg.item, cwd)
.to_string_lossy()
.to_string()
} else {
arg.item
};
arg.item = expand_path(arg.item);
let cwd = PathBuf::from(cwd);