Repository navigation
make string hooks behave like closure ones - #19600
Conversation
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
One more: string hooks print and swallow runtime errors that closure hooks return to the caller.
Findings: 0
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.
welcome to the future
|
yup, clippy is objecting to your code |
|
should be fine now, i hope |
|
unfortunately not. now there are also conflicts |
There was a problem hiding this comment.
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
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.
|
what is going on with the let alone i didn't touch that area p.s. turns out i didn't run all workspace tests |
|
ok, this is indeed caused by |
|
it's always windows. |
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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
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), |
There was a problem hiding this comment.
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 -iThe 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:
| 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.
Co-authored-by: Darren Schroeder <[email protected]>
|
This was meant to be merely a refactor and now I'm being tortured by a clanker to write code for its hallucinations |
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_hereshould exit with the So it should probably return the error from |
|
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 |
|
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 }
42And 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) |
|
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. |
There was a problem hiding this comment.
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
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.
|
Thanks |







Description
pure refactor, semantically everything (i hope) should be the same.removed like 50 lines of code duplication (eval_string_hook), also changedlet mut output; match { x => output = ... }to just `let output = match { ... } and eventually went to also fix some stuff, see release notesUser-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.