Skip to content

fix(shell): register completions under the --cmd name, not clap's - #3817

Merged
max-sixty merged 3 commits into
mainfrom
fix/issue-3816
Aug 13, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/issue-3816

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

wt config shell init <shell> --cmd <name> renames the shell wrapper and its lazy completion loader, but the registration that loader evals comes from clap, which derives every identifier in it from its own compile-time Command name (wt) — not from argv[0] and not from --cmd. The two halves never agreed:

$ wt config shell init zsh --cmd wot | grep _clap
        if ! (( $+functions[_clap_dynamic_completer_wot] )); then
        _clap_dynamic_completer_wot "$@"

$ COMPLETE=zsh wt | grep -oE '_clap_dynamic_completer_[a-z_]*' | sort -u
_clap_dynamic_completer_wt

Nothing completed, and because the guard never became true the completion script was regenerated and re-evaluated on every TAB. Same shape in bash (_clap_complete_*); PowerShell emitted Register-ArgumentCompleter -Native -CommandName wt, so the --cmd name was never registered at all. The documented --cmd=git-wt case (the Windows Terminal conflict) was broken too — including for a binary genuinely installed under that name, since clap's name comes from the declaration rather than argv[0].

There is a second, sharper edge: zsh's registration ends with compdef <completer> <cmd>, so the first TAB on wot also bound worktrunk's completer to plain wt — handing completions to the other wt that --cmd exists to step around.

fish and nushell were unaffected. Both register a completer that shells out to the binary rather than depending on a clap-emitted identifier, so the reporter's "unverified" row for fish is a pass.

Solution

The bash, zsh, and PowerShell loaders now pass the name they bind in WORKTRUNK_COMPLETE_NAME, and registration_name() in src/completion.rs emits the registration under that name (validated through the same validate_shell_command_name guard --cmd uses, since the value lands verbatim in generated shell code). The fallback is binary_name(), which covers a binary installed as git-wt and invoked directly. The templates apply clap's own - → _ escaping to the function they call, so --cmd git-wt guards on _clap_complete_git_wt rather than the invalid _clap_complete_git-wt.

That fixes all four shells and the stray compdef in one place, rather than pinning the templates to clap's internal naming:

$ WORKTRUNK_COMPLETE_NAME=wot COMPLETE=zsh wt | grep -oE '_clap_dynamic_completer_[a-z_]*|compdef .*' | sort -u
_clap_dynamic_completer_wot
compdef _clap_dynamic_completer_wot wot

Testing

Two reproduction tests in tests/integration_tests/completion.rs, both failing before the change:

  • test_init_custom_cmd_defines_clap_completer_in_bash drives the whole chain through a real bash — generate the init script with --cmd, call the loader it defines, then assert clap's completer function exists afterwards. Printed MISSING before, DEFINED after. Cases for wot and git-wt.
  • test_completion_registration_uses_shell_integration_cmd_name covers zsh and PowerShell, which CI can't drive: the identifier the init script references must be the one the registration defines, and the compdef / -CommandName target must be the --cmd name.

cargo test --lib --bins and cargo test --test integration are otherwise green (one unrelated failure locally, test_copy_ignored_preserves_file_executable_permissions, from this sandbox's umask 0002), and cargo clippy --all-targets --all-features / cargo fmt --check are clean.


Closes #3816 — automated triage

`wt config shell init <shell> --cmd <name>` renamed the wrapper and the
lazy completion loader, but the registration the loader evals comes from
clap, which derives every identifier in it from its own compile-time
`Command` name (`wt`). The loader guarded on and called
`_clap_dynamic_completer_wot` while the eval only ever defined
`_clap_dynamic_completer_wt`: nothing completed, and since the guard
never became true the script was regenerated and re-evaluated on every
TAB. Same shape in bash (`_clap_complete_*`) and PowerShell, whose
`Register-ArgumentCompleter -CommandName wt` never registered the
`--cmd` name at all. The documented `--cmd=git-wt` case was broken too,
including for a binary genuinely installed under that name — clap's name
comes from the declaration, not `argv[0]`.

zsh's registration also ends with `compdef <completer> <cmd>`, so the
first TAB bound worktrunk's completer to plain `wt` — actively wrong in
the situation `--cmd` exists to solve, where another `wt` owns that name.

The bash, zsh, and PowerShell loaders now pass the name they bind in
`WORKTRUNK_COMPLETE_NAME`, and the registration is emitted under it;
the fallback is the binary name, which covers a `git-wt` install invoked
directly. The templates apply clap's own `-` → `_` escaping to the
function they call. fish and nushell were unaffected — neither depends
on a clap-emitted identifier.

Closes #3816

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Two notes, both minor — the fix itself traces correctly. I confirmed against clap_complete 4.6.9's env/shells.rs that name drives the bash/zsh completer identifier (name.replace('-', "_")) and bin drives complete … BIN, compdef … BIN, and -CommandName, so setting both via .name()/.bin() is what actually moves all three. The fish claim holds for a different reason than the identifier one: fish_completion_content in src/commands/configure_shell.rs hand-writes complete --command {cmd}, so fish never consumes clap's registration at all.

One gap worth a test rather than a fix: nothing exercises the filter(…) reject branch. A case with an invalid WORKTRUNK_COMPLETE_NAME asserting the fallback would pin the guard that keeps unvalidated text out of generated shell code.

Comment thread src/completion.rs Resolved
Comment thread src/shell/mod.rs Resolved
Address review on #3817:

- `registration_name` validated only the `WORKTRUNK_COMPLETE_NAME` arm; the
  `binary_name()` fallback is `argv[0]`'s file stem, which no `--cmd` gate has
  seen. A binary installed under a name outside the allowlist emitted
  `complete -F _clap_complete_<name>` with an identifier the shell can't parse,
  so completions silently did nothing. `handle_completions` guards the same
  value for the same reason; the last resort is the compile-time `wt`.
- `make_zsh_autoload_safe` hand-rolled the same `-` → `_` escaping the new
  `clap_completer_ident` encodes. One home for clap's rule, so a change to it
  is one edit rather than two sites that drift apart silently.
- Cover the reject branches: an invalid `WORKTRUNK_COMPLETE_NAME` falls back to
  the binary name without reaching the generated script, and an invalid binary
  name falls back to `wt`.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Both notes from the last review are addressed correctly. Making clap_completer_ident pub and routing make_zsh_autoload_safe through it does leave clap's escaping rule with one home, and the snapshots confirm the output didn't move; the new registration_name fallback chain and its two reject-branch tests close the untested-filter gap, and preferring a valid binary_name over the constant is the better answer than the suggestion I'd written.

One new observation on the registration branch, inline.

Comment thread src/completion.rs Resolved
The registration branch hands clap one string. `write_registration` in
clap_complete 4.6.9 reads `cmd.get_name()` and `self.bin`, so building the
full completion tree there cost a `Repository::current()` fork, both config
loads, and a `PATH` scan for `wt-*` on the first TAB of every new shell.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Check status on 0b62f04, at the end of my poll budget: test (linux), lint, fast-checks, feature-check, check-docs, code-coverage and codecov/patch are green; test (macos) and test (windows) (both required) and the advisory macos/windows affected-test legs were still running, so I haven't verified them.

@max-sixty
max-sixty merged commit 96c6c84 into main Aug 13, 2026
39 checks passed
@max-sixty
max-sixty deleted the fix/issue-3816 branch August 13, 2026 23:44
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
worktrunk 0.74.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## Release Notes

### Improved

- **A JSON bool or null nested in a `vars` value renders as `True`, `False`, or `None`**: minijinja 2.22 adopted Jinja2's spelling for all three, so `{{ vars.<key>.<field> }}` into an object set by `wt config state vars set` prints those words. A var whose whole value is `true` stays a string. (Breaking: the old spellings were `true`, `false`, and `none`.) ([#3795](max-sixty/worktrunk#3795))

- **`wt config approvals add --yes` records approvals without a terminal**: the command refused every non-interactive run, so a container or CI job could not pre-approve a project it had just cloned. `--yes` now lists what it trusts and writes it, and a save it cannot make fails the command rather than warning and exiting 0. ([#3819](max-sixty/worktrunk#3819))

- **`remote_repo` names the repository as the remote spells it**: `repo` is the directory on disk, so a renamed clone reports the new name. `{{ remote_repo }}` takes it from the primary remote's URL, available everywhere `owner` is and unset when no remote parses. ([#3745](max-sixty/worktrunk#3745), thanks @canac)

### Fixed

- **`wt remove --force` deleted a live worktree moved onto another's registered path**: the ownership gate asked which *repository* the occupant belonged to, which a sibling worktree of this repo satisfies, so the directory went to trash with its uncommitted work. The directory and the registration must now name each other, as `git worktree remove` requires. Also reaches `wt merge`, `wt step prune`, and picker removal, and refuses a bare repository's path that `--force` previously deleted. ([#3808](max-sixty/worktrunk#3808))

- **`wt step copy-ignored` skips a source file that vanishes mid-copy**: the walk collects leaves, then copies them in parallel, so a file removed in between — a concurrent build rewriting `target/`, say — aborted the batch, under `--force` after deleting the destination. The source is stat'd before anything is removed now. ([#3744](max-sixty/worktrunk#3744), fixes [#3743](max-sixty/worktrunk#3743), thanks @dataders for reporting and fixing)

- **`wt step promote` refuses to delete a source it did not fully copy**: across filesystems the move copies then deletes, and an entry with no copy — a socket or FIFO, or a file a concurrent build rewrote mid-walk — was deleted anyway. The copy now reports what it skipped, and a non-zero count fails the move. ([#3820](max-sixty/worktrunk#3820))

- **An `ssh://` remote's host comes from the authority, not a later path segment**: the search for the userinfo `@` covered the whole remainder, so `ssh://[email protected]/owner/[email protected]/org/repo.git` resolved to `github.com` and borrowed that repository's approvals while git dialed the attacker. Every URL form now splits the authority first, so `@` in a namespace parses instead of being rejected. ([#3813](max-sixty/worktrunk#3813))

- **`WORKTRUNK_*` env vars and `--config-set` outrank a `[projects."…"]` entry**: layer and specificity resolved separately, so a project entry answered for the global key whichever layer set it — `WORKTRUNK_WORKTREE_PATH` could not override a project's `worktree-path`. Both invocation layers now apply at either scope, and the help text gained a precedence section. ([#3790](max-sixty/worktrunk#3790), fixes [#3788](max-sixty/worktrunk#3788), thanks @jeremy0dell for reporting)

- **Shell completions register under the `--cmd` name**: `wt config shell init zsh --cmd wot` renamed the wrapper, but clap emitted the registration under `wt`. Bash and zsh completed nothing and regenerated the script on every TAB, PowerShell registered the wrong command, and zsh's `compdef` handed worktrunk's completer to the other `wt`. ([#3817](max-sixty/worktrunk#3817), fixes [#3816](max-sixty/worktrunk#3816), thanks @sbarre for reporting)

### Documentation

- **The picker's `Alt-x` never forces**: the keybinding table read `Remove selected worktree/branch`, which sent a reader looking for a force-remove that isn't there. ([#3811](max-sixty/worktrunk#3811), raised in [#3809](max-sixty/worktrunk#3809), thanks @josh-burton)

### Internal

- **Library API rework** (Breaking library API): `GitError::WorktreePathNotOurs` gained an `occupant_registered_at` field, and `WorkingTree::ensure_belongs_to_repo` is now `ensure_holds_this_worktree`. ([#3808](max-sixty/worktrunk#3808))

## Install worktrunk 0.74.0

### Install prebuilt binaries via shell script

```sh
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-installer.sh | sh && wt config shell install
```

### Install prebuilt binaries via powershell script

```sh
powershell -ExecutionPolicy Bypass -c "irm https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-installer.ps1 | iex"; git-wt config shell install
```

### Install prebuilt binaries via Homebrew

```sh
brew install worktrunk && wt config shell install
```

## Download worktrunk 0.74.0

|  File  | Platform | Checksum |
|--------|----------|----------|
| [worktrunk-aarch64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-aarch64-apple-darwin.tar.xz) | Apple Silicon macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-aarch64-apple-darwin.tar.xz.sha256) |
| [worktrunk-x86_64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-apple-darwin.tar.xz) | Intel macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-apple-darwin.tar.xz.sha256) |
| [worktrunk-x86_64-pc-windows-msvc.zip](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-pc-windows-msvc.zip) | x64 Windows | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-pc-windows-msvc.zip.sha256) |
| [worktrunk-aarch64-unknown-linux-musl.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-aarch64-unknown-linux-musl.tar.xz) | ARM64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-aarch64-unknown-linux-musl.tar.xz.sha256) |
| [worktrunk-x86_64-unknown-linux-musl.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-unknown-linux-musl.tar.xz) | x64 MUSL Linux | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.74.0/worktrunk-x86_64-unknown-linux-musl.tar.xz.sha256) |



### Install via Cargo

```sh
cargo install worktrunk && wt config shell install
```

### Install via Winget (Windows)

```sh
winget install max-sixty.worktrunk && git-wt config shell install
```

### Install via AUR (Arch Linux)

```sh
paru worktrunk-bin && wt config shell install
```
</pre>
  <p>View the full release notes at <a href="https://github.com/max-sixty/worktrunk/releases/tag/v0.74.0">https://github.com/max-sixty/worktrunk/releases/tag/v0.74.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!16610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

config shell init --cmd <name> generates broken tab completion

2 participants