Skip to content

fix(alias): unify typo error format across config alias show/dry-run - #2306

Merged
max-sixty merged 3 commits into
mainfrom
alias-did-you-mean
Apr 19, 2026
Merged

max-sixty merged 3 commits into
mainfrom
alias-did-you-mean

Conversation

@max-sixty

@max-sixty max-sixty commented Apr 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

Four alias-typo surfaces rendered in two formats. After #2304's round-2 review, this aligns config alias show/dry-run with wt <typo> and wt step <typo>.

Before:

  • wt deplyo / wt step deplyo → clap-native error: unrecognized subcommand 'X' + tip: …, exit 2
  • wt config alias show deplyo / dry-run deplyo → custom anyhow gutter ✗ unknown alias 'X' + gutter bars, exit 1

After (all four surfaces):

  • Same clap-native error: / tip: / Usage: layout, exit 2
  • config alias show/dry-run say "alias" / "aliases" instead of "subcommand" / "subcommands" — the positional is an alias name, not a subcommand

How

unknown_alias_error builds a real clap::Error with ErrorKind::InvalidSubcommand, renders it through clap, then substitutes clap's fixed phrases (unrecognized subcommand → unrecognized alias, similar subcommands → similar aliases, similar subcommand → similar alias). Writing through anstream::AutoStream::auto(stderr) gets NO_COLOR / TTY detection and clap's singular-vs-plural phrasing automatically. Returns WorktrunkError::AlreadyDisplayed { exit_code: 2 } so main's finish_command still runs terminate_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-returning unknown_alias_error (exit 1, custom gutter) with a clap-rendering version (exit 2, matches clap-native surfaces). Parameterized by sub so the Usage line reads wt config alias <sub> <NAME>.
  • New tests: test_config_alias_dry_run_unknown_suggests, test_config_alias_show_unknown_singular_suggestion, test_config_alias_show_unknown_preserves_user_input.
  • Refreshed test_config_alias_show_unknown_suggests and test_config_alias_show_unknown_no_suggestions snapshots.

Test plan

  • cargo test --test integration step_alias (58 pass)
  • cargo test --lib --bins (598 pass)
  • pre-commit run --all-files clean
  • Manual smoke on all four paths (exit 2; ANSI stripped under NO_COLOR=1 | cat; preserved under CLICOLOR_FORCE=1)
  • Aliases containing subcommand echoed verbatim under typo

`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 worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/commands/config/alias.rs Resolved
…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]>

@worktrunk-bot worktrunk-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@max-sixty
max-sixty merged commit 24181f2 into main Apr 19, 2026
25 checks passed
@max-sixty
max-sixty deleted the alias-did-you-mean branch April 19, 2026 22:27
max-sixty added a commit that referenced this pull request Apr 19, 2026
`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 added a commit that referenced this pull request Apr 19, 2026
## 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]>
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