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(), }) }