Revert "Remove std::env::set_current_dir() call from EngineState::merge_env()" (#12954)

Reverts nushell/nushell#12922
This commit is contained in:
Darren Schroeder 2024-05-24 11:09:59 -05:00 committed by GitHub
parent bf07806b1b
commit 7d11c28eea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 83 additions and 41 deletions

View file

@ -235,10 +235,17 @@ pub fn eval_config_contents(
engine_state.file = prev_file; engine_state.file = prev_file;
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
if let Err(e) = engine_state.merge_env(stack) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
report_error_new(engine_state, &e); report_error_new(engine_state, &e);
} }
} }
Err(e) => {
report_error_new(engine_state, &e);
}
}
}
} }
} }

View file

@ -15,7 +15,10 @@ use crate::{
use crossterm::cursor::SetCursorStyle; use crossterm::cursor::SetCursorStyle;
use log::{error, trace, warn}; use log::{error, trace, warn};
use miette::{ErrReport, IntoDiagnostic, Result}; use miette::{ErrReport, IntoDiagnostic, Result};
use nu_cmd_base::{hook::eval_hook, util::get_editor}; use nu_cmd_base::{
hook::eval_hook,
util::{get_editor, get_guaranteed_cwd},
};
use nu_color_config::StyleComputer; use nu_color_config::StyleComputer;
#[allow(deprecated)] #[allow(deprecated)]
use nu_engine::{convert_env_values, current_dir_str, env_to_strings}; use nu_engine::{convert_env_values, current_dir_str, env_to_strings};
@ -115,7 +118,8 @@ pub fn evaluate_repl(
PipelineData::empty(), PipelineData::empty(),
false, false,
); );
engine_state.merge_env(&mut unique_stack)?; let cwd = get_guaranteed_cwd(engine_state, &unique_stack);
engine_state.merge_env(&mut unique_stack, cwd)?;
} }
let hostname = System::host_name(); let hostname = System::host_name();
@ -277,10 +281,12 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
hostname, hostname,
} = ctx; } = ctx;
let cwd = get_guaranteed_cwd(engine_state, &stack);
let mut start_time = std::time::Instant::now(); let mut start_time = std::time::Instant::now();
// Before doing anything, merge the environment from the previous REPL iteration into the // Before doing anything, merge the environment from the previous REPL iteration into the
// permanent state. // permanent state.
if let Err(err) = engine_state.merge_env(&mut stack) { if let Err(err) = engine_state.merge_env(&mut stack, cwd) {
report_error_new(engine_state, &err); report_error_new(engine_state, &err);
} }
perf( perf(

View file

@ -18,11 +18,11 @@ use support::{
#[fixture] #[fixture]
fn completer() -> NuCompleter { fn completer() -> NuCompleter {
// Create a new engine // Create a new engine
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example // Add record value as example
let record = "def tst [--mod -s] {}"; let record = "def tst [--mod -s] {}";
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(Arc::new(engine), Arc::new(stack))
@ -31,12 +31,12 @@ fn completer() -> NuCompleter {
#[fixture] #[fixture]
fn completer_strings() -> NuCompleter { fn completer_strings() -> NuCompleter {
// Create a new engine // Create a new engine
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example // Add record value as example
let record = r#"def animals [] { ["cat", "dog", "eel" ] } let record = r#"def animals [] { ["cat", "dog", "eel" ] }
def my-command [animal: string@animals] { print $animal }"#; def my-command [animal: string@animals] { print $animal }"#;
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(Arc::new(engine), Arc::new(stack))
@ -45,7 +45,7 @@ fn completer_strings() -> NuCompleter {
#[fixture] #[fixture]
fn extern_completer() -> NuCompleter { fn extern_completer() -> NuCompleter {
// Create a new engine // Create a new engine
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example // Add record value as example
let record = r#" let record = r#"
@ -56,7 +56,7 @@ fn extern_completer() -> NuCompleter {
-b: string@animals -b: string@animals
] ]
"#; "#;
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(Arc::new(engine), Arc::new(stack))
@ -65,7 +65,7 @@ fn extern_completer() -> NuCompleter {
#[fixture] #[fixture]
fn custom_completer() -> NuCompleter { fn custom_completer() -> NuCompleter {
// Create a new engine // Create a new engine
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example // Add record value as example
let record = r#" let record = r#"
@ -79,7 +79,7 @@ fn custom_completer() -> NuCompleter {
completer: $external_completer completer: $external_completer
} }
"#; "#;
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(Arc::new(engine), Arc::new(stack))
@ -751,11 +751,11 @@ fn folder_with_directorycompletions() {
#[test] #[test]
fn variables_completions() { fn variables_completions() {
// Create a new engine // Create a new engine
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example // Add record value as example
let record = "let actor = { name: 'Tom Hardy', age: 44 }"; let record = "let actor = { name: 'Tom Hardy', age: 44 }";
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
@ -858,11 +858,11 @@ fn variables_completions() {
#[test] #[test]
fn alias_of_command_and_flags() { fn alias_of_command_and_flags() {
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Create an alias // Create an alias
let alias = r#"alias ll = ls -l"#; let alias = r#"alias ll = ls -l"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
@ -877,11 +877,11 @@ fn alias_of_command_and_flags() {
#[test] #[test]
fn alias_of_basic_command() { fn alias_of_basic_command() {
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Create an alias // Create an alias
let alias = r#"alias ll = ls "#; let alias = r#"alias ll = ls "#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
@ -896,14 +896,14 @@ fn alias_of_basic_command() {
#[test] #[test]
fn alias_of_another_alias() { fn alias_of_another_alias() {
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Create an alias // Create an alias
let alias = r#"alias ll = ls -la"#; let alias = r#"alias ll = ls -la"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir.clone()).is_ok());
// Create the second alias // Create the second alias
let alias = r#"alias lf = ll -f"#; let alias = r#"alias lf = ll -f"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
@ -920,7 +920,7 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
let completer = format!("$env.config.completions.external.completer = {completer}"); let completer = format!("$env.config.completions.external.completer = {completer}");
// Create a new engine // Create a new engine
let (_, _, mut engine_state, mut stack) = new_engine(); let (dir, _, mut engine_state, mut stack) = new_engine();
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);
let block = parse(&mut working_set, None, completer.as_bytes(), false); let block = parse(&mut working_set, None, completer.as_bytes(), false);
@ -936,7 +936,7 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack).is_ok()); assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine_state), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine_state), Arc::new(stack));
@ -1114,11 +1114,11 @@ fn custom_completer_triggers_cursor_after_word(mut custom_completer: NuCompleter
#[ignore = "was reverted, still needs fixing"] #[ignore = "was reverted, still needs fixing"]
#[rstest] #[rstest]
fn alias_offset_bug_7648() { fn alias_offset_bug_7648() {
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Create an alias // Create an alias
let alias = r#"alias ea = ^$env.EDITOR /tmp/test.s"#; let alias = r#"alias ea = ^$env.EDITOR /tmp/test.s"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
@ -1133,11 +1133,11 @@ fn alias_offset_bug_7648() {
#[ignore = "was reverted, still needs fixing"] #[ignore = "was reverted, still needs fixing"]
#[rstest] #[rstest]
fn alias_offset_bug_7754() { fn alias_offset_bug_7754() {
let (_, _, mut engine, mut stack) = new_engine(); let (dir, _, mut engine, mut stack) = new_engine();
// Create an alias // Create an alias
let alias = r#"alias ll = ls -l"#; let alias = r#"alias ll = ls -l"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));

View file

@ -62,7 +62,7 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack); let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok()); assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
@ -97,7 +97,7 @@ pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack); let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok()); assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
@ -132,7 +132,7 @@ pub fn new_partial_engine() -> (PathBuf, String, EngineState, Stack) {
); );
// Merge environment into the permanent state // Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack); let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok()); assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
@ -173,6 +173,7 @@ pub fn merge_input(
input: &[u8], input: &[u8],
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
dir: PathBuf,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
@ -195,5 +196,5 @@ pub fn merge_input(
.is_ok()); .is_ok());
// Merge environment into the permanent state // Merge environment into the permanent state
engine_state.merge_env(stack) engine_state.merge_env(stack, &dir)
} }

View file

@ -1,3 +1,4 @@
use crate::util::get_guaranteed_cwd;
use miette::Result; use miette::Result;
use nu_engine::{eval_block, eval_block_with_early_return}; use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::parse; use nu_parser::parse;
@ -283,7 +284,8 @@ pub fn eval_hook(
} }
} }
engine_state.merge_env(stack)?; let cwd = get_guaranteed_cwd(engine_state, stack);
engine_state.merge_env(stack, cwd)?;
Ok(output) Ok(output)
} }

View file

@ -138,7 +138,7 @@ pub fn check_example_evaluates_to_expected_output(
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy())); stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
engine_state engine_state
.merge_env(&mut stack) .merge_env(&mut stack, cwd)
.expect("Error merging environment"); .expect("Error merging environment");
let empty_input = PipelineData::empty(); let empty_input = PipelineData::empty();

View file

@ -277,7 +277,11 @@ impl EngineState {
} }
/// Merge the environment from the runtime Stack into the engine state /// Merge the environment from the runtime Stack into the engine state
pub fn merge_env(&mut self, stack: &mut Stack) -> Result<(), ShellError> { pub fn merge_env(
&mut self,
stack: &mut Stack,
cwd: impl AsRef<Path>,
) -> Result<(), ShellError> {
let mut config_updated = false; let mut config_updated = false;
for mut scope in stack.env_vars.drain(..) { for mut scope in stack.env_vars.drain(..) {
@ -307,6 +311,9 @@ impl EngineState {
} }
} }
// TODO: better error
std::env::set_current_dir(cwd)?;
if config_updated { if config_updated {
// Make plugin GC config changes take effect immediately. // Make plugin GC config changes take effect immediately.
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]

View file

@ -98,7 +98,8 @@ use std pwd
eval_block::<WithoutDebug>(engine_state, &mut stack, &block, pipeline_data)?; eval_block::<WithoutDebug>(engine_state, &mut stack, &block, pipeline_data)?;
engine_state.merge_env(&mut stack)?; let cwd = engine_state.cwd(Some(&stack))?;
engine_state.merge_env(&mut stack, cwd)?;
Ok(()) Ok(())
} }

View file

@ -164,10 +164,17 @@ pub(crate) fn read_default_env_file(engine_state: &mut EngineState, stack: &mut
); );
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
if let Err(e) = engine_state.merge_env(stack) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
report_error_new(engine_state, &e); report_error_new(engine_state, &e);
} }
} }
Err(e) => {
report_error_new(engine_state, &e);
}
}
}
fn eval_default_config( fn eval_default_config(
engine_state: &mut EngineState, engine_state: &mut EngineState,
@ -195,10 +202,17 @@ fn eval_default_config(
); );
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
if let Err(e) = engine_state.merge_env(stack) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
report_error_new(engine_state, &e); report_error_new(engine_state, &e);
} }
} }
Err(e) => {
report_error_new(engine_state, &e);
}
}
}
pub(crate) fn setup_config( pub(crate) fn setup_config(
engine_state: &mut EngineState, engine_state: &mut EngineState,

View file

@ -251,9 +251,13 @@ pub fn nu_repl() {
for (i, line) in source_lines.iter().enumerate() { for (i, line) in source_lines.iter().enumerate() {
let mut stack = Stack::with_parent(top_stack.clone()); let mut stack = Stack::with_parent(top_stack.clone());
let cwd = engine_state
.cwd(Some(&stack))
.unwrap_or_else(|err| outcome_err(&engine_state, &err));
// Before doing anything, merge the environment from the previous REPL iteration into the // Before doing anything, merge the environment from the previous REPL iteration into the
// permanent state. // permanent state.
if let Err(err) = engine_state.merge_env(&mut stack) { if let Err(err) = engine_state.merge_env(&mut stack, &cwd) {
outcome_err(&engine_state, &err); outcome_err(&engine_state, &err);
} }