Skip to content

Hook does not rewrite go -C DIR <subcommand> (global chdir flag before the subcommand) #4970

Description

@aibeshkov

Repro

$ rtk rewrite "go -C ./sub test ./..."
                  # prints nothing: not rewritten
$ rtk rewrite "go test ./..."
rtk go test ./...
$ rtk rewrite "cd ./sub && go test ./..."
cd ./sub && rtk go test ./...

rtk 1.1.0, macOS, Claude Code hook (rtk hook claude).

Why it matters

Agents working across several clones/worktrees use go -C DIR test|vet|build to avoid changing the shell's working directory. In my last 30 days rtk discover lists go among the top unhandled commands (4,155 calls; the example it shows is go -C <clone> ...), so the full go test output of those calls reaches the agent, while the same tests run as cd DIR && go test are compressed.

Expected

go -C DIR test ARGS → rtk go -C DIR test ARGS (and the same for the other subcommands rtk already handles), with rtk keeping -C DIR as the first flag when it injects -json. Go accepts -C DIR, -C=DIR, --C DIR and --C=DIR, only as the first flag (handleChdirFlag in cmd/go/main.go).

Related: #4362 / #3520 fix the same ordering problem for go test -C DIR (chdir after the subcommand). This report is about the form with -C before the subcommand, which the hook does not match at all.

Activity

  1. added
    bug
    good first issue
    on Nov 3, 2026
  2. rtk-wshm-sync-bot commented on Nov 3, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 88%

    Summary

    rtk's command rewrite doesn't match go -C DIR <subcommand> (global chdir flag before subcommand), so full go output bypasses compression — extend the go rewrite matcher alongside the existing -C-after-subcommand fix.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • cmd/go/main.go
    • internal/rewrite/go.go

    Suggested Actions

    1. Extend the go rewrite matcher to recognize -C/--C (and =DIR form) before the subcommand, not just after
    2. Reuse/adapt the fix from rtk go test -C DIR fails: -json is injected before -C #4362 / fix(go): preserve -C as the first test flag #3520 which handled go test -C DIR ordering
    3. Preserve -C DIR as the first flag when injecting -json, per Go's handleChdirFlag requirement
    4. Add test cases for -C DIR, -C=DIR, --C DIR, --C=DIR across the go subcommands rtk already handles

    Triaged automatically by wshm · This is an automated analysis, not a human review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibuggood first issuepriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions