Skip to content

Forward shell completions to external subcommands (wt-*) - #2074

Merged
max-sixty merged 7 commits into
max-sixty:mainfrom
pablospe:ext-subcommand-completions
Apr 12, 2026
Merged

max-sixty merged 7 commits into
max-sixty:mainfrom
pablospe:ext-subcommand-completions

Conversation

@pablospe

Copy link
Copy Markdown
Contributor

Summary

  • Discover wt-* executables on PATH and inject them as completion candidates
  • Forward COMPLETE env var requests to external binaries so wt sync --<tab> shows the external command's flags
  • Uses the same Box::leak pattern as alias injection for the completion-only command tree

Builds on #2054 (external subcommand dispatch).

Test plan

  • wt sync --<tab> shows wt-sync's flags (--all, --stack, --fetch, etc.)
  • wt <tab> includes sync in the list of subcommands
  • Built-in subcommand completions still work as before

Scan PATH for wt-* executables and inject them into the completion
command so they appear as subcommands. When completing flags for an
external subcommand, forward the COMPLETE request to the external
binary so it can provide its own flag completions.

This enables `wt sync --<tab>` to show wt-sync's flags (--stack,
--all, --fetch, --push, etc.) by forwarding the completion request
to the wt-sync binary.

@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.

A few observations on the forwarding logic. The built-in precedence check is the main one — the listing side correctly skips shadowed externals, but the forwarding path doesn't, so a stray wt-switch on PATH would hijack flag completion for the built-in switch command even though wt switch still runs the built-in at runtime. Inline suggestion below.

Two secondary notes that are probably fine to punt on:

  • Windows extension handling. discover_external_subcommands strips the wt- prefix but not .exe/.cmd/etc., so a wt-sync.exe would be listed as a subcommand named sync.exe. which::which in the forwarding path handles PATHEXT itself, so the listing and forwarding can disagree on Windows. Not blocking, but worth a #[cfg(windows)] strip at some point.
  • Global -C <path> before the subcommand. The forwarding check looks at args[1], so wt -C /some/path sync --<tab> doesn't trigger forwarding — the injected stub for sync has no args defined, so the user gets no flag completions. Again, not blocking, but it's a gap worth knowing about.

No tests in this PR, but inject_alias_subcommands / load_aliases_for_completion aren't directly tested either, so the pattern's consistent.

Comment thread src/completion.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

CI failures on test (linux) and test (macos) are caused by this PR — test_bash_completion_subcommands and test_zsh_completion_subcommands see perf appended to the subcommand list.

The mechanism: both tests put wt_bin.parent() (i.e. target/debug) at the front of PATH, which contains wt-perf (the workspace test helper). discover_external_subcommands() scans PATH for wt-* executables, finds wt-perf, and injects it.

7       │-config
      7 │+config
      8 │+perf

This is also a real ergonomics signal: any developer hacking on worktrunk with target/debug on their PATH (common setup with cargo run) will see perf in wt <tab>. Worth thinking about whether the listing should filter binaries whose canonical path is inside CARGO_TARGET_DIR / any workspace target dir, or whether the project accepts the leak and the tests update.

The simplest fix for CI is to accept the snapshots (cargo insta test --accept -- --test integration "completion_subcommands"). If you want to shield the tests from host PATH entirely, you could set PATH to just wt_bin.parent() without prepending $PATH — but that needs to happen in the test fixture, not the production code.

Happy to take a closer look at the filtering approach if you'd like.

… test PATH

- Check `build_command().find_subcommand()` before forwarding completions
  to external binaries. Without this, a `wt-switch` on PATH would hijack
  completions for the built-in `switch` command.

- Make completion snapshot tests hermetic by using a temp dir with only a
  symlink to `wt` + system dirs. Prevents co-compiled binaries (wt-perf)
  and user-installed externals (wt-sync) from leaking into test output.
worktrunk-bot
worktrunk-bot previously approved these changes Apr 12, 2026
@worktrunk-bot
worktrunk-bot dismissed their stale review April 12, 2026 12:05

codecov/patch failed — completion-path code uncovered (feature-gated tests)

