Skip to content

fix(alias): run finish_command cleanup on typo error paths - #2307

Merged
max-sixty merged 1 commit into
mainfrom
alias-did-you-mean
Apr 19, 2026
Merged

max-sixty merged 1 commit into
mainfrom
alias-did-you-mean

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

wt step <typo> and wt <typo> previously exited through enhance_and_exit_error → process::exit(2), skipping finish_command's diagnostic dump and ANSI-reset for shell integration. The same issue applied to wt config alias show/dry-run <typo> and was fixed on #2306 by returning WorktrunkError::AlreadyDisplayed so the error flows back through handle_command_failure. This PR applies the same fix to the other two typo surfaces.

  • Split enhance_and_exit_error into a shared print_enhanced_clap_error helper plus a non-diverging enhance_clap_error that returns AlreadyDisplayed with clap's exit code.
  • Route wt step <typo> (via unknown_step_command_error) and wt <typo> (via handle_custom_command) through enhance_clap_error.
  • The pre-dispatch parse path in parse_cli keeps the diverging variant (no finish_command setup yet at that point).

User-visible output is unchanged: err.print() (called by the shared helper when no nested-subcommand tip applies) produces the same stderr content as err.exit() minus the process exit, and err.exit_code() returns 2 for InvalidSubcommand errors.

Test plan

  • cargo check --bin wt passes
  • cargo clippy --bin wt clean
  • Typo/suggestion integration tests pass (nested_subcommand, step_alias unrecognized cases)
  • help:: integration tests still pass — pre-dispatch parse path untouched

🤖 Generated with Claude Code

`wt step <typo>` and `wt <typo>` previously exited through
`enhance_and_exit_error` → `process::exit(2)`, skipping
`finish_command`'s diagnostic dump and ANSI-reset for shell integration.
The same issue applied to `wt config alias show/dry-run <typo>` and was
fixed on #2306 by returning `WorktrunkError::AlreadyDisplayed` so the
error flows back through `handle_command_failure`. Apply the same fix to
the other two typo surfaces.

Split `enhance_and_exit_error` into a shared `print_enhanced_clap_error`
helper plus a non-diverging `enhance_clap_error` that returns
`AlreadyDisplayed` with clap's exit code. The pre-dispatch parse path in
`parse_cli` keeps the diverging variant (no `finish_command` setup yet).

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit b26a0a6 into main Apr 19, 2026
25 checks passed
@max-sixty
max-sixty deleted the alias-did-you-mean branch April 19, 2026 23:06
max-sixty added a commit that referenced this pull request Apr 19, 2026
… sites (#2308)

## Summary

Follow-up from #2306/#2307. Three surfaces all built the same clap
`InvalidSubcommand` error with identical recipes (insert name +
suggestions + usage context, render):

- `wt <typo>` — `unrecognized_subcommand_error`
(`src/commands/custom.rs`)
- `wt step <typo>` — `unknown_step_command_error`
(`src/commands/alias.rs`)
- `wt config alias show/dry-run <typo>` — `unknown_alias_error`
(`src/commands/config/alias.rs`)

Consolidate into two shared helpers in `src/commands/mod.rs`:

- `build_invalid_subcommand_error(anchor, name, suggestions) ->
clap::Error` — builds the error with `InvalidSubcommand` /
`SuggestedSubcommand` / `Usage` context populated.
- `similar_subcommands(name, parent, alias_names) -> Vec<String>` —
filters visible built-ins + alias names through `did_you_mean`. Moved
from `custom.rs`; `unknown_step_command_error` used to re-implement this
inline.

`unknown_alias_error` keeps its unique `unrecognized subcommand` →
`unrecognized alias` string rewrite and direct-print path, but delegates
the clap error construction to the shared builder.

Net: **-12 lines**, three near-duplicate recipes collapsed to one. All
snapshots pass unchanged — the rendered output is identical.

## Test plan

- [x] `cargo run -- hook pre-merge --yes` — all 3282 tests pass, lints
clean, no snapshot drift

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants