Add ConfigDirNotFound error (#11849)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.

This is what the error looks like:


![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **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.
-->

---------

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
This commit is contained in:
Yash Thakur 2024-02-26 02:42:20 -05:00 committed by GitHub
parent 2697ea9a25
commit c0ff0f12f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 102 additions and 98 deletions

View File

@ -131,10 +131,7 @@ impl Command for History {
}
}
} else {
Err(ShellError::FileNotFound {
file: "history file".into(),
span: head,
})
Err(ShellError::ConfigDirNotFound { span: Some(head) })
}
}

View File

@ -52,13 +52,7 @@ impl Command for ConfigReset {
let mut config_path = match nu_path::config_dir() {
Some(path) => path,
None => {
return Err(ShellError::GenericError {
error: "Could not find config path".into(),
msg: "Could not find config path".into(),
span: None,
help: None,
inner: vec![],
});
return Err(ShellError::ConfigDirNotFound { span: None });
}
};
config_path.push("nushell");

View File

@ -26,19 +26,23 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
let mut record = Record::new();
let config_path = match nu_path::config_dir() {
Some(mut path) => {
path.push("nushell");
Ok(path)
}
None => Err(Value::error(
ShellError::ConfigDirNotFound { span: Some(span) },
span,
)),
};
record.push(
"default-config-dir",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not get config directory".into(),
},
span,
)
},
config_path.as_ref().map_or_else(
|e| e.clone(),
|path| Value::string(path.to_string_lossy(), span),
),
);
record.push(
@ -46,16 +50,13 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
if let Some(path) = engine_state.get_config_path("config-path") {
let canon_config_path = canonicalize_path(engine_state, path);
Value::string(canon_config_path.to_string_lossy(), span)
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("config.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not get config directory".into(),
config_path.clone().map_or_else(
|e| e,
|mut path| {
path.push("config.nu");
Value::string(path.to_string_lossy(), span)
},
span,
)
},
);
@ -65,59 +66,46 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
if let Some(path) = engine_state.get_config_path("env-path") {
let canon_env_path = canonicalize_path(engine_state, path);
Value::string(canon_env_path.to_string_lossy(), span)
} else if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("env.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not find environment path".into(),
config_path.clone().map_or_else(
|e| e,
|mut path| {
path.push("env.nu");
Value::string(path.to_string_lossy(), span)
},
span,
)
},
);
record.push(
"history-path",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
match engine_state.config.history.file_format {
HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
config_path.clone().map_or_else(
|e| e,
|mut path| {
match engine_state.config.history.file_format {
HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
}
HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
let canon_hist_path = canonicalize_path(engine_state, &path);
Value::string(canon_hist_path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not find history path".into(),
},
span,
)
},
let canon_hist_path = canonicalize_path(engine_state, &path);
Value::string(canon_hist_path.to_string_lossy(), span)
},
),
);
record.push(
"loginshell-path",
if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.push("login.nu");
let canon_login_path = canonicalize_path(engine_state, &path);
Value::string(canon_login_path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not find login shell path".into(),
},
span,
)
},
config_path.clone().map_or_else(
|e| e,
|mut path| {
path.push("login.nu");
let canon_login_path = canonicalize_path(engine_state, &path);
Value::string(canon_login_path.to_string_lossy(), span)
},
),
);
#[cfg(feature = "plugin")]
@ -127,17 +115,14 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
if let Some(path) = &engine_state.plugin_signatures {
let canon_plugin_path = canonicalize_path(engine_state, path);
Value::string(canon_plugin_path.to_string_lossy(), span)
} else if let Some(mut plugin_path) = nu_path::config_dir() {
// If there are no signatures, we should still populate the plugin path
plugin_path.push("nushell");
plugin_path.push("plugin.nu");
Value::string(plugin_path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not get plugin signature location".into(),
// If there are no signatures, we should still populate the plugin path
config_path.clone().map_or_else(
|e| e,
|mut path| {
path.push("plugin.nu");
Value::string(path.to_string_lossy(), span)
},
span,
)
},
);

View File

@ -1324,6 +1324,21 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
#[label = "byte index is not a char boundary or is out of bounds of the input"]
span: Span,
},
/// The config directory could not be found
#[error("The config directory could not be found")]
#[diagnostic(
code(nu::shell::config_dir_not_found),
help(
r#"On Linux, this would be $XDG_CONFIG_HOME or $HOME/.config.
On MacOS, this would be `$HOME/Library/Application Support`.
On Windows, this would be %USERPROFILE%\AppData\Roaming"#
)
)]
ConfigDirNotFound {
#[label = "Could not find config directory"]
span: Option<Span>,
},
}
// TODO: Implement as From trait

View File

@ -28,32 +28,45 @@ fn test_default_config_path() {
let _ = fs::create_dir_all(&config_dir_nushell);
}
let cwd = std::env::current_dir().expect("Could not get current working directory");
let config_dir_nushell =
std::fs::canonicalize(&config_dir_nushell).expect("canonicalize config dir failed");
let actual = nu!(cwd: &cwd, "$nu.default-config-dir");
assert_eq!(actual.out, adjust_canonicalization(&config_dir_nushell));
let config_path = config_dir_nushell.join("config.nu");
// Create an empty file for canonicalization if it doesn't already exist
if !config_path.exists() {
let _ = std::fs::File::create(&config_path);
}
// We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653
let canon_config_path = adjust_canonicalization(
std::fs::canonicalize(config_path).expect("canonicalize config-path failed"),
);
let canon_config_path =
adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path));
let actual = nu!(cwd: &cwd, "$nu.config-path");
assert_eq!(actual.out, canon_config_path);
let env_path = config_dir_nushell.join("env.nu");
// Create an empty file for canonicalization if it doesn't already exist
if !env_path.exists() {
let _ = std::fs::File::create(&env_path);
}
let canon_env_path = adjust_canonicalization(
std::fs::canonicalize(env_path).expect("canonicalize of env-path failed"),
);
let canon_env_path =
adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path));
let actual = nu!(cwd: &cwd, "$nu.env-path");
assert_eq!(actual.out, canon_env_path);
let history_path = config_dir_nushell.join("history.txt");
let canon_history_path =
adjust_canonicalization(std::fs::canonicalize(&history_path).unwrap_or(history_path));
let actual = nu!(cwd: &cwd, "$nu.history-path");
assert_eq!(actual.out, canon_history_path);
let login_path = config_dir_nushell.join("login.nu");
let canon_login_path =
adjust_canonicalization(std::fs::canonicalize(&login_path).unwrap_or(login_path));
let actual = nu!(cwd: &cwd, "$nu.loginshell-path");
assert_eq!(actual.out, canon_login_path);
#[cfg(feature = "plugin")]
{
let plugin_path = config_dir_nushell.join("plugin.nu");
let canon_plugin_path =
adjust_canonicalization(std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path));
let actual = nu!(cwd: &cwd, "$nu.plugin-path");
assert_eq!(actual.out, canon_plugin_path);
}
}
#[test]