Skip to content

Conversation

lox
Copy link

@lox lox commented Oct 8, 2025

Summary

Fixes tab completion for wtp cd <tab> by resolving two issues:

  1. Shell hook function was intercepting completion system calls
  2. Asterisk markers in completion output caused shell glob issues

Problem

When using wtp completion zsh with wtp hook zsh, tab completion failed because:

  • The wtp() shell function intercepted --generate-shell-completion flag
  • Instead of passing through to the binary, the hook tried to execute cd logic
  • The completion output included * markers (e.g., @*) which caused shell expansion errors

Solution

Hook functions (bash, zsh, fish)

  • Added early detection of --generate-shell-completion flag
  • Pass through directly to command wtp when detected
  • Prevents hook logic from interfering with completion system

Completion output (cd.go)

  • Strip trailing * marker from worktree names
  • Marker is useful for list command but breaks completion
  • Clean output allows proper shell completion matching

Test Plan

Manual testing completed:

  • Tab completion works: wtp cd <tab> shows all worktrees
  • cd functionality still works: wtp cd <worktree> navigates correctly
  • Works across bash, zsh, and fish shells
  • Unit tests pass for cmd and internal packages

Files Changed

  • cmd/wtp/cd.go: Strip asterisk from completion output
  • cmd/wtp/hook.go: Pass through completion requests in all hooks

Summary by CodeRabbit

  • Bug Fixes
    • Shell hooks for bash, zsh, and fish now correctly forward the --generate-shell-completion flag to the main command, ensuring reliable generation of shell completions across supported shells.
    • Worktree suggestions for the cd command no longer include a trailing asterisk on the current worktree, providing cleaner, more readable completion entries and preventing issues with parsing or selection in interactive completion.

The shell hook function was intercepting --generate-shell-completion
calls, preventing the completion system from working correctly. When
zsh's completion system invoked the command, the hook would try to
execute cd logic instead of passing through to the binary.

Additionally, the asterisk marker (*) used to indicate the current
worktree was being included in completion output, causing shell glob
expansion issues.

Changes:
- Hook functions now detect and pass through completion requests
- Completion output strips asterisk markers before display
- Fixes apply to bash, zsh, and fish hooks

Fixes tab completion for 'wtp cd <tab>' command.
Copy link

coderabbitai bot commented Oct 8, 2025

Walkthrough

Updates the cd worktrees output to strip trailing asterisks before printing, and augments generated shell hook scripts (bash/zsh/fish) to detect and forward the --generate-shell-completion flag directly to the main wtp command.

Changes

Cohort / File(s) Summary of Changes
Worktree CD output formatting
cmd/wtp/cd.go
Trims trailing asterisk from buffered worktree lines before printing, replacing direct line output; no API or control-flow changes.
Shell hooks: completion pass-through
cmd/wtp/hook.go
Embedded bash/zsh/fish hook scripts now detect --generate-shell-completion in args and delegate to wtp with that flag; no Go API changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as User Shell
  participant H as Hook Script (bash/zsh/fish)
  participant W as wtp Binary

  rect rgb(245,248,252)
  Note over U,H: Startup / user command
  U->>H: source hook + run shell function
  alt Arg includes --generate-shell-completion (new)
    H->>W: wtp --generate-shell-completion
    W-->>H: completion script output
    H-->>U: prints completion data
  else Normal invocation
    H->>W: wtp [other args]
    W-->>H: command output
    H-->>U: forwards output
  end
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

In burrows of code where switches flip,
I nibble stars from worktree script.
Hooks now hear completion’s plea,
And pass it on, swift as a bunny can be.
Tap-tap keys, outputs neat—
Carrots compiled, releases sweet. 🥕✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title concisely and accurately summarizes the main change by describing the fix for shell hook interference with tab completion, aligns with the PR’s objectives, and uses clear, conventional commit phrasing without unnecessary details.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/wtp/cd.go (1)

365-371: Consider checking scanner error for robustness.

The scanner error is not checked after the loop completes. While scanner errors are rare for in-memory buffers and the function signature does not allow error propagation, checking scanner.Err() would make the implementation more complete.

