Canonicalize config dir (#12136)

It turns out that my previous PR,
https://github.com/nushell/nushell/pull/11999, didn't properly
canonicalize `$nu.default-config-dir` in a scenario where
`XDG_CONFIG_HOME` (or the equivalent on each platform) was a symlink. To
remedy that, this PR makes `nu_path::config_dir()` return a
canonicalized path. This probably shouldn't break anything (except maybe
tests relying on the old behavior), since the canonical path will be
equivalent to non-canonical paths.

# User-Facing Changes

A user may get a path with symlinks resolved and `..`s replaced where
they previously didn't. I'm not sure where this would happen, though,
and anyway, the canonical path is probably the "correct" thing to
present to the user. We're using `omnipath` to make the path presentable
to the user on Windows, so there's no danger of someone getting an path
with `\\?` there.

# Tests + Formatting

The tests for config files have been updated to run the binary using the
`Director` so that it has access to the `XDG_CONFIG_HOME`/`HOME`
environment variables to be able to change the config directory.
This commit is contained in:
Yash Thakur
2024-03-10 06:07:31 -04:00
committed by GitHub
parent 1d14d29408
commit a7b281292d
3 changed files with 49 additions and 34 deletions

View File

@ -71,8 +71,7 @@ pub fn add_plugin_file(
let e = ParseError::FileNotFound(plugin_file.item, plugin_file.span);
report_error(&working_set, &e);
}
} else if let Some(plugin_path) = nu_path::config_dir() {
let mut plugin_path = canonicalize_with(&plugin_path, cwd).unwrap_or(plugin_path);
} else if let Some(mut plugin_path) = nu_path::config_dir() {
// Path to store plugins signatures
plugin_path.push(storage_path);
plugin_path.push(PLUGIN_FILE);

View File

@ -7,7 +7,7 @@ pub fn home_dir() -> Option<PathBuf> {
}
pub fn config_dir() -> Option<PathBuf> {
dirs_next::config_dir()
dirs_next::config_dir().map(|path| canonicalize(&path).unwrap_or(path))
}
#[cfg(windows)]