Enable reloading changes to a submodule (#13170)

# Description

Fixes: https://github.com/nushell/nushell/issues/12099

Currently if user run `use voice.nu`, and file is unchanged, then run
`use voice.nu` again. nushell will use the module directly, even if
submodule inside `voice.nu` is changed.

After discussed with @kubouch, I think it's ok to re-parse the module
file when:
1. It exports sub modules which are defined by a file
2. It uses other modules which are defined by a file

## About the change:
To achieve the behavior, we need to add 2 attributes to `Module`:
1. `imported_modules`: it tracks the other modules is imported by the
givem `module`, e.g: `use foo.nu`
2. `file`: the path of a module, if a module is defined by a file, it
will be `Some(path)`, or else it will be `None`.

After the change:

    use voice.nu always read the file and parse it.
    use voice will still use the module which is saved in EngineState.

# User-Facing Changes

use `xxx.nu` will read the file and parse it if it exports submodules or
uses submodules

# Tests + Formatting

Done

---------

Co-authored-by: Jakub Žádník <kubouch@gmail.com>
This commit is contained in:
Wind
2024-06-26 09:33:37 +08:00
committed by GitHub
parent 55ee476306
commit def36865ef
7 changed files with 258 additions and 17 deletions

View File

@ -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;
@ -760,3 +760,162 @@ fn nested_list_export_works() {
let actual = nu!(&inp.join("; "));
assert_eq!(actual.out, "bacon");
}
#[test]
fn reload_submodules() {
Playground::setup("reload_submodule_changed_file", |dirs, sandbox| {
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export module animals.nu"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
// should also verify something unchanged if `use voice`.
let inp = [
"use voice.nu",
r#""export def cat [] {'meow'}" | save -f animals.nu"#,
"use voice",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
// should also works if we use members directly.
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export module animals.nu"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu animals cat",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu animals cat",
"(cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
});
}
#[test]
fn use_submodules() {
Playground::setup("use_submodules", |dirs, sandbox| {
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
// should also verify something unchanged if `use voice`.
let inp = [
"use voice.nu",
r#""export def cat [] {'meow'}" | save -f animals.nu"#,
"use voice",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
// also verify something is changed when using members.
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu cat"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu",
"(voice cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu *"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu",
"(voice cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu [cat]"#),
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
"use voice.nu",
"(voice cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
});
}
#[test]
fn use_nested_submodules() {
Playground::setup("use_submodules", |dirs, sandbox| {
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu"#),
FileWithContent("animals.nu", r#"export use nested_animals.nu"#),
FileWithContent("nested_animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
"use voice.nu",
"(voice animals nested_animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
sandbox.with_files(&[
FileWithContent("voice.nu", r#"export use animals.nu"#),
FileWithContent("animals.nu", r#"export use nested_animals.nu cat"#),
FileWithContent("nested_animals.nu", "export def cat [] { 'meow'}"),
]);
let inp = [
"use voice.nu",
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
"use voice.nu",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
sandbox.with_files(&[
FileWithContent("animals.nu", r#"export use nested_animals.nu cat"#),
FileWithContent("nested_animals.nu", "export def cat [] { 'meow' }"),
]);
let inp = [
"module voice { export module animals.nu }",
"use voice",
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
"use voice.nu",
"(voice animals cat) == 'woem'",
];
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
assert_eq!(actual.out, "true");
})
}