Repository navigation
fix(alias): unify typo error format across config alias show/dry-run - #2306
Conversation
`wt config alias show <typo>` and `wt config alias dry-run <typo>` rendered as a custom anyhow gutter (exit 1) while `wt <typo>` and `wt step <typo>` used clap's native `InvalidSubcommand` error (exit 2). A user hitting the same typo at different surfaces saw two different error shapes. Replace the anyhow path with a manual render that mirrors clap's visual layout (bold red `error:`, green `tip:`, bold+underline `Usage:`, exit 2) but uses "unrecognized alias" / "some similar aliases exist" wording — the positional is an alias name, not a subcommand. Clap's `InvalidSubcommand` bakes "subcommand" into its message, so manual rendering is the only way to keep the shape while tightening the wording. Add a dry-run typo snapshot (the fourth surface was untested) and refresh the two existing show snapshots to match the new format. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
One regression vs. the pre-PR behavior: the new unknown_alias_exit emits raw ANSI escapes regardless of terminal support (no TTY, NO_COLOR=1, etc.). The pre-PR anyhow path rendered via worktrunk styling and dropped colors when appropriate; wt step <typo> (clap-native path) also drops them correctly. This one doesn't — detail in the inline comment.
Minor nit (not flagging inline): did_you_mean can return a single candidate, but the tip is hardcoded to "some similar aliases exist: …". Clap uses "a similar subcommand exists: 'x'" for the 1-match case. The current snapshots only exercise the multi-match case, so the singular form is untested and reads slightly off when only one match survives the jaro_winkler > 0.7 filter.
…works The manual `ceprintln!`-based render forced ANSI escapes into the output regardless of terminal support. Piped stderr, `NO_COLOR=1`, and non-TTY consumers all saw raw escape sequences — a regression vs. both the pre-PR anyhow path and the clap-native `wt step <typo>` surface, which drop colors correctly. Build a real `clap::Error` with `ErrorKind::InvalidSubcommand`, render it via clap, then substitute `subcommand(s)` → `alias(es)` in the final string. Write through `anstream::AutoStream::auto(stderr)` so TTY and `NO_COLOR` are respected. Side benefit: clap's singular-vs-plural phrasing flows through unchanged — a 1-match case now reads "a similar alias exists: 'X'" instead of hardcoded plural. Add a singular-match snapshot so the new phrasing is under test. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Two minor notes on the new unknown_alias_exit.
Substring replacement can corrupt user input. .replace("subcommand", "alias") runs against the full rendered string, including the echo of the user's typo and any suggestion names. If a typo or alias happens to contain the literal substring subcommand (e.g., wt config alias show my-subcommand), clap's echo becomes unrecognized alias 'my-alias'. Narrowing to clap's fixed phrases avoids touching user-supplied text:
.replace("unrecognized subcommand", "unrecognized alias")
.replace("similar subcommands", "similar aliases")
.replace("similar subcommand", "similar alias");Plural subcommands still needs to precede singular subcommand (the existing docstring rationale continues to apply — "similar subcommands" otherwise becomes "similar aliass").
process::exit(2) bypasses finish_command. Other printed-already exit paths (e.g., src/main.rs:595) return WorktrunkError::AlreadyDisplayed { exit_code: 2 }.into() so main still runs output::terminate_output (ANSI reset for shell integration) and diagnostic::write_if_verbose. The pre-PR return Err(unknown_alias_error(...)) flowed through that path; process::exit skips it. Changing the signature back to -> anyhow::Result<()> and returning AlreadyDisplayed after the write! would keep this aligned with the rest of the codebase.
(Apologies for the now-deleted test-inline earlier — that was a calibration probe for the suggestion-line resolution that GitHub's API was rejecting; falling back to the review body.)
…played
Two issues the reviewer flagged on the first round of rendering fixes:
- The broad `.replace("subcommand", "alias")` ran against clap's full
rendered output, including the echoed typo and suggestion names. An
alias literally containing `subcommand` (e.g. `my-subcommand`) would
be mangled to `my-alias` when echoed. Narrow to clap's fixed phrases
(`unrecognized subcommand`, `similar subcommands`, `similar
subcommand`) so user input is never touched.
- `process::exit(2)` skipped main's `finish_command` cleanup —
`terminate_output` (ANSI reset for shell integration) and
`diagnostic::write_if_verbose` no longer ran. Return
`WorktrunkError::AlreadyDisplayed { exit_code: 2 }` instead, matching
the established pattern at `src/main.rs:595`.
Add a snapshot covering an alias whose name contains `subcommand` so a
future broadening of the substitution scope fails the test.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`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]>
## 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 - [x] `cargo check --bin wt` passes - [x] `cargo clippy --bin wt` clean - [x] Typo/suggestion integration tests pass (`nested_subcommand`, `step_alias` unrecognized cases) - [x] `help::` integration tests still pass — pre-dispatch parse path untouched 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
… 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]>
Summary
Four alias-typo surfaces rendered in two formats. After #2304's round-2 review, this aligns
config alias show/dry-runwithwt <typo>andwt step <typo>.Before:
wt deplyo/wt step deplyo→ clap-nativeerror: unrecognized subcommand 'X'+tip: …, exit 2wt config alias show deplyo/dry-run deplyo→ custom anyhow gutter✗ unknown alias 'X'+ gutter bars, exit 1After (all four surfaces):
error:/tip:/Usage:layout, exit 2config alias show/dry-runsay "alias" / "aliases" instead of "subcommand" / "subcommands" — the positional is an alias name, not a subcommandHow
unknown_alias_errorbuilds a realclap::ErrorwithErrorKind::InvalidSubcommand, renders it through clap, then substitutes clap's fixed phrases (unrecognized subcommand→unrecognized alias,similar subcommands→similar aliases,similar subcommand→similar alias). Writing throughanstream::AutoStream::auto(stderr)gets NO_COLOR / TTY detection and clap's singular-vs-plural phrasing automatically. ReturnsWorktrunkError::AlreadyDisplayed { exit_code: 2 }somain'sfinish_commandstill runsterminate_output+write_if_verbose.Substitutions are deliberately narrow — an alias name or typo containing the literal substring
subcommand(e.g.my-subcommand) is echoed verbatim, not mangled.Changes
src/commands/config/alias.rs: replace the anyhow-returningunknown_alias_error(exit 1, custom gutter) with a clap-rendering version (exit 2, matches clap-native surfaces). Parameterized bysubso the Usage line readswt config alias <sub> <NAME>.test_config_alias_dry_run_unknown_suggests,test_config_alias_show_unknown_singular_suggestion,test_config_alias_show_unknown_preserves_user_input.test_config_alias_show_unknown_suggestsandtest_config_alias_show_unknown_no_suggestionssnapshots.Test plan
cargo test --test integration step_alias(58 pass)cargo test --lib --bins(598 pass)pre-commit run --all-filescleanNO_COLOR=1 | cat; preserved underCLICOLOR_FORCE=1)subcommandechoed verbatim under typo