From 44be445b579b001c9ba2fd93dbccd44e895a2d65 Mon Sep 17 00:00:00 2001 From: Wind Date: Fri, 11 Oct 2024 14:45:42 +0800 Subject: [PATCH] Revert "fix $env.FILE_PWD and $env.CURRENT_FILE inside `use` (#13958)" (#14057) This reverts commit 5002d87af471685b5bde1b866d3e4a74b87c2f88 from pr #13958 It seems that something unexpected happened from [@ealap](https://github.com/ealap)'s report. Thanks! Reopen: #13425 --- crates/nu-cmd-lang/src/core_commands/use_.rs | 30 ++++--------- crates/nu-command/tests/commands/use_.rs | 44 -------------------- 2 files changed, 7 insertions(+), 67 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/use_.rs b/crates/nu-cmd-lang/src/core_commands/use_.rs index d822e0c955..8035cc81fe 100644 --- a/crates/nu-cmd-lang/src/core_commands/use_.rs +++ b/crates/nu-cmd-lang/src/core_commands/use_.rs @@ -98,21 +98,15 @@ This command is a parser keyword. For details, check: engine_state.get_span_contents(import_pattern.head.span), ); - let maybe_file_path_or_dir = find_in_dirs_env( + let maybe_file_path = find_in_dirs_env( &module_arg_str, engine_state, caller_stack, get_dirs_var_from_call(caller_stack, call), )?; - // module_arg_str maybe a directory, in this case - // find_in_dirs_env returns a directory. - let maybe_parent = maybe_file_path_or_dir.as_ref().and_then(|path| { - if path.is_dir() { - Some(path.to_path_buf()) - } else { - path.parent().map(|p| p.to_path_buf()) - } - }); + let maybe_parent = maybe_file_path + .as_ref() + .and_then(|path| path.parent().map(|p| p.to_path_buf())); let mut callee_stack = caller_stack .gather_captures(engine_state, &block.captures) @@ -124,15 +118,9 @@ This command is a parser keyword. For details, check: callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd); } - if let Some(path) = maybe_file_path_or_dir { - let module_file_path = if path.is_dir() { - // the existence of `mod.nu` is verified in parsing time - // so it's safe to use it here. - Value::string(path.join("mod.nu").to_string_lossy(), call.head) - } else { - Value::string(path.to_string_lossy(), call.head) - }; - callee_stack.add_env_var("CURRENT_FILE".to_string(), module_file_path); + if let Some(file_path) = maybe_file_path { + let file_path = Value::string(file_path.to_string_lossy(), call.head); + callee_stack.add_env_var("CURRENT_FILE".to_string(), file_path); } let eval_block = get_eval_block(engine_state); @@ -142,10 +130,6 @@ This command is a parser keyword. For details, check: // Merge the block's environment to the current stack redirect_env(engine_state, caller_stack, &callee_stack); - - // File-reative PWD is useless after eval file block. - caller_stack.remove_env_var(engine_state, "FILE_PWD"); - caller_stack.remove_env_var(engine_state, "CURRENT_FILE"); } } else { return Err(ShellError::GenericError { diff --git a/crates/nu-command/tests/commands/use_.rs b/crates/nu-command/tests/commands/use_.rs index 2822be83c7..2895e1635f 100644 --- a/crates/nu-command/tests/commands/use_.rs +++ b/crates/nu-command/tests/commands/use_.rs @@ -308,47 +308,3 @@ fn can_use_sub_subname_from_submodule() { let actual = nu!(inp); assert_eq!(actual.out, "bar") } - -#[test] -fn test_use_with_printing_file_pwd() { - Playground::setup("use_with_printing_file_pwd", |dirs, nu| { - let file = dirs.test().join("mod.nu"); - nu.with_files(&[FileWithContent( - file.as_os_str().to_str().unwrap(), - r#" - export-env { - print $env.FILE_PWD - } - "#, - )]); - - let actual = nu!( - cwd: dirs.test(), - "use ." - ); - - assert_eq!(actual.out, dirs.test().to_string_lossy()); - }); -} - -#[test] -fn test_use_with_printing_current_file() { - Playground::setup("use_with_printing_current_file", |dirs, nu| { - let file = dirs.test().join("mod.nu"); - nu.with_files(&[FileWithContent( - file.as_os_str().to_str().unwrap(), - r#" - export-env { - print $env.CURRENT_FILE - } - "#, - )]); - - let actual = nu!( - cwd: dirs.test(), - "use ." - ); - - assert_eq!(actual.out, dirs.test().join("mod.nu").to_string_lossy()); - }); -}