Skip to content

fix(cli): show the script name instead of main in errors about a script's arguments - #19282

Merged
fdncred merged 3 commits into
nushell:mainfrom
qodelicious:fix-main-error-script-name
Oct 19, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
qodelicious:fix-main-error-script-name

Conversation

@qodelicious

@qodelicious qodelicious commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

nu script.nu runs the script's main by evaluating main <args>, so an error about the arguments showed main in the source snippet, even though the usage line already uses the script name:

Error: nu::parser::missing_positional

  × Missing required positional argument.
   ╭─[<commandline>:1:5]
 1 │ main
   ·     ▲
   ·     ╰── missing foo
   ╰────
  help: Usage: script.nu <foo> . Use `--help` for more information.

evaluate_file already adds the script name to the overlay as another name for main (and script.nu foo for main foo), so the call can use it. This now builds the command line as script.nu <args> when that parses as a call to the script's renamed main (checked with a single probe parse), and falls back to main <args> otherwise. The snippet then shows script.nu:

   ╭─[<commandline>:1:10]
 1 │ script.nu
   ·          ▲
   ·          ╰── missing foo
   ╰────
  help: Usage: script.nu <foo> . Use `--help` for more information.

The script name is only used when it parses as a command. Scripts named after a parser keyword (for, source, use, module, alias, ...) always call main directly, and names that parse as something else, such as 10kb, true, it's.nu or a #b.nu, still show main. Names with a space (my script.nu) use the script name. Scripts without a main aren't affected.

Tests added in tests/shell/mod.rs:

  • main_script_missing_argument_uses_script_name
  • main_script_subcommand_missing_argument_uses_script_name
  • main_script_with_space_in_name_uses_script_name
  • main_script_named_like_a_parser_keyword_still_runs
  • main_script_named_like_a_value_still_runs (checks the fallback to main)
  • script_without_main_is_unaffected

User-facing changes (Release notes)

  • Errors about the arguments passed to a script's main (such as a missing positional argument) now show the script name instead of main, when the script name parses as a command. Other names (for example 10kb or it's.nu) still show main.

Additional notes

Fixes #10191

Tests + formatting:

  • cargo fmt --all -- --check
  • cargo clippy -p nu-config -p nu-cmd-lang -p nu-command -p nu-cli -p nu-explore --all-targets -- -D warnings -D clippy::unwrap_used
  • cargo clippy -p nu -p nu-cli --all-targets -- -D warnings -D clippy::unwrap_used
  • cargo test --test tests -- shell::
  • cargo test -p nu-cli --lib eval_file

The script name is only used when the call resolves to main, so names like 10kb, it's.nu or main;… still show main.

A script's `main` was called as `main <args>`, so an error like a
missing positional argument showed `main` in the source snippet. Call
it by the script's name instead, which the overlay already resolves to
`main`, when that parses as the same call. A name that doesn't (such
as `10kb`, which parses as a filesize) keeps using `main`.
@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

scripts don't have to have a main. what happens in this PR when they do not?

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

Calling main by the script's file name works for ordinary names, but a script named after a parser keyword now panics (for) or fails to parse (source, use, module, alias), where the base commit ran it.

Findings: 2 High severity · 0 Medium severity · 5 Low severity

Open (7)

Notes from my clanker, checked on 97cf1f2. I built the PR and its parent and ran about 35 script names through both, covering parser keywords, names with spaces and names with shell metacharacters.

Comment thread crates/nu-cli/src/eval_file.rs Resolved
Comment thread crates/nu-cli/src/eval_file.rs Resolved
Comment thread crates/nu-cli/src/eval_file.rs Resolved
Comment thread crates/nu-cli/src/eval_file.rs Resolved
Comment thread crates/nu-cli/src/eval_file.rs Resolved
Comment thread tests/shell/mod.rs Resolved
A script named after a parser keyword (`for`, `source`, `use`, `module`,
`alias`) is now called through `main`, since parsing `<keyword> <args>`
runs the keyword's own parser, which panics for `for` and reports parse
errors for the others.

