From 5954c55882387c59913c3c6689e80847f9db3896 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 13:12:08 +0200 Subject: [PATCH 1/6] feat(autoload): implement conditional vendor autoloading based on environment variable during command --- src/config_files.rs | 35 ++++++++++++++++++++++++++++++++--- src/run.rs | 11 ++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/config_files.rs b/src/config_files.rs index 5c53d8f082..f351bb8d9a 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -173,6 +173,20 @@ fn read_and_sort_directory(path: &Path) -> Result> { Ok(entries) } +pub(crate) fn should_run_autoload(engine_state: &EngineState, stack: &Stack) -> bool { + let env_vars = stack.get_env_vars(engine_state); + env_vars + .get("NU_AUTOLOAD_ON_COMMAND") + .and_then(|val| { + if let Ok(val_str) = val.as_str() { + Some(val_str.to_lowercase() == "true") + } else { + None + } + }) + .unwrap_or(false) +} + pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: &mut Stack) { warn!( "read_vendor_autoload_files() {}:{}:{}", @@ -180,10 +194,20 @@ pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: line!(), column!() ); + let mut autoload_dirs = get_vendor_autoload_dirs(engine_state); + if should_run_autoload(engine_state, stack) { + // Check for custom autoload dir in Nushell's environment + let env_vars = stack.get_env_vars(engine_state); + if let Some(val) = env_vars.get("NU_VENDOR_AUTOLOAD_DIR") { + if let Ok(path_str) = val.as_str() { + let path = std::path::PathBuf::from(path_str); + autoload_dirs.push(path); + } + } + } - // The evaluation order is first determined by the semantics of `get_vendor_autoload_dirs` - // to determine the order of directories to evaluate - get_vendor_autoload_dirs(engine_state) + // Use our modified autoload_dirs list + autoload_dirs // Changed: use our modified list instead of calling get_vendor_autoload_dirs again .iter() // User autoload directories are evaluated after vendor, which means that // the user can override vendor autoload files @@ -206,6 +230,11 @@ pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: } } }); + + // Add environment merging here to ensure changes take effect + if let Err(e) = engine_state.merge_env(stack) { + report_shell_error(engine_state, &e); + } } fn eval_default_config( diff --git a/src/run.rs b/src/run.rs index 43ca28bb04..c4216f3dc9 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,6 +1,6 @@ use crate::{ command, - config_files::{self, setup_config}, + config_files::{self, read_vendor_autoload_files, setup_config, should_run_autoload}, }; use log::trace; #[cfg(feature = "plugin")] @@ -76,6 +76,15 @@ pub(crate) fn run_commands( } perf!("read login.nu", start_time, use_color); + // Only run vendor autoload if NU_AUTOLOAD_ON_COMMAND is set to true + + if should_run_autoload(engine_state, &stack) { + read_vendor_autoload_files(engine_state, &mut stack); + if let Err(e) = engine_state.merge_env(&mut stack) { + report_shell_error(engine_state, &e); + } + perf!("read vendor autoload", start_time, use_color); + } } // Before running commands, set up the startup time From 4b3e4305a82a73111abc1e5e757eff889c43c7e1 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 13:55:07 +0200 Subject: [PATCH 2/6] refactor(autoload): rename should_run_autoload to nu_autoload_on_command for clarity --- src/config_files.rs | 4 ++-- src/run.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config_files.rs b/src/config_files.rs index f351bb8d9a..23c0575406 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -173,7 +173,7 @@ fn read_and_sort_directory(path: &Path) -> Result> { Ok(entries) } -pub(crate) fn should_run_autoload(engine_state: &EngineState, stack: &Stack) -> bool { +pub(crate) fn nu_autoload_on_command(engine_state: &EngineState, stack: &Stack) -> bool { let env_vars = stack.get_env_vars(engine_state); env_vars .get("NU_AUTOLOAD_ON_COMMAND") @@ -195,7 +195,7 @@ pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: column!() ); let mut autoload_dirs = get_vendor_autoload_dirs(engine_state); - if should_run_autoload(engine_state, stack) { + if nu_autoload_on_command(engine_state, stack) { // Check for custom autoload dir in Nushell's environment let env_vars = stack.get_env_vars(engine_state); if let Some(val) = env_vars.get("NU_VENDOR_AUTOLOAD_DIR") { diff --git a/src/run.rs b/src/run.rs index c4216f3dc9..48c2ab4db1 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,6 +1,6 @@ use crate::{ command, - config_files::{self, read_vendor_autoload_files, setup_config, should_run_autoload}, + config_files::{self, read_vendor_autoload_files, setup_config, nu_autoload_on_command}, }; use log::trace; #[cfg(feature = "plugin")] @@ -78,7 +78,7 @@ pub(crate) fn run_commands( perf!("read login.nu", start_time, use_color); // Only run vendor autoload if NU_AUTOLOAD_ON_COMMAND is set to true - if should_run_autoload(engine_state, &stack) { + if nu_autoload_on_command(engine_state, &stack) { read_vendor_autoload_files(engine_state, &mut stack); if let Err(e) = engine_state.merge_env(&mut stack) { report_shell_error(engine_state, &e); From 6ae491a6d89ec41ded6ee81651bb2659b37f19d0 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 13:58:02 +0200 Subject: [PATCH 3/6] refactor(run): reorder imports for improved clarity --- src/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.rs b/src/run.rs index 48c2ab4db1..a81f808e94 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,6 +1,6 @@ use crate::{ command, - config_files::{self, read_vendor_autoload_files, setup_config, nu_autoload_on_command}, + config_files::{self, nu_autoload_on_command, read_vendor_autoload_files, setup_config}, }; use log::trace; #[cfg(feature = "plugin")] From 0cf878c5dc26f20c726b0f528d2314729530fd82 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 14:35:13 +0200 Subject: [PATCH 4/6] refactor(autoload): simplify nu_autoload_on_command logic to check for existence of environment variable --- src/config_files.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/config_files.rs b/src/config_files.rs index 23c0575406..09f955d797 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -175,16 +175,7 @@ fn read_and_sort_directory(path: &Path) -> Result> { pub(crate) fn nu_autoload_on_command(engine_state: &EngineState, stack: &Stack) -> bool { let env_vars = stack.get_env_vars(engine_state); - env_vars - .get("NU_AUTOLOAD_ON_COMMAND") - .and_then(|val| { - if let Ok(val_str) = val.as_str() { - Some(val_str.to_lowercase() == "true") - } else { - None - } - }) - .unwrap_or(false) + env_vars.get("NU_AUTOLOAD_ON_COMMAND").is_some() } pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: &mut Stack) { From 2712d04a9865051e247a0c541ab7a9c1cf9d8920 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 14:36:43 +0200 Subject: [PATCH 5/6] refactor(run): update comment to clarify vendor autoload condition --- src/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.rs b/src/run.rs index a81f808e94..31e40abe1e 100644 --- a/src/run.rs +++ b/src/run.rs @@ -76,7 +76,7 @@ pub(crate) fn run_commands( } perf!("read login.nu", start_time, use_color); - // Only run vendor autoload if NU_AUTOLOAD_ON_COMMAND is set to true + // Only run vendor autoload if NU_AUTOLOAD_ON_COMMAND is set if nu_autoload_on_command(engine_state, &stack) { read_vendor_autoload_files(engine_state, &mut stack); From f2186dafeee738fa26707357e6bb95e03db997ea Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Fri, 2 May 2025 14:45:15 +0200 Subject: [PATCH 6/6] refactor(autoload): improve nu_autoload_on_command logic to use contains_key for clarity --- src/config_files.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config_files.rs b/src/config_files.rs index 09f955d797..a08a791732 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -175,7 +175,7 @@ fn read_and_sort_directory(path: &Path) -> Result> { pub(crate) fn nu_autoload_on_command(engine_state: &EngineState, stack: &Stack) -> bool { let env_vars = stack.get_env_vars(engine_state); - env_vars.get("NU_AUTOLOAD_ON_COMMAND").is_some() + env_vars.contains_key("NU_AUTOLOAD_ON_COMMAND") } pub(crate) fn read_vendor_autoload_files(engine_state: &mut EngineState, stack: &mut Stack) {