Skip to content

Commit 022eddf

Browse files
davidpavlovschiDavid Pavlovschii
andauthored
Fix incorrect claim that a module file cannot match its directory name (#2219)
The 'Creating Modules' chapter stated that a `.nu` file cannot have the same name as its module directory (e.g. `spam/spam.nu`). Nushell accepts that layout: the file imports normally, both directly (`use spam/spam.nu`) and as a submodule exported from `spam/mod.nu`. The restriction that does exist is on the exported *name*: a module cannot export a command, alias, or known external defined inside it, or a submodule declared by name, with the same name as the module itself. Replace the false statement with the real restriction, and keep a note warning that a same-named submodule is still worth avoiding because its `main` is shadowed by the parent module's `main`. Co-authored-by: David Pavlovschii <guyraya9@gmail.com>
1 parent 7a4ab02 commit 022eddf

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

book/modules/creating_modules.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,22 @@ To bring `my-utils` exported environment into scope for the `go.nu` module, ther
425425

426426
Note that the first method keeps `my-utils` environment inside the `go.nu` module's scope. The second, on the other hand, re-exports `my-utils` environment into the user scope.
427427

428-
### Module files and commands cannot be named after parent module
428+
### Exports cannot be named after their module
429429

430-
A `.nu` file cannot have the same name as its module directory (e.g., `spam/spam.nu`) as this would create an ambiguous condition with the name being defined twice. This is similar to the situation described above where a command cannot have the same name as its parent.
430+
A module cannot export a command, alias, or known external defined inside it that has the same name as the module itself. For commands and known externals, name the definition `main` instead, as covered in [`main` Exports](#main-exports) above. The same restriction applies to a submodule declared by name, so `export module spam` inside a module named `spam` is rejected:
431+
432+
```nu
433+
module spam { export module spam { } }
434+
# => Error: nu::parser::named_as_module
435+
# => ...
436+
# => help: Module spam can't export module named
437+
# => the same as the module. Either change the module
438+
# => name, or export `mod` module.
439+
```
440+
441+
::: note
442+
A `.nu` file _may_ have the same name as its module directory (e.g., `spam/spam.nu`), and Nushell will import it. Still, prefer a different name: if the parent module and the same-named submodule both export a `main`, the two definitions resolve to the same command name and the parent's `main` silently wins.
443+
:::
431444

432445
## Windows Path Syntax
433446

0 commit comments

Comments
 (0)