The call is now parsed once, and the script's name is used when it
resolves to one of the script's own commands. Fix the doc comment about
names with spaces, and add tests for keyword names, a name with a space,
and a script without `main`.
@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Please reply to each review comment to ensure nothing is missed.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

All six threads from my last review are fixed on 1af46ed and resolved, but the new calls_script guard picks the wrong command for a script with a helper named after it and for file names that start with main followed by ;, # or |.

Findings: 1 High severity · 0 Medium severity · 1 Low severity

Open (2)

Notes from my clanker, checked on 1af46ed. I built the PR head and its parent, ran about 50 script names through both (parser keywords, multi-word names that start with a keyword, names with spaces, value-like names, helpers named after the script, names that start with main), and ran the 13 shell::main_script* and shell::script_without_main* tests with the suggested fix applied.

Comment thread crates/nu-cli/src/eval_file.rs Outdated
Comment on lines +238 to +245
// The blocks of the script's commands were renamed after the script above, which
// distinguishes them from any other command that shares the name.
Expr::Call(call) => working_set
.get_decl(call.decl_id)
.block_id()
.map(|block_id| working_set.get_block(block_id).signature.name.as_str())
.and_then(|name| name.strip_prefix(script_name))
.is_some_and(|rest| rest.is_empty() || rest.starts_with(' ')),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High severity The guard accepts calls that are not to main, or that stop partway through the name

The check passes when the resolved decl's block name starts with the script name. That doesn't show the call is to main, and it doesn't show the call covers the whole name. Each of these worked on the base commit:

Script Command Base This PR
foo: def main [x: string] and def "foo bar" [] nu foo bar runs main with x = bar runs the foo bar helper
build.nu: def main [target: string] and def "build.nu docs" [] nu build.nu docs building docs runs the helper
main;print injected: def main [] nu 'main;print injected' main ran main ran, then injected
main #2.nu: def main [foo: string] nu 'main #2.nu' ok got ok missing_positional, #2.nu ok became a comment
main|str upcase: def main [foo: string] nu 'main|str upcase' ok got ok missing_positional, ok went to str upcase

In the first two rows, the script's own foo bar helper isn't renamed. The overlay adds foo for main and foo <sub> for each main <sub>, so a helper named after the script keeps its own decl, and its block name foo bar starts with foo. The comment on this arm says the renaming tells the script's commands apart from others with the same name, but this helper still gets through.

In the last three, the first element is a call to main itself, because the parser stops at ;, # or |. Its block was renamed to the whole file name, so strip_prefix returns "", and eval_source then runs the rest of the file name as code.

The decl keeps its original name (only the block signatures were renamed), so comparing the call's head with the decl name covers both cases:

Expr::Call(call) => {
    let head = working_set.get_span_contents(call.head);
    let decl_name = working_set.get_decl(call.decl_id).name().as_bytes();
    head.strip_prefix(script_name.as_bytes())
        .is_some_and(|sub| decl_name.strip_prefix(b"main") == Some(sub))
}

The head has to be the whole script name plus any subcommand words, and the decl has to be main with the same subcommand words. With this patch all five rows match the base output again, script.nu and my script.nu still show the script name in the snippet, and the 13 shell::main_script* and shell::script_without_main* tests pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to your version. the call has to name the whole script and resolve to main with the same subcommand words. A build.nu docs helper and names like main;print injected, main #2.nu and main|str upcase now behave like the base commit.

Comment thread tests/shell/mod.rs
let stderr: String = test()
.cwd(dirs.test())
.run("nu 10kb | complete | get stderr")?;
assert!(stderr.contains("missing foo"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low severity The 10kb test passes whichever name the call uses

The PR description says this test checks the fallback to main, but missing foo is printed on both paths. Asserting that the snippet shows main (it's main at <commandline>:1:5 here) would pin the fallback. A script with a def "<script name> <word>" helper and one named main;... would cover the cases in the comment on calls_script.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, the 10kb test now checks that the snippet shows main. I also added tests for a helper named after the script and for names that start with main;, main # and main| (the last one is skipped on Windows).

@fdncred
fdncred merged commit 2deff7f into nushell:main Oct 19, 2026
11 checks passed
@fdncred

fdncred commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 19, 2026
@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect name in missing argument error for main command

2 participants