Try to make hide-env respects overlays (#15904)

# Description
Closes: #15755
I think it's a good feature, to achieve this, we need to get all hidden
envs(it's defined in `get_hidden_env_vars`, and then restore these envs
back to stack)

# User-Facing Changes
### Before
```nushell
> $env.foo = 'bar'
> overlay new xxx
> hide-env foo
> overlay hide xxx
> $env.foo
Error: nu:🐚:column_not_found

  × Cannot find column 'foo'
   ╭─[entry #21:5:1]
 4 │ overlay hide xxx
 5 │ $env.foo
   · ────┬───┬
   ·     │   ╰── value originates here
   ·     ╰── cannot find column 'foo'
   ╰────
```

### After
```nushell
> $env.foo = 'bar'
> overlay new xxx
> hide-env foo
> overlay hide xxx
> $env.foo
bar
```

## Note
But it doesn't work if it runs the example code in script:
`nu -c "$env.foo = 'bar'; overlay new xxx; hide-env foo; overlay hide
xxx; $env.foo"`
still raises an error says `foo` doesn't found. That's because if we run
the script at once, the envs in stack doesn't have a chance to merge
back into `engine_state`, which is only called in `repl`.

It introduces some sort of inconsistency, but I think users use overlays
mostly in repl, so it's good to have such feature first.

# Tests + Formatting
Added 2 tests

# After Submitting
NaN
This commit is contained in:
Wind
2025-06-13 07:22:23 +08:00
committed by GitHub
parent 2fe25d6299
commit 81e86c40e1
3 changed files with 66 additions and 1 deletions

View File

@ -856,6 +856,36 @@ fn overlay_hide_renamed_overlay() {
assert!(actual_repl.err.contains("external_command"));
}
#[test]
fn overlay_hide_restore_hidden_env() {
let inp = &[
"$env.foo = 'bar'",
"overlay new aa",
"hide-env foo",
"overlay hide aa",
"$env.foo",
];
let actual_repl = nu!(nu_repl_code(inp));
assert_eq!(actual_repl.out, "bar");
}
#[test]
fn overlay_hide_dont_restore_hidden_env_which_is_introduce_currently() {
let inp = &[
"overlay new aa",
"$env.foo = 'bar'",
"hide-env foo", // hide the env in overlay `aa`
"overlay hide aa",
"'foo' in $env",
];
let actual_repl = nu!(nu_repl_code(inp));
assert_eq!(actual_repl.out, "false");
}
#[test]
fn overlay_hide_and_add_renamed_overlay() {
let inp = &[