Repository navigation
fix(cli): show the script name instead of main in errors about a script's arguments - #19282
Conversation
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`.
|
scripts don't have to have a main. what happens in this PR when they do not? |
There was a problem hiding this comment.
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
Open (7)
A script namedforpanics nu Scripts named after parser keywords stop running The doc comment says names with a space fall back tomain Three parses of the arguments, two of them thrown away Some names still showmain, but the release note says they don't The new tests miss the cases that break The script-name alias is the thing that lets keywords through. The two High findings come from the overlay alias inevaluate_filemapping a keyword likefororsourcetomain. This PR is the first code to parse through that alias. Skipping the alias foris_parser_keywordnames would fix both at the source. Thefile_has_main && engine_state.find_decl(&script_name_bytes, &[])gate right after the merge looksmainup by the script name, so it would have to change too, or keyword-named scripts would stop runningmainat all. The alias is already unsafe before this PR: a script namedifbreaks the defaultdisplay_outputhook (if (term size).columns >= 100 ...reports "Extra positional argument") on both the base commit and this branch.
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.
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`.
|
Please reply to each review comment to ensure nothing is missed. |
There was a problem hiding this comment.
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
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.
| // 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(' ')), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| let stderr: String = test() | ||
| .cwd(dirs.test()) | ||
| .run("nu 10kb | complete | get stderr")?; | ||
| assert!(stderr.contains("missing foo")); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
Thanks |



Description
nu script.nuruns the script'smainby evaluatingmain <args>, so an error about the arguments showedmainin the source snippet, even though the usage line already uses the script name:evaluate_filealready adds the script name to the overlay as another name formain(andscript.nu fooformain foo), so the call can use it. This now builds the command line asscript.nu <args>when that parses as a call to the script's renamedmain(checked with a single probe parse), and falls back tomain <args>otherwise. The snippet then showsscript.nu:The script name is only used when it parses as a command. Scripts named after a parser keyword (
for,source,use,module,alias, ...) always callmaindirectly, and names that parse as something else, such as10kb,true,it's.nuora #b.nu, still showmain. Names with a space (my script.nu) use the script name. Scripts without amainaren't affected.Tests added in
tests/shell/mod.rs:main_script_missing_argument_uses_script_namemain_script_subcommand_missing_argument_uses_script_namemain_script_with_space_in_name_uses_script_namemain_script_named_like_a_parser_keyword_still_runsmain_script_named_like_a_value_still_runs(checks the fallback tomain)script_without_main_is_unaffectedUser-facing changes (Release notes)
main(such as a missing positional argument) now show the script name instead ofmain, when the script name parses as a command. Other names (for example10kborit's.nu) still showmain.Additional notes
Fixes #10191
Tests + formatting:
cargo fmt --all -- --checkcargo clippy -p nu-config -p nu-cmd-lang -p nu-command -p nu-cli -p nu-explore --all-targets -- -D warnings -D clippy::unwrap_usedcargo clippy -p nu -p nu-cli --all-targets -- -D warnings -D clippy::unwrap_usedcargo test --test tests -- shell::cargo test -p nu-cli --lib eval_fileThe script name is only used when the call resolves to main, so names like 10kb, it's.nu or main;… still show main.