Repository navigation
fix: report missing closure blocks instead of panicking - #19159
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Other closure-consuming commands retain infallible evaluator construction and can still panic on the same stale closure.
Review effort: Balanced
Findings: 1
What changed in this PR
Prevents do from panicking when evaluating closures whose blocks are absent from a job’s cloned engine state.
Changes:
- Adds fallible
ClosureEvalOnceconstructors. - Updates
doto propagate missing-block errors. - Adds a multi-input job regression test.
| File | Description |
|---|---|
crates/nu-engine/src/closure_eval.rs |
Adds fallible closure evaluator construction. |
crates/nu-cmd-lang/src/core_commands/do_.rs |
Uses fallible constructors in do. |
crates/nu-command/tests/commands/job.rs |
Tests the reported job/closure scenario. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
are you able to address the review? |
Address the review on nushell#19159: the stale-closure panic was still reachable through the infallible ClosureEval/ClosureEvalOnce constructors, e.g. a job receiving a closure parsed after it was spawned and passing it to zip, or each/where via ClosureEval::new. - remove the infallible constructors so missing-block lookup can only happen through the fallible try_* variants - add ClosureEval::try_new / try_new_preserve_out_dest - migrate all closure-consuming commands to propagate the error - generate now reports the same error instead of panicking on EngineState::get_block for a stale closure - add job regression tests for zip and each (nushell#19147 flow)
|
Addressed the review — thanks for the nudge! The infallible |
fcbd49f to
9688cb1
Compare
|
Let's do one more clanker review to make sure everything checks out. |
fdncred
left a comment
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
The fallible constructors fix do, each and zip, but the branch no longer compiles against main after #19190, and tee, describe --detailed, the to serializers and the tui hooks still panic on the same closure.
Findings: 3
Open (8)
RemovingClosureEval::newbreaks therecordcommands merged in #19190closure_aritystill panics beforetry_newrunstee,describeand thetoserializers still panic on the same closurepar-eachturns the construction error into one error value per itemdefaultstores the error as the default value instead of returning itmissing_block_errorhas no span and always reports the same Rust location The block lookup is repeated six times; one fallible lookup would cover every callergeneratelooks the block up twice
Reviewed by reading the code at 9688cb1 and comparing it with current main (67edfb6). I didn't build or run the tests.
6c03fdd to
99ffc63
Compare
|
when you're done, please respond to each review comment so we can be sure all have been addressed. thanks! |
17137ef to
25c14a6
Compare
fdncred
left a comment
There was a problem hiding this comment.
Review of 25c14a6
I checked all nine earlier threads against this commit. Seven are resolved. The par-each and tui preview threads have follow-ups posted in their threads.
New findings, inline below:
debug profileleaves the profiler active when the closure is stale.job spawnreports a stale-closure error from the background thread instead of returning it.- The YAML help string contains a run of spaces.
- Three call sites look up the block twice.
- Three changed paths have no regression test.
The release notes say to yaml --serialize and to msgpack --serialize degrade to a placeholder. They return an error: closure_block_not_found for YAML and CantConvert for msgpack. Only to text, to toml and describe --detailed produce a placeholder.
I reviewed by reading the code at 25c14a6. CI is green on that commit. I didn't build it or run the tests.
a740756 to
a04bea0
Compare
fdncred
left a comment
There was a problem hiding this comment.
Notes from my clanker
Review of a04bea0
I built this commit and ran cargo test -p nu-command --test tests -- commands::job (45 pass). I also drove the REPL through a pty and fed a job a closure parsed after it was spawned, then ran it through about 50 consumers (items, reduce, insert, tee, par-each, the to serializers, view ir, describe -d, table and so on). None of them panic. Root CI was still pending when I checked.
Six of the seven open threads are fixed, and I resolved them. I left a note on the par-each thread about leftover clones. The tests thread stays open: default is reachable from a script, see the reply there.
New findings, inline below:
- Errors from sites that use the closure value's span render
OutOfBoundsinstead of a label in the job. job spawnkeeps a second copy ofhead.closure_aritytakes a span that its only caller sets toSpan::unknown().- The combined consumer test reports only
falsewhen it fails.
| id | ||
| }; | ||
|
|
||
| let call_head = call.head; |
There was a problem hiding this comment.
call_head is a copy of head from line 56. Span is Copy, so the move closure can capture head directly.
There was a problem hiding this comment.
Dropped; the thread now captures head directly.
a04bea0 to
784e472
Compare
|
ok, please fix the conflicts so we can try to move on with this. thanks! |
784e472 to
3b2d8f9
Compare
|
Conflicts fixed and rebased onto current main (single commit as before). The Everything green on my side: the recursion tests, the job/par-each/group-by suites, fmt and clippy. Only the usual Windows symlink-privilege failures in the full run. |
|
ugh, more conflicts. sorry. |
When a job is spawned before a later input is parsed, the job retains an older EngineState. The later closure's BlockId is therefore unavailable in that job, which previously panicked in EngineState::get_block. Closure evaluator construction is now fallible across all public entry points and every closure-consuming command propagates the error, so this condition returns 'Closure cannot be evaluated' with a span and actionable help instead of panicking. - remove the infallible ClosureEval / ClosureEvalOnce constructors and make every try_new* constructor take a span pointing at the failing call or closure value - add EngineState::get_closure_block as the single fallible lookup with a span and help text; direct get_block callers on runtime closure values (tee, with-env, explain, view source, record where, the hook runner and the tui closure_arity) now report the error too - degrade gracefully where a best-effort string is enough: describe --detailed, the to text / to toml / to yaml / to msgpack serializers and Closure::coerce_into_string - par-each checks the block once before consuming input (skipping empty input like each) instead of yielding one error value per item - default propagates the construction error instead of storing it as the default value - generate looks the block up once through try_new - convert the record commands from nushell#19190 to the fallible constructors - add regression tests that reproduce the interactive, multi-input job spawn / job send flow from nushell#19147 through do, each, zip, tee, par-each, record each, view source, describe --detailed, to text --serialize, and a combined test for with-env, explain, generate, record where, record walk and sort-by Fixes nushell#19147
3b2d8f9 to
039a899
Compare
|
No worries 🙂 Rebased again — still a single commit. Only real overlap was |
…nfig The prompt resolver takes over the `ClosureEvalOnce::try_new` call that nushell#19159 gave `get_prompt_string`, so a prompt closure whose block is gone is still reported instead of panicking. `commandline set-prompt` builds its push as a `PromptContents` the way nushell#19236 does, with `vi_visual` added. The deprecation note in `doc_config.nu` names 0.118.0, the release this now lands in.