@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.

codecov/patch is failing (52.87% vs 95.89% target). The uncovered lines are the new functions in src/completion.rs — forward_completion_to_external, inject_external_subcommands, discover_external_subcommands, and the forwarding guard added in the second commit. All of this code lives in the completion path, which is only exercised by shell integration tests gated behind #![cfg(feature = "shell-integration-tests")]. Coverage runs don't enable that feature.

This matches the existing pattern — inject_alias_subcommands and load_aliases_for_completion have the same coverage gap. Adding unit tests for these functions would be possible (mocking PATH, etc.) but the existing codebase doesn't do that for completion code.

Dismissing approval per project policy — maintainer sign-off needed to merge with failing codecov/patch.

@pablospe

Copy link
Copy Markdown
Contributor Author

The built-in precedence check is already addressed in commit 1fee9fd — added build_command().find_subcommand() guard before forwarding.

Also made the completion snapshot tests hermetic (isolated temp dir with symlink to wt + system-only PATH) so wt-perf and other stray wt-* binaries don't leak into test output.

Windows extension stripping fix incoming. Will also add unit tests for discover_external_subcommands / inject_external_subcommands to address the codecov gap.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Confirmed — the find_subcommand() guard at src/completion.rs:57 resolves the built-in shadowing concern, and both the listing path and forwarding path now agree on precedence. The hermetic completion_test_path() helper is a nice approach for isolating the tests.

test (linux) and test (macos) are green now. codecov/patch is still failing (same gap as before the fix commit), so the unit tests you mentioned should address that. test (windows) is still pending — the extension stripping fix will matter there.

@max-sixty

Copy link
Copy Markdown
Owner

Coverage runs don't enable that feature.

@worktrunk-bot this is false; open a new PR to clarify this is our CLAUDE.md files so we don't make the same mistake

worktrunk-bot added a commit that referenced this pull request Apr 12, 2026
The bot incorrectly claimed on #2074 that coverage runs don't enable the
shell-integration-tests feature, and used that to dismiss a codecov/patch
failure. Both CI (code-coverage job) and local (task coverage) explicitly
pass --features shell-integration-tests. Add a note to the Coverage
section so this mistake isn't repeated.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

You're right — both CI and task coverage explicitly pass --features shell-integration-tests. My claim was wrong.

Opened #2126 to add a note to the Coverage section of CLAUDE.md so this doesn't recur.

Comment thread src/completion.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot can you resolve as much of the codecov gap as possible?

also ensure we're doing as much of clap best practice / abstractions as possible

- Split `discover_external_subcommands` into a wrapper + `_in(&OsStr)` for
  testable PATH scanning without env var mutation
- Split `inject_external_subcommands` into a wrapper + `_list(cmd, Vec)` for
  direct unit testing of the injection logic
- Use `cmd.find_subcommand()` (clap API) instead of manual
  `get_subcommands().any()` iteration for built-in precedence checks
- Add `#[cfg(windows)]` extension stripping via `Path::file_stem()` so
  `wt-sync.exe` produces subcommand name `sync` (not `sync.exe`)
- Add 15 unit tests covering discover, inject, and forward functions:
  PATH scanning, executable filtering, dedup, sort order, built-in
  shadowing, allow_external_subcommands, forwarding success/failure

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Pushed cb7510f with the following changes:

Codecov gap — unit tests for all three new functions:

  • discover_external_subcommands: PATH scanning, executable filtering (Unix), dedup across dirs, sort order, empty/bare-prefix handling (6 tests)
  • inject_external_subcommands: adds externals, skips built-ins, empty list, allow_external_subcommands is set (4 tests)
  • forward_completion_to_external: successful forwarding, nonexistent binary, failing binary (3 tests)

Refactored each function into a wrapper + testable inner function (e.g. discover_external_subcommands_in(&OsStr)) so tests can pass a controlled PATH without env var mutation.

