Skip to content

make string hooks behave like closure ones - #19600

Merged
fdncred merged 18 commits into
nushell:mainfrom
pheenty:hooks
Nov 23, 2026
Merged

fdncred merged 18 commits into
nushell:mainfrom
pheenty:hooks

Conversation

@pheenty

@pheenty pheenty commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

pure refactor, semantically everything (i hope) should be the same. removed like 50 lines of code duplication (eval_string_hook), also changed let mut output; match { x => output = ... } to just `let output = match { ... } and eventually went to also fix some stuff, see release notes

User-facing changes (Release notes)

Did you know that you could cause internal panic with $env.config.hooks.command_not_found = "hello"; try { world }? Well now you can't. String hooks should now match the behavior of closure ones, for example they no longer are just printed and then swallowed, so you can correctly catch them.
The only difference remaining is formatting of errors happening inside the hook itself - string hooks can only point at their source string in general, whereas closure hooks point at the exact place the error occurred.

@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

Every hook config I tried behaves the same as main except one: a record-form string code now runs with the caller's pipe redirections cleared, which changes what command_not_found does with an external's output.

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

Open (2)

Notes from my clanker, checked on 2c52927. Built the branch and its base 16817a8, and ran 20 hook configs through nu -c on both (command_not_found in string, closure, record and list form, display_output in string and record form, and the error paths in eval_hook). cargo test --test tests -- hooks passed 31/31 on the branch, and CI's clippy, fmt and tests are green on this head. env_change, pre_prompt and pre_execution only run in the REPL, so those rest on the hooks tests, not on my probes.

Comment thread crates/nu-cmd-base/src/hook.rs Resolved
Comment thread crates/nu-cmd-base/src/hook.rs Resolved

@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

One more: string hooks print and swallow runtime errors that closure hooks return to the caller.

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

Open (1)

Notes from my clanker, checked on 2c52927. Built the branch, its base 16817a8, and the branch with the suggested change. Ran 7 error cases through nu -c and failing pre_prompt/env_change hooks in a real REPL on all three. cargo test --test tests -- hooks passes 31/31 with the change.

Comment thread crates/nu-cmd-base/src/hook.rs Resolved
welcome to the future
@pheenty
pheenty requested a review from fdncred November 19, 2026 13:51
@pheenty

pheenty commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author
image of course.

@fdncred

fdncred commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

yup, clippy is objecting to your code

@pheenty

pheenty commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

should be fine now, i hope

@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

unfortunately not. now there are also conflicts

@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

The redirection and doc comment threads are fixed and resolved. The error thread is open again. On ce86c7e eval_string_hook still reports the error and returns empty output, so the release note doesn't match the code yet. 8d90d4f had the fix, and the merge with main took it out. The two notes below cover what the tests need for that fix.

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

Open (2)

Notes from my clanker, checked on ce86c7e. Built the head, and the head with main's hook.rs (ca70639) swapped in. The 20 nu -c hook configs from the first review, the 7 error cases, and the pre_prompt/env_change REPL probes print the same on both. cargo test --test tests -- hooks:: passes 56/56 on the head, 54/56 with the result fix, and 55/55 with the fix plus the test changes suggested here.

Comment thread tests/hooks/mod.rs Resolved
Comment thread tests/hooks/mod.rs Resolved
@pheenty
pheenty requested a review from fdncred November 20, 2026 16:55
@pheenty

pheenty commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

what is going on with the util::test::unlet_constant_in_nested_block_keeps_it_for_later_source test? i have all of tests green locally:

     Summary [  40.368s] 2827 tests run: 2827 passed, 21 skipped

let alone i didn't touch that area

p.s. turns out i didn't run all workspace tests

@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

okay, for some reason that test is being skipped by default?
now, main:
image
and this branch:
image

@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

ok, this is indeed caused by eval_string_hook not swallowing the error anymore, so the test is wrong on its own, my changes just revealed this and are not a regression, gotta figure out how to fix the test now

@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

it's always windows.

@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 five earlier threads are fixed. String hooks now return their errors like closures do, the release note says so, and the two new string cases fail without the fix. One new note: that change breaks the error label for a failing string command_not_found hook.

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

Open (1)

Notes from my clanker, checked on f5a8aa0. Built the head, and the head with main's hook.rs (e3bf397) swapped in. Ran the 20 nu -c hook configs, the 7 error cases, and REPL probes of pre_prompt, env_change and command_not_found hooks on both. cargo test --test tests -- hooks:: with main's hook.rs: 53 passed, and the 2 new string cases failed. With the run_external.rs change from the new thread: 55 passed. CI is green on f5a8aa0.

Comment thread crates/nu-cmd-base/src/hook.rs Resolved
@pheenty
pheenty requested a review from fdncred November 22, 2026 16:47

@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

The OutOfBounds label is gone. String command_not_found hooks print what main prints again, and the five earlier threads still hold. The OutOfBounds thread stays open for its test. One new note: the new error arm reports every hook error, not just a string hook's, so a failing closure hook now prints through try and do -i.

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

Open (1)

Notes from my clanker, checked on 48d8013. Built the head, and the head with main's hook.rs and run_external.rs (e3bf397) swapped in. Ran the 20 nu -c hook configs, the 7 error cases, 10 failing command_not_found cases (try, do -i, exit codes, the recursion guard) and REPL command_not_found probes on both. With the change in the new thread: the 20 configs, 9 of the 10 failing cases and the REPL probes print what main prints, the 7 error cases differ only where this PR means to, and cargo test --test tests -- hooks:: passes 55/55, and clippy with CI's flags is clean on nu-command. CI is green on 48d8013.

_ => {
// The hook did not return a string, so ignore it.
}
Err(err) => report_shell_error(Some(&stack), &hook_state, &err),

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 A failing closure command_not_found hook now prints through try and do -i

This comes from the fix I suggested in the other thread, and it reaches further than I said there. The new arm takes every Err the hook returns, not just a string hook's. Closure errors, bad hook config like {code: 3}, and the recursion guard's error all get printed here now, and the caller gets the generic not-found error instead. Because the command prints the error itself, try and do -i can't hold it back, and catch never sees it.

On 48d8013, with $env.config.hooks.command_not_found = {|cmd| error make {msg: boom}}:

try { ferris_is_not_here } catch {|e| $"caught: ($e.msg)" }
# main:   caught: boom
# branch: Error: nu::shell::error
#           x boom
#         caught: External command failed

do -i { ferris_is_not_here }; "after do -i"
# main:   after do -i
# branch: Error: nu::shell::error
#           x boom
#         after do -i

The hook's exit code gets lost too. With {|cmd| ^sh -c "exit 42"; null}, main prints nothing and nu -c exits 42. The branch prints the not-found error and exits 1, and $env.LAST_EXIT_CODE in a catch reads 1 where main reads 42. Six of the 20 configs from my first review ({code: 3}, {condition: "x", code: "1"}, 3, and three more) now print the not-found error after their own error.

Only a string hook has source that lives in hook_state alone. Its runtime error comes after merge_delta, so by then hook_state has one more file than engine_state. A parse error returns before that merge, with a span in the caller's source. So this keeps the report for string hooks and returns every other error, as main does:

Suggested change
Err(err) => report_shell_error(Some(&stack), &hook_state, &err),
// A string hook's code was parsed into `hook_state`, so only it can show the
// error's span. Report the error here, then give the usual not-found error.
Err(err) if hook_state.num_files() > engine_state.num_files() => {
report_shell_error(Some(&stack), &hook_state, &err)
}
Err(err) => return err,

I built it. The 20 configs, 9 of the 10 failing cases and the REPL probes print what main prints, and cargo test --test tests -- hooks:: passes 55/55. It misses one shape: a list where a string hook succeeds before a closure fails, like ["null", {|cmd| error make {msg: boom}}]. That closure's error still gets printed, because the string hook already added its file.

If you'd rather report every error, the release note needs a line for closure hooks that covers try, do -i and the exit code.

@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

This was meant to be merely a refactor and now I'm being tortured by a clanker to write code for its hallucinations
Welcome to the future I guess 🤣

@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

report_shell_error(Some(&stack), &hook_state, &err)

Actually after some thinking I don't even think it's a good idea.

$env.config.hooks.command_not_found = "error make {msg: boom}"
ferris_is_not_here

should exit with the boom error semantically because it comes first (the hook runs, and then the cmd not found error is meant to be built from its output).

So it should probably return the error from eval_hook as is, but its span should just point at a string. Not knowing what exactly in that string failed is a fair price of stringly typing if anything lol

@pheenty

pheenty commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

also this "successfully" panics now (this is this pr's HEAD).

❮ $env.config.hooks.command_not_found = "error make {msg: boom}"
❮ try { ferris_is_not_here } catch {|e| $e.msg }
Error: nu::shell::error

  × boom
   ╭─[command_not_found hook:1:12]
 1 │ error make {msg: boom}
   ·            ───────────
   ╰────

Error:   × Main thread panicked.
  ├─▶ at crates/nu-protocol/src/engine/engine_state.rs:1168:14
  ╰─▶ internal error: missing variable
  help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.

p.s. main actually does that too. wtf

@pheenty

pheenty commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Okay, now string and closure hooks should behave literally the same

❮ $env.config.hooks.command_not_found = "error make {msg: boom}"
❮ ferris_is_not_here
Error: nu::shell::error

  × boom
   ╭─[repl_entry #1:1:39]
 1 │ $env.config.hooks.command_not_found = "error make {msg: boom}"
   ·                                       ────────────┬───────────
   ·                                                   ╰── `command_not_found` hook was erroneous
   ╰────
❮ try { ferris_is_not_here } catch { 42 }
42

And closure hook works in the exact same way

The one downside before main is that we longer have this luxury with closure hooks:

❮ $env.config.hooks.command_not_found = {error make {msg: boom}}
❮ ferris_is_not_here
Error: nu::shell::error

  × boom
   ╭─[repl_entry #3:1:51]
 1 │ $env.config.hooks.command_not_found = {error make {msg: boom}}
   ·                                                   ───────────
   ╰────

(notice how this branch points at the whole hook - this now applies to closures too)
But imo this is a sane trade-off for the coherence

@pheenty pheenty changed the title refactor nu-cmd-base::hook make string hooks behave like closure ones Nov 22, 2026
@pheenty

pheenty commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author
image what is github doing

@pheenty
pheenty marked this pull request as draft November 22, 2026 23:00
@pheenty
pheenty marked this pull request as ready for review November 22, 2026 23:01
@fdncred

fdncred commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

I'm sorry there have been so many reviews but it's not like things are just barely changing. There have been lots and lots of changes and this is not an area that I'm comfortable with just winging it. Hooks are a core foundation in the repl so it has to be as right as we can get it. So, with that said, prepare for yet another re-check since there have been significant changes since the last re-check. Also, note that I only post about 50% of the findings. The clankers can definitely find problems.

@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

Six of the seven threads are fixed and resolved. A failing closure hook's error reaches try, catch and do -i as it does on main, the string command_not_found error no longer shows a broken label, and the REPL panic from the release note is gone. The try/do -i thread is open again for one case. A closure hook whose external exits 42 still makes nu -c exit 1, and it now prints an error that main hides. The new thread is about the same error arm, which keeps only each error's message.

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

Open (1)

Notes from my clanker, checked on b73e3a2. Built the head, the head with main's hook.rs and run_external.rs (933b486), and the head with the change in the new thread. Ran the 20 nu -c hook configs, the 7 error cases, 10 failing command_not_found cases, 19 cases for what the new error arm keeps, and REPL command_not_found probes (config file, try) on all three. Ran the new test against older versions of run_external.rs, cargo test --test tests -- hooks:: (58/58 with the change) and clippy with CI's flags on nu-command. On b73e3a2, CI's lint, root tests and fuzz jobs pass on Linux, macOS and Windows. The Incredibuild runner job didn't run.

@pheenty
pheenty requested a review from fdncred November 23, 2026 00:16
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:other Noted in "Other changes" section labels Nov 23, 2026
@fdncred
fdncred merged commit 3dbf050 into nushell:main Nov 23, 2026
14 checks passed
@fdncred

fdncred commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 23, 2026
@pheenty
pheenty deleted the hooks branch November 23, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:other Noted in "Other changes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants