mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 03:02:52 +02:00
add function to make env vars case-insensitive (#14390)
# Description This PR adds a new function that allows one to get an env var case-insensitively. I did this so we can hopefully stop having problems when Windows has HKLM as path and HKCU as Path. Instead of just changing every function that used the original one, I chose the ones that I thought were specific to getting the path. I didn't want to go all in and make every env get case insensitive, but maybe we should? 🤷🏻♂️ closes #12676 # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking 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` 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 toolkit.nu; toolkit test stdlib"` 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. -->
This commit is contained in:
@ -11,13 +11,6 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[cfg(windows)]
|
||||
const ENV_PATH_NAME: &str = "Path";
|
||||
#[cfg(windows)]
|
||||
const ENV_PATH_NAME_SECONDARY: &str = "PATH";
|
||||
#[cfg(not(windows))]
|
||||
const ENV_PATH_NAME: &str = "PATH";
|
||||
|
||||
const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS";
|
||||
|
||||
enum ConversionResult {
|
||||
@ -53,14 +46,14 @@ pub fn convert_env_values(engine_state: &mut EngineState, stack: &Stack) -> Resu
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
error = error.or_else(|| ensure_path(&mut new_scope, ENV_PATH_NAME));
|
||||
error = error.or_else(|| ensure_path(&mut new_scope, "PATH"));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let first_result = ensure_path(&mut new_scope, ENV_PATH_NAME);
|
||||
let first_result = ensure_path(&mut new_scope, "Path");
|
||||
if first_result.is_some() {
|
||||
let second_result = ensure_path(&mut new_scope, ENV_PATH_NAME_SECONDARY);
|
||||
let second_result = ensure_path(&mut new_scope, "PATH");
|
||||
|
||||
if second_result.is_some() {
|
||||
error = error.or(first_result);
|
||||
@ -107,7 +100,7 @@ pub fn env_to_string(
|
||||
ConversionResult::CellPathError => match value.coerce_string() {
|
||||
Ok(s) => Ok(s),
|
||||
Err(_) => {
|
||||
if env_name == ENV_PATH_NAME {
|
||||
if env_name.to_lowercase() == "path" {
|
||||
// Try to convert PATH/Path list to a string
|
||||
match value {
|
||||
Value::List { vals, .. } => {
|
||||
@ -216,31 +209,21 @@ pub fn current_dir_const(working_set: &StateWorkingSet) -> Result<PathBuf, Shell
|
||||
}
|
||||
|
||||
/// Get the contents of path environment variable as a list of strings
|
||||
///
|
||||
/// On non-Windows: It will fetch PATH
|
||||
/// On Windows: It will try to fetch Path first but if not present, try PATH
|
||||
pub fn path_str(
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
span: Span,
|
||||
) -> Result<String, ShellError> {
|
||||
let (pathname, pathval) = match stack.get_env_var(engine_state, ENV_PATH_NAME) {
|
||||
Some(v) => Ok((ENV_PATH_NAME, v)),
|
||||
None => {
|
||||
#[cfg(windows)]
|
||||
match stack.get_env_var(engine_state, ENV_PATH_NAME_SECONDARY) {
|
||||
Some(v) => Ok((ENV_PATH_NAME_SECONDARY, v)),
|
||||
None => Err(ShellError::EnvVarNotFoundAtRuntime {
|
||||
envvar_name: ENV_PATH_NAME_SECONDARY.to_string(),
|
||||
span,
|
||||
}),
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
Err(ShellError::EnvVarNotFoundAtRuntime {
|
||||
envvar_name: ENV_PATH_NAME.to_string(),
|
||||
span,
|
||||
})
|
||||
}
|
||||
let (pathname, pathval) = match stack.get_env_var_insensitive(engine_state, "path") {
|
||||
Some(v) => Ok((if cfg!(windows) { "Path" } else { "PATH" }, v)),
|
||||
None => Err(ShellError::EnvVarNotFoundAtRuntime {
|
||||
envvar_name: if cfg!(windows) {
|
||||
"Path".to_string()
|
||||
} else {
|
||||
"PATH".to_string()
|
||||
},
|
||||
span,
|
||||
}),
|
||||
}?;
|
||||
|
||||
env_to_string(pathname, pathval, engine_state, stack)
|
||||
|
Reference in New Issue
Block a user