mirror of
https://github.com/nushell/nushell.git
synced 2025-04-27 06:38:21 +02:00
# Description Fix #14544 and is also the reciprocal of #14549. Before: If both a const and env `NU_PLUGIN_DIRS` were defined at the same time, the env paths would not be used. After: The directories from `const NU_PLUGIN_DIRS` are searched for a matching filename, and if not found, `$env.NU_PLUGIN_DIRS` directories will be searched. Before: `$env.NU_PLUGIN_DIRS` was unnecessary set both in main() and in default_env.nu After: `$env.NU_PLUGIN_DIRS` is only set in main() Before: `$env.NU_PLUGIN_DIRS` was set to `plugins` in the config directory After: `$env.NU_PLUGIN_DIRS` is set to an empty list and `const NU_PLUGIN_DIRS` is set to the directory above. Also updates `sample_env.nu` to use the `const` # User-Facing Changes Most scenarios should work just fine as there continues to be an `$env.NU_PLUGIN_DIRS` to append to or override. However, there is a small chance of a breaking change if someone was *querying* the old default `$env.NU_PLUGIN_DIRS`. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` Also updated the `env` tests and added one for the `const`. # After Submitting Config doc updates
159 lines
5.1 KiB
Rust
159 lines
5.1 KiB
Rust
#[allow(deprecated)]
|
|
use nu_engine::{command_prelude::*, current_dir};
|
|
use nu_protocol::{engine::StateWorkingSet, PluginRegistryFile};
|
|
use std::{
|
|
fs::{self, File},
|
|
path::PathBuf,
|
|
};
|
|
|
|
fn get_plugin_registry_file_path(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
span: Span,
|
|
custom_path: &Option<Spanned<String>>,
|
|
) -> Result<PathBuf, ShellError> {
|
|
#[allow(deprecated)]
|
|
let cwd = current_dir(engine_state, stack)?;
|
|
|
|
if let Some(ref custom_path) = custom_path {
|
|
Ok(nu_path::expand_path_with(&custom_path.item, cwd, true))
|
|
} else {
|
|
engine_state
|
|
.plugin_path
|
|
.clone()
|
|
.ok_or_else(|| ShellError::GenericError {
|
|
error: "Plugin registry file not set".into(),
|
|
msg: "pass --plugin-config explicitly here".into(),
|
|
span: Some(span),
|
|
help: Some("you may be running `nu` with --no-config-file".into()),
|
|
inner: vec![],
|
|
})
|
|
}
|
|
}
|
|
|
|
pub(crate) fn read_plugin_file(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
span: Span,
|
|
custom_path: &Option<Spanned<String>>,
|
|
) -> Result<PluginRegistryFile, ShellError> {
|
|
let plugin_registry_file_path =
|
|
get_plugin_registry_file_path(engine_state, stack, span, custom_path)?;
|
|
|
|
let file_span = custom_path.as_ref().map(|p| p.span).unwrap_or(span);
|
|
|
|
// Try to read the plugin file if it exists
|
|
if fs::metadata(&plugin_registry_file_path).is_ok_and(|m| m.len() > 0) {
|
|
PluginRegistryFile::read_from(
|
|
File::open(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned {
|
|
msg: format!(
|
|
"failed to read `{}`: {}",
|
|
plugin_registry_file_path.display(),
|
|
err
|
|
),
|
|
span: file_span,
|
|
})?,
|
|
Some(file_span),
|
|
)
|
|
} else if let Some(path) = custom_path {
|
|
Err(ShellError::FileNotFound {
|
|
file: path.item.clone(),
|
|
span: path.span,
|
|
})
|
|
} else {
|
|
Ok(PluginRegistryFile::default())
|
|
}
|
|
}
|
|
|
|
pub(crate) fn modify_plugin_file(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
span: Span,
|
|
custom_path: &Option<Spanned<String>>,
|
|
operate: impl FnOnce(&mut PluginRegistryFile) -> Result<(), ShellError>,
|
|
) -> Result<(), ShellError> {
|
|
let plugin_registry_file_path =
|
|
get_plugin_registry_file_path(engine_state, stack, span, custom_path)?;
|
|
|
|
let file_span = custom_path.as_ref().map(|p| p.span).unwrap_or(span);
|
|
|
|
// Try to read the plugin file if it exists
|
|
let mut contents = if fs::metadata(&plugin_registry_file_path).is_ok_and(|m| m.len() > 0) {
|
|
PluginRegistryFile::read_from(
|
|
File::open(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned {
|
|
msg: format!(
|
|
"failed to read `{}`: {}",
|
|
plugin_registry_file_path.display(),
|
|
err
|
|
),
|
|
span: file_span,
|
|
})?,
|
|
Some(file_span),
|
|
)?
|
|
} else {
|
|
PluginRegistryFile::default()
|
|
};
|
|
|
|
// Do the operation
|
|
operate(&mut contents)?;
|
|
|
|
// Save the modified file on success
|
|
contents.write_to(
|
|
File::create(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned {
|
|
msg: format!(
|
|
"failed to create `{}`: {}",
|
|
plugin_registry_file_path.display(),
|
|
err
|
|
),
|
|
span: file_span,
|
|
})?,
|
|
Some(span),
|
|
)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn canonicalize_possible_filename_arg(
|
|
engine_state: &EngineState,
|
|
stack: &Stack,
|
|
arg: &str,
|
|
) -> PathBuf {
|
|
// This results in the best possible chance of a match with the plugin item
|
|
#[allow(deprecated)]
|
|
if let Ok(cwd) = nu_engine::current_dir(engine_state, stack) {
|
|
let path = nu_path::expand_path_with(arg, &cwd, true);
|
|
// Try to canonicalize
|
|
nu_path::locate_in_dirs(&path, &cwd, || get_plugin_dirs(engine_state, stack))
|
|
// If we couldn't locate it, return the expanded path alone
|
|
.unwrap_or(path)
|
|
} else {
|
|
arg.into()
|
|
}
|
|
}
|
|
|
|
pub(crate) fn get_plugin_dirs(
|
|
engine_state: &EngineState,
|
|
stack: &Stack,
|
|
) -> impl Iterator<Item = String> {
|
|
// Get the NU_PLUGIN_DIRS from the constant and/or env var
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
let dirs_from_const = working_set
|
|
.find_variable(b"$NU_PLUGIN_DIRS")
|
|
.and_then(|var_id| working_set.get_constant(var_id).ok())
|
|
.cloned() // TODO: avoid this clone
|
|
.into_iter()
|
|
.flat_map(|value| value.into_list().ok())
|
|
.flatten()
|
|
.flat_map(|list_item| list_item.coerce_into_string().ok());
|
|
|
|
let dirs_from_env = stack
|
|
.get_env_var(engine_state, "NU_PLUGIN_DIRS")
|
|
.cloned() // TODO: avoid this clone
|
|
.into_iter()
|
|
.flat_map(|value| value.into_list().ok())
|
|
.flatten()
|
|
.flat_map(|list_item| list_item.coerce_into_string().ok());
|
|
|
|
dirs_from_const.chain(dirs_from_env)
|
|
}
|