Files
nushell/tests/modules
Darren Schroeder ab6c901127 fix(modules): reloading a module picks up edits and drops what the file no longer exports (#19468)
## Description

Working on a module at the prompt had a few traps, raised on #10995.

`use spam` and `overlay use spam` kept the loaded copy after the file
changed, while `use spam.nu` read it again. Directory modules had it
worst, because `use pkg` read the disk the first time and memory every
time after. By-name imports now read the file again when it, or anything
it imports, changed. With an unchanged file, or a module written at the
prompt, they behave as before.

Reloading also left old definitions behind. A command deleted from the
file kept working through `use`, `hide`, `overlay use --reload` and
`overlay hide` followed by `overlay use`, and a name you didn't import
again kept running the old code. A reload now hides what the new version
no longer exports, and moves every name still showing the old version to
the new one. A command you defined yourself with the same name stays,
and so does one you imported directly from a module this one used to
re-export.

`hide spam` after `use spam.nu *` silently did nothing, and now hides
the module's commands however they were imported.

Changing overlays moved you. `overlay hide` went back to the directory
you activated the overlay from, and bringing a hidden overlay back with
`overlay use <name>` or `overlay new` went to the directory it was
hidden in. `PWD` and `OLDPWD` now stay put unless the module's
`export-env` changes directory.

The first change could have sent more people into an existing trap. An
unclosed delimiter in a file that `use` or `source` loads left the REPL
at the `:::` prompt with nothing you could type to finish the line. The
validator now counts only errors in the line itself as unfinished input.

## User-facing changes (Release notes)

- `use <name>` and `overlay use <name>` now read the module's file again
if it changed, the same as `use <path>`.
- Reloading a module now removes commands, aliases, type aliases,
constants and submodules that were deleted from its file, and moves
names you imported earlier to the new version.
- `hide <module>` now hides a module's commands after `use <module> *`,
as well as after `use <module>`.
- Fixed the REPL getting stuck at the `:::` prompt when `use` or
`source` loaded a file with an unclosed delimiter. You now see the
error.

### Breaking changes

- `overlay hide`, `overlay use` and `overlay new` no longer change the
current directory, unless the module's `export-env` does. `--keep-env
[PWD]` still works but is no longer needed. A script that used a
temporary overlay as a scoped `cd` has to `cd` back itself.
- `use <name>` and `overlay use <name>` now report parse errors in the
module's file, where they used to keep the loaded copy. A hook that runs
`use toolkit` on every `cd` shows them until the file is fixed.


## Additional notes

Related to #10995.

This reverses two choices that existing tests had recorded. #13170 said
`use voice` "will still use the module which is saved in EngineState",
so one assertion in `use_submodules` flipped. Four tests asserted that
changing overlays moves you, so they're renamed with the new expected
value: `overlay_hide_in_export_env_keeps_cd`,
`use_overlay_hide_keeps_cd`, `source_env_overlay_hide_keeps_cd` and
`overlay_reactivate_with_module_name_keeps_pwd`.

By-name imports only check modules from an earlier source unit, so
loading a module tree reads no extra files. Loading a 120-module `export
module x.nu; export use x` tree takes 57.7 ms before and 58.2 ms after
on a debug build. Loading that tree and then running a by-name `use` of
it in a later source unit, which reads its 120 files, takes 77.1 ms
before and 80.6 ms after.

Only a reload removes a module's name. `hide <module>` keeps it on
purpose, so `hide spam; use spam` still works.
2026-11-12 08:08:01 -06:00
..