From 87c684c7daf06882ddf10a5668f826646a012d93 Mon Sep 17 00:00:00 2001 From: nicole mazzuca Date: Wed, 13 Apr 2022 11:42:57 -0700 Subject: [PATCH] don't join paths to cwd ever in calls to external functions (#5180) This is a follow-up to #5131, since I don't personally like the way it worked. --- crates/nu-command/src/system/run_external.rs | 25 +++++--------------- crates/nu-path/src/expansions.rs | 13 ++++++++++ crates/nu-path/src/lib.rs | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index f37e12794..a979721b6 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -397,25 +397,10 @@ impl ExternalCommand { /// Spawn a command without shelling out to an external shell pub fn spawn_simple_command(&self, cwd: &str) -> Result { - 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 { - arg - } - }; - let head = trim_enclosing_quotes(&self.name.item); - let head = expand_path(head); + let head = nu_path::expand_path_for_external_programs(head) + .to_string_lossy() + .to_string(); let mut process = std::process::Command::new(&head); @@ -424,7 +409,9 @@ impl ExternalCommand { item: trim_enclosing_quotes(&arg.item), span: arg.span, }; - arg.item = expand_path(arg.item); + arg.item = nu_path::expand_path_for_external_programs(arg.item) + .to_string_lossy() + .to_string(); let cwd = PathBuf::from(cwd); diff --git a/crates/nu-path/src/expansions.rs b/crates/nu-path/src/expansions.rs index f23b6197f..96eb70036 100644 --- a/crates/nu-path/src/expansions.rs +++ b/crates/nu-path/src/expansions.rs @@ -73,3 +73,16 @@ where expand_path(path) } + +/// Resolve similarly to other shells - tilde is expanded, and ndot path components are expanded. +/// +/// This function will take a leading tilde path component, and expand it to the user's home directory; +/// it will also expand any path elements consisting of only dots into the correct number of `..` path elements. +/// It does not touch the system at all, except for getting the home directory of the current user. +pub fn expand_path_for_external_programs

(path: P) -> PathBuf +where + P: AsRef, +{ + let path = expand_tilde(path); + expand_ndots(path) +} diff --git a/crates/nu-path/src/lib.rs b/crates/nu-path/src/lib.rs index e6fbf4e6a..b389b2e12 100644 --- a/crates/nu-path/src/lib.rs +++ b/crates/nu-path/src/lib.rs @@ -4,7 +4,7 @@ mod helpers; mod tilde; mod util; -pub use expansions::{canonicalize_with, expand_path_with}; +pub use expansions::{canonicalize_with, expand_path_for_external_programs, expand_path_with}; pub use helpers::{config_dir, home_dir}; pub use tilde::expand_tilde; pub use util::trim_trailing_slash;