diff --git a/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs b/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs index 189fd39daf..c8ad90e518 100644 --- a/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs +++ b/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs @@ -119,31 +119,40 @@ impl Command for OverlayUse { // Evaluate the export-env block (if any) and keep its environment if let Some(block_id) = module.env_block { - let maybe_path = find_in_dirs_env( + let maybe_file_path_or_dir = find_in_dirs_env( &name_arg.item, engine_state, caller_stack, get_dirs_var_from_call(caller_stack, call), )?; - let block = engine_state.get_block(block_id); let mut callee_stack = caller_stack .gather_captures(engine_state, &block.captures) .reset_pipes(); - if let Some(path) = &maybe_path { + if let Some(path) = &maybe_file_path_or_dir { // Set the currently evaluated directory, if the argument is a valid path - let mut parent = path.clone(); - parent.pop(); - + let parent = if path.is_dir() { + path.clone() + } else { + let mut parent = path.clone(); + parent.pop(); + parent + }; let file_pwd = Value::string(parent.to_string_lossy(), call.head); callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd); } - if let Some(file_path) = &maybe_path { - let file_path = Value::string(file_path.to_string_lossy(), call.head); - callee_stack.add_env_var("CURRENT_FILE".to_string(), file_path); + 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); } let eval_block = get_eval_block(engine_state); diff --git a/tests/overlays/mod.rs b/tests/overlays/mod.rs index f3126c8140..63dd6d6446 100644 --- a/tests/overlays/mod.rs +++ b/tests/overlays/mod.rs @@ -1,4 +1,4 @@ -use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; +use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed}; use nu_test_support::playground::Playground; use nu_test_support::{nu, nu_repl_code}; use pretty_assertions::assert_eq; @@ -1408,3 +1408,50 @@ fn overlay_help_no_error() { let actual = nu!("overlay use -h"); assert!(actual.err.is_empty()); } + +#[test] +fn test_overlay_use_with_printing_file_pwd() { + Playground::setup("use_with_printing_file_pwd", |dirs, nu| { + let file = dirs.test().join("foo").join("mod.nu"); + nu.mkdir("foo").with_files(&[FileWithContent( + file.as_os_str().to_str().unwrap(), + r#" + export-env { + print $env.FILE_PWD + } + "#, + )]); + + let actual = nu!( + cwd: dirs.test(), + "overlay use foo" + ); + + assert_eq!(actual.out, dirs.test().join("foo").to_string_lossy()); + }); +} + +#[test] +fn test_overlay_use_with_printing_current_file() { + Playground::setup("use_with_printing_current_file", |dirs, nu| { + let file = dirs.test().join("foo").join("mod.nu"); + nu.mkdir("foo").with_files(&[FileWithContent( + file.as_os_str().to_str().unwrap(), + r#" + export-env { + print $env.CURRENT_FILE + } + "#, + )]); + + let actual = nu!( + cwd: dirs.test(), + "overlay use foo" + ); + + assert_eq!( + actual.out, + dirs.test().join("foo").join("mod.nu").to_string_lossy() + ); + }); +}