Description
When a job is spawned before a later input is parsed, the job retains an older
EngineState. The later closure'sBlockIdis therefore unavailable in that job, which previously panicked inEngineState::get_block.ClosureEval/ClosureEvalOnceconstructors and make everytry_new*constructor take a span pointing at the failing call or closure value, so a staleBlockIdcan no longer panic through any entry point (do,each,zip,where, …)EngineState::get_closure_blockas the single fallible lookup with a span and actionable help (sharedMISSING_CLOSURE_BLOCK_HELPconst); directget_blockcallers on runtime closure values (tee,with-env,export-env,explain,view source,record where,job spawn,debug profile, the hook runner and the tuiclosure_arity) now report the same error instead of panickingdebug profilechecks the block before installing the profiler, andjob spawnchecks before registering the job, so the error reaches the caller'stry/catchinstead of the background threaddescribe --detailedomits thesignaturekey,to text/to toml --serializefall back to the existing placeholder string,to yaml --serializereturns theclosure_block_not_founderror, andClosure::coerce_into_string(to msgpack,to kdl) returnsCantConvertpar-eachbuilds the evaluator once inrun(skipping empty input likeeach) and clones it into the workers, returning a single error instead of one error value per itemdefaultpropagates the construction error instead of storing it as the default valuegeneratereads the signature from the evaluator'sblock()instead of looking the block up twiceClosureEval/ClosureEvalOnceexposeblock(), removing the remaining second lookups inrecord where, the tuicall_closureandgeneraterecordcommands fromrecordfamily of commands #19190 to the fallible constructorsjob spawn/job sendflow from Panics executing a closure received from the main process in a job #19147 throughdo,each,zip,tee,par-each,record each,view source,describe --detailed,to text --serialize,debug profile,job spawn, and a combined test forwith-env,explain,generate,record where,record walkandsort-byRemoving the infallible constructors is a breaking change to nu-engine's public API without a deprecation step: a deprecated shim cannot avoid the panic, which is what this PR removes, so the break is intentional.
User-facing changes (Release notes)
Report missing closure blocks instead of panicking
Fixed a bug where using a closure created after a job was spawned could crash the job. Nushell now reports
Closure cannot be evaluatedwith a source span and helpful hint instead. Errors fromjob spawnanddebug profilereach the caller'stry/catch. For unavailable closures,par-eachreturns a single error rather than an error per item, anddefaultpropagates the error instead of storing it as the default value.Inspecting or serializing these closures no longer crashes:
describe --detailedomits thesignaturekey,to textandto toml --serializeuse the existing placeholder string,to yaml --serializereportsclosure_block_not_found, andto msgpackandto kdlreport conversion errors.Additional notes
recordcommands merged inrecordfamily of commands #19190, which called the removed constructorsValidation
cargo test -p nu-command --test tests job(44 passed; do / each / zip / tee / par-each / record each / record where / record walk / view source / describe / to text / debug profile / job spawn / with-env / explain / generate / sort-by scenarios)cargo test -p nu-command --test tests(3438 passed; the 21 failures are pre-existing Windows symlink-privilege environment failures, confirmed identical on upstream main)cargo testfor nu-engine, nu-cmd-lang, nu-protocol, nu-tui, nu-cmd-base, nu-color-config, nu-heavy-utils, nu-cli, nu-cmd-extra, nu-lsp, nu-dap, nu-plugin-engine (only the same symlink-privilege failures, confirmed on upstream main)cargo test -p nu-engine --doc,cargo test -p nu-protocol --doccargo fmt --all -- --checkcargo clippy -p nu-engine -p nu-cmd-lang -p nu-command -p nu-cli -p nu-cmd-extra -p nu-color-config -p nu-plugin-engine -p nu-tui -p nu-protocol -p nu-cmd-base -p nu-heavy-utils --tests --no-deps -- -D warnings