Skip to content

fix(hooks): rewrite go -C DIR <subcommand> - #4971

Open
LetMeSleep8h wants to merge 2 commits into
rtk-ai:developfrom
LetMeSleep8h:fix/go-chdir-flag-before-subcommand
Open

LetMeSleep8h wants to merge 2 commits into
rtk-ai:developfrom
LetMeSleep8h:fix/go-chdir-flag-before-subcommand

Conversation

@LetMeSleep8h

Copy link
Copy Markdown

Closes #4970

What

The go rule's pattern only matched subcommand-first text, so go -C ./sub test ./... was never rewritten while cd ./sub && go test was — the exact inversion an agent working across clones hits (the issue's rtk discover shows 4,155 unhandled go calls in 30 days, most in the -C form).

  • Rule pattern now accepts the chdir flag between go and test|build|vet in all four spellings go itself accepts (-C DIR, -C=DIR, --C DIR, --C=DIR), as the first flag only — one optional group, mirroring handleChdirFlag in cmd/go/main.go.
  • strip_go_chdir_opt normalizes the same flag in classify_command, so rtk discover accounting stays aligned with what the hook can actually rewrite (the same classify/rewrite-alignment discipline as the pnpm and golangci strippers, per fix(pnpm): rewrite pnpm commands with global flags before the subcommand #3275).
  • The rewrite re-emits the original command: go -C ./sub test ./... → rtk go -C ./sub test ./.... With the flag first, clap routes it through the Other passthrough, which runs the command as typed — verified end-to-end below.

Testing

Two new unit tests: the four chdir spellings all rewrite (plus go test -C DIR still working, bare go -C DIR and go -C DIR env staying put), and the helper's alignment cases. End-to-end on a real two-module tree:

$ rtk rewrite "go -C ./sub test ./..."
rtk go -C ./sub test ./...

$ rtk go -C ./sub test ./...      # inside /tmp/gosub
ok  	gosub/sub	0.859s             # rc=0, same as native `go test` in the subdir
cargo test --lib     # 4520 passed
cargo fmt --check    # clean
cargo clippy --tests --lib   # clean

The go rule only matched subcommand-first text, so  —
the form agents use to work across clones without moving the shell's cwd —
reached the agent uncompressed while  was filtered. The
rule pattern now accepts the chdir flag between  and the subcommand in all
four spellings go accepts (-C DIR, -C=DIR, --C DIR, --C=DIR), first flag only,
and a strip_go_chdir_opt normalization aligns classify with the rewrite so
discover accounting does not report a saving the hook cannot deliver. The
rewrite re-emits the original command, so the flag reaches rtk go untouched.

Closes rtk-ai#4970
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working go hooks labels Nov 3, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes the rtk go rule so go -C DIR test|build|vet (and its -C=, --C, --C= spellings) is recognized and rewritten, mirroring the existing subcommand-first handling. Adds a strip_go_chdir_opt normalizer so classify_command's discover accounting stays aligned with what the rewrite rule actually matches, plus unit tests covering all flag spellings and alignment cases.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4970


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

@Kernel-Hunter Kernel-Hunter left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran this branch on macOS with a stub go on PATH that logs its argv and prints go test -json events when it gets -json, plain go test text otherwise.

The rewrite works: all four spellings now give rtk go -C ./sub test ./... where develop gives nothing, and both go_chdir_* tests pass.

What runs after the rewrite isn't filtered, though. rtk go -C ./sub test ./... lands in GoCommands::Other (run_other, src/cmds/go/go_cmd.rs:144), so go gets -C ./sub test ./... with no -json and the output comes back raw:

$ rtk go test ./...
Go test: 1 passed in 1 packages        # go got: test -json ./...
$ rtk go -C ./sub test ./...           # go got: -C ./sub test ./...
=== RUN   TestA
--- PASS: TestA (0.00s)
PASS
ok  	ex/sub	0.100s

So these calls still reach the agent uncompressed, which is what #4970 is about, while classify_command now reports them as supported (src/discover/registry.rs:224) and rtk discover stops listing them as missed. The issue asks for -C DIR kept first with -json injected, i.e. go -C DIR test -json ARGS. I think that needs the go dispatch to recognise a leading chdir flag followed by test/build/vet and call run_test/run_build/run_vet with the flag placed before the subcommand. Rewriting to rtk go test -C DIR would run into the ordering problem #3520 is still fixing.

Smaller: the comment at src/discover/rules.rs:564 says "(also -C=DIR, -C=DIR single-arg)", and the pattern itself doesn't match --C, so the --C cases in the test presumably pass through strip_go_chdir_opt rather than the regex.

@KuSh KuSh added the needs-cla label Nov 8, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

Hey @LetMeSleep8h, thanks for handling go -C DIR <subcommand> in the rewrite. What's blocking merge is the CLA signature: the CLAassistant bot left the link in the PR. Once that's done, we can take it from there.

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

Labels

bug Something isn't working go hooks needs-cla

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants