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.
This commit is contained in:
Darren Schroeder 2023-03-29 13:23:58 -05:00 committed by GitHub
parent 97e7d550c8
commit a49e5b30ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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