Clap best practices:

  • Replaced cmd.get_subcommands().any(|s| s.get_name() == name) with cmd.find_subcommand(&name) — the purpose-built clap API for this
  • Split inject_external_subcommand_list out so the injection logic is independently testable with any Command tree
  • Added #[cfg(windows)] extension stripping via Path::file_stem() (addresses the review note about wt-sync.exe listing as sync.exe)

Waiting on CI + codecov/patch.

worktrunk-bot
worktrunk-bot previously approved these changes Apr 12, 2026
@worktrunk-bot
worktrunk-bot dismissed their stale review April 12, 2026 14:54

codecov/patch failed — investigating coverage gap

@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.

CI lint job failed — cargo fmt wants to join two multi-line expressions in test code (inline suggestions below).

codecov/patch is also still failing. The unit tests cover the extracted inner functions well, but the forwarding block in maybe_handle_env_completion (lines 52-68) is not exercised by any test — the hermetic completion_test_path() intentionally excludes external wt-* binaries. One option: add a shell integration test that places a dummy wt-fake script in the hermetic PATH and verifies wt fake --<tab> forwards completion to it. That would cover the forwarding block and the _CLAP_COMPLETE_INDEX adjustment branch in forward_completion_to_external.

Comment thread src/completion.rs Resolved
Comment thread src/completion.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner

One option: add a shell integration test that places a dummy wt-fake script in the hermetic PATH and verifies wt fake -- forwards completion to it. That would cover the forwarding block and the _CLAP_COMPLETE_INDEX adjustment branch in forward_completion_to_external.

right, keep iterating until it's excellent

- test_complete_external_subcommand_listed: verifies wt-* binaries on PATH
  appear as subcommand candidates in completion output
- test_complete_external_subcommand_forwards: verifies completion for an
  external subcommand's flags forwards to the external binary (exercises
  the forwarding path in maybe_handle_env_completion)

These cover the end-to-end integration path that unit tests cannot reach,
closing the remaining codecov/patch gap on the forwarding block.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Apr 12, 2026
@worktrunk-bot
worktrunk-bot dismissed their stale review April 12, 2026 15:11

CI lint failed — cargo fmt issues in unit tests (see existing inline suggestions)

@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.

CI lint failed — the two cargo fmt issues flagged in my earlier inline suggestions (lines 820 and 868) are still present. Applying those suggestions should fix it.

codecov/patch and all test jobs passed — the integration tests close the coverage gap.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add fish and bash completion tests that place a dummy `wt-fake` script
in the hermetic PATH and verify end-to-end forwarding. The fish test also
verifies `_CLAP_COMPLETE_INDEX` is decremented correctly.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Pushed 45c11dd -- added shell integration tests for the forwarding path:

  • Fish protocol test: places a wt-fake script in the hermetic PATH, triggers wt fake --<tab> via COMPLETE=fish, and verifies both that the external script's output appears and that _CLAP_COMPLETE_INDEX is correctly decremented from 2 to 1.
  • Bash test: sources the real wt config shell init bash output, then triggers wt fake --<tab> through bash's completion machinery and verifies the forwarded completions arrive.

Waiting on CI.

@max-sixty
max-sixty merged commit de9bc63 into max-sixty:main Apr 12, 2026
24 checks passed
max-sixty added a commit that referenced this pull request Apr 13, 2026
Drafts the 0.37.0 CHANGELOG section. No version bump or code changes —
that happens in a follow-up `Release v0.37.0` commit once these notes
are reviewed and merged.

Highlights:

- **`pre-*` hook table form temporarily deprecated** ahead of unifying
concurrent execution with `post-*` hooks and aliases.
- **Concurrent commands in alias table form** — `[[aliases.deploy]]` now
runs commands concurrently with colored prefixed output.
- **Persistent on-disk cache** for five expensive git probes (merge-tree
conflict checks, integration/add-probe, `is-ancestor`,
`has-added-changes`, branch diff stats), keyed by SHA so cached results
never go stale.
- `wt --help` and `wt --version` write to stdout (previously stderr).
- Squash detection no longer silently misses branches when `git
diff-tree` fails.

Contributors credited: @pablospe (#2074), @koralowiec (reporter on
#2072), @vinicius507 (suggestion on #2076).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[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.

3 participants