Add after the loop:

 	for scanner.Scan() {
 		line := scanner.Text()
 		// Remove trailing asterisk that marks current worktree
 		line = strings.TrimSuffix(line, "*")
 		fmt.Println(line)
 	}
+	// Note: scanner errors cannot be propagated due to completion handler signature
+	if err := scanner.Err(); err != nil {
+		// Could log here if logging is available in completion context
+	}
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7025099 and 913953f.

📒 Files selected for processing (2)
  • cmd/wtp/cd.go (1 hunks)
  • cmd/wtp/hook.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Run gofmt and goimports; keep imports grouped and organized; local import prefix follows module path github.com/satococoa/wtp
Adhere to golangci-lint rules configured for the project (vet, staticcheck, gosec, mnd, lll=120, etc.)
Errors should be wrapped with context and must not be ignored
Use snake_case for Go filenames; document exported identifiers when non-trivial

Files:

  • cmd/wtp/hook.go
  • cmd/wtp/cd.go
cmd/wtp/**

📄 CodeRabbit inference engine (AGENTS.md)

cmd/wtp/**: CLI entrypoint and commands are implemented under cmd/wtp
Update CLI help text to reflect user-facing changes
Command behavior: wtp cd prints only the absolute worktree path with no side effects
Command behavior: wtp completion generates pure completion scripts via urfave/cli
Command behavior: wtp hook emits shell functions that intercept wtp cd; wtp shell-init combines completion and hook output

Files:

  • cmd/wtp/hook.go
  • cmd/wtp/cd.go
🧠 Learnings (5)
📓 Common learnings
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp completion <shell> generates pure completion scripts via urfave/cli
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp hook <shell> emits shell functions that intercept wtp cd; wtp shell-init <shell> combines completion and hook output
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp cd prints only the absolute worktree path with no side effects
📚 Learning: 2025-10-07T15:56:11.502Z
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp hook <shell> emits shell functions that intercept wtp cd; wtp shell-init <shell> combines completion and hook output

Applied to files:

  • cmd/wtp/hook.go
📚 Learning: 2025-10-07T15:56:11.502Z
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp completion <shell> generates pure completion scripts via urfave/cli

Applied to files:

  • cmd/wtp/hook.go
📚 Learning: 2025-10-07T15:56:11.502Z
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/completion.go : Use getWorktreeNameFromPath() to resolve worktree display names consistently across completion, errors, and parsing

Applied to files:

  • cmd/wtp/cd.go
📚 Learning: 2025-10-07T15:56:11.502Z
Learnt from: CR
PR: satococoa/wtp#0
File: AGENTS.md:0-0
Timestamp: 2025-10-07T15:56:11.502Z
Learning: Applies to cmd/wtp/** : Command behavior: wtp cd prints only the absolute worktree path with no side effects

Applied to files:

  • cmd/wtp/cd.go
🔇 Additional comments (4)
cmd/wtp/cd.go (1)

365-371: Asterisk stripping correctly prevents shell glob expansion.

The implementation correctly strips trailing asterisks from worktree names before printing them for completion. This prevents the shell from interpreting patterns like @* as glob expressions, which was causing the completion failure described in the PR.

cmd/wtp/hook.go (3)

76-82: LGTM: Completion passthrough correctly prevents hook interference.

The early detection of --generate-shell-completion ensures that completion requests bypass the hook's cd logic and reach the binary directly. This fixes the completion issue where the hook was attempting to execute cd logic instead of delegating to the completion system.


105-111: LGTM: Consistent completion handling for zsh.

The zsh hook implementation mirrors the bash approach with appropriate shell-specific syntax. The completion passthrough logic is correctly implemented.


134-140: LGTM: Fish shell syntax correctly adapted.

The fish hook implementation correctly adapts the completion passthrough logic using fish-specific syntax ($argv, test, $status, end). The behavior is consistent with the bash and zsh implementations.

@satococoa
Copy link
Owner

@lox

Thank you for your contribution!

I’ve just merged PR #27, which resolves the regression this PR highlighted.
I had already noticed a couple of other completion glitches in the same area, so I folded those fixes into #27 as well; together they should leave shell completion behaving properly again.

With that merged I’ll go ahead and close this PR.

Really appreciate you taking the time to open it!

@satococoa satococoa closed this Oct 16, 2025
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