From a49e5b30ff5846b2cc88eaa9c210b1162872138e Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 29 Mar 2023 13:23:58 -0500 Subject: [PATCH] auto-expand paths in the `$nu` variable (#8653) # Description I recently ran into an issue where one of the $nu paths was not expanded and was causing issue because $env.PWD did not equal $nu.temp-path, even though I was in $nu.temp-path. The reason it didn't match was because my temp path was symlinked. This PR fixes that issue by expanding the paths with canonicalize_with(). # User-Facing Changes # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- crates/nu-engine/src/nu_variable.rs | 38 ++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/nu-engine/src/nu_variable.rs b/crates/nu-engine/src/nu_variable.rs index ec39413794..e1848dc05b 100644 --- a/crates/nu-engine/src/nu_variable.rs +++ b/crates/nu-engine/src/nu_variable.rs @@ -5,6 +5,7 @@ use nu_protocol::{ LazyRecord, ShellError, Span, Value, }; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use sysinfo::SystemExt; // NuVariable: a LazyRecord for the special $nu variable @@ -53,11 +54,25 @@ impl LazyRecord for NuVariable { }) }; + fn canonicalize_path(engine_state: &EngineState, path: &PathBuf) -> PathBuf { + let cwd = engine_state.current_work_dir(); + + if path.exists() { + match nu_path::canonicalize_with(path, cwd) { + Ok(canon_path) => canon_path, + Err(_) => path.clone(), + } + } else { + path.clone() + } + } + match column { "config-path" => { if let Some(path) = self.engine_state.get_config_path("config-path") { + let canon_config_path = canonicalize_path(&self.engine_state, path); Ok(Value::String { - val: path.to_string_lossy().to_string(), + val: canon_config_path.to_string_lossy().to_string(), span: self.span, }) } else if let Some(mut path) = nu_path::config_dir() { @@ -73,8 +88,9 @@ impl LazyRecord for NuVariable { } "env-path" => { if let Some(path) = self.engine_state.get_config_path("env-path") { + let canon_env_path = canonicalize_path(&self.engine_state, path); Ok(Value::String { - val: path.to_string_lossy().to_string(), + val: canon_env_path.to_string_lossy().to_string(), span: self.span, }) } else if let Some(mut path) = nu_path::config_dir() { @@ -99,8 +115,9 @@ impl LazyRecord for NuVariable { path.push("history.txt"); } } + let canon_hist_path = canonicalize_path(&self.engine_state, &path); Ok(Value::String { - val: path.to_string_lossy().to_string(), + val: canon_hist_path.to_string_lossy().to_string(), span: self.span, }) } else { @@ -111,8 +128,9 @@ impl LazyRecord for NuVariable { if let Some(mut path) = nu_path::config_dir() { path.push("nushell"); path.push("login.nu"); + let canon_login_path = canonicalize_path(&self.engine_state, &path); Ok(Value::String { - val: path.to_string_lossy().to_string(), + val: canon_login_path.to_string_lossy().to_string(), span: self.span, }) } else { @@ -123,8 +141,9 @@ impl LazyRecord for NuVariable { #[cfg(feature = "plugin")] { if let Some(path) = &self.engine_state.plugin_signatures { + let canon_plugin_path = canonicalize_path(&self.engine_state, path); Ok(Value::String { - val: path.to_string_lossy().to_string(), + val: canon_plugin_path.to_string_lossy().to_string(), span: self.span, }) } else { @@ -139,9 +158,10 @@ impl LazyRecord for NuVariable { } "scope" => Ok(create_scope(&self.engine_state, &self.stack, self.span())?), "home-path" => { - if let Some(home_path) = nu_path::home_dir() { + if let Some(path) = nu_path::home_dir() { + let canon_home_path = canonicalize_path(&self.engine_state, &path); Ok(Value::String { - val: home_path.to_string_lossy().into(), + val: canon_home_path.to_string_lossy().into(), span: self.span(), }) } else { @@ -149,9 +169,9 @@ impl LazyRecord for NuVariable { } } "temp-path" => { - let temp_path = std::env::temp_dir(); + let canon_temp_path = canonicalize_path(&self.engine_state, &std::env::temp_dir()); Ok(Value::String { - val: temp_path.to_string_lossy().into(), + val: canon_temp_path.to_string_lossy().into(), span: self.span(), }) }