Files
Darren Schroeder 75ead0b200 Keep failures in $env.LAST_EXIT_CODE until something resets them (#19607)
## Description

A failure now stays in `$env.LAST_EXIT_CODE` until another command sets
a code or you reset it with `$env.LAST_EXIT_CODE = 0`. Three places
dropped it. `try` without `catch` kept the old code. The REPL set 0
after every line that didn't error, so a failure the line handled never
reached the prompt. A hook that failed without catching it left the code
alone, and so did one ending in a failing external.

This works like PowerShell's `$LASTEXITCODE`, which error handling never
resets. `catch` can keep or reset the code, `finally` sees it, and `nu
-c 'try { error make {msg: x} }'` still exits 0.

A record hook with a string `code` (`{code: "..."}`) now runs like a
plain string hook. It gets the hook's input and doesn't inherit the
caller's output redirection.

Tables compare main with this branch.

| `$env.LAST_EXIT_CODE = 5`, then | main | this branch |
|---|---|---|
| `try { ^sh -c "exit 7" }` | 5 | 7 |
| `try { error make {msg: x} }` | 5 | 1 |
| `try { ^sh -c "exit 7" } catch { }` | 7 | 7 |
| `try { ^sh -c "exit 7" } catch { $env.LAST_EXIT_CODE = 0 }` | 0 | 0 |
| `try { "fine" }` | 5 | 5 |

| Line typed at the prompt | main | this branch |
|---|---|---|
| `try { ^sh -c "exit 7" }` | 0 | 7 |
| `try { ^sh -c "exit 7" } catch { }` | 0 | 7 |
| `try { ^sh -c "exit 7" } catch { $env.LAST_EXIT_CODE = 0 }` | 0 | 0 |
| `try { ^sh -c "exit 7" }; ^true` | 0 | 0 |
| `$env.LAST_EXIT_CODE = 4` | 0 | 4 |
| `ls \| ignore` | 0 | 0 |

| Hook, code at the next prompt | main | this branch |
|---|---|---|
| `pre_prompt = [{\|\| ^sh -c "exit 9"; null }]` | 0 | 9 |
| `pre_prompt = [{\|\| ^sh -c "exit 8" }]` | 0 | 8 |
| `env_change.FOO = [{\|\| ^sh -c "exit 8" }]`, then `$env.FOO = 1` | 0
| 8 |
| `display_output = [{\|\| try { ^sh -c "exit 7" } catch { } } {\|\|
table }]`, then `1` | 0 | 7 |

| Record hook with a string `code` | main | this branch |
|---|---|---|
| `display_output = {code: "$in \| describe"}`, then `[1 2 3]` |
`nothing` | `list<int>` |
| `pre_prompt = [{code: 'try { "x" }'}]` | prints `x` before each prompt
| prints nothing, like the plain string |
| `command_not_found = {code: "^echo from-hook"}`, then `try { nosuch }
catch { "caught" }` | `caught` | `from-hook`, `caught` |


## User-facing changes (Release notes)

- `try` without `catch` sets `$env.LAST_EXIT_CODE` to the code of the
failure it handles, or 1 if the error has none.
- At the prompt, `$env.LAST_EXIT_CODE` keeps a code the line set, so
`try { ^false }` shows 1 instead of 0.
- A failing `pre_prompt`, `pre_execution` or `env_change` hook sets
`$env.LAST_EXIT_CODE`, also when the failure is an external at the end
of the hook. A failing `env_change` hook runs once per change instead of
before every prompt.
- A record hook with a string `code` gets the hook's input, so
`display_output = {code: "table -e"}` prints the table. In `pre_prompt`
and `env_change` it no longer prints a value it ends with, and in
`command_not_found` an external it runs writes to the terminal.

## Additional notes

- `do -i` still sets 0 for a failing external. Eating errors is its job.
- An external at the end of a `command_not_found` or `display_output`
list hook still isn't checked. In `command_not_found`, checking it would
replace the "command not found" error with, say, `pkgfile`'s exit
status.
- After a `command_not_found` hook runs, env vars set earlier in the
same line or script are gone, `$env.LAST_EXIT_CODE` included. Main does
the same.
2026-11-20 07:58:15 -06:00
..