[object Object]

← back to Cli Printing Press

fix(cli): scope goimports to patched files only (#223)

8c54c4c8223288a49f042651f6c0638d58162501 · 2026-04-18 02:40:18 -0700 · Trevin Chow

The patcher was running `goimports -w` over the entire internal/cli
directory, which pulled every existing file into the diff to reshuffle
its cobra import between import groups — purely cosmetic but drowned
the real patch in 5 unrelated 1-line changes per CLI (100+ noise lines
across a 21-CLI rollout).

Scope goimports to only the files we actually wrote (root.go + any new
drop-ins). Verified against weather-goat: patcher now produces exactly
4 diffs (3 new drop-ins + root.go), zero noise.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 8c54c4c8223288a49f042651f6c0638d58162501
Author: Trevin Chow <trevin@trevinchow.com>
Date:   Sat Apr 18 02:40:18 2026 -0700

    fix(cli): scope goimports to patched files only (#223)
    
    The patcher was running `goimports -w` over the entire internal/cli
    directory, which pulled every existing file into the diff to reshuffle
    its cobra import between import groups — purely cosmetic but drowned
    the real patch in 5 unrelated 1-line changes per CLI (100+ noise lines
    across a 21-CLI rollout).
    
    Scope goimports to only the files we actually wrote (root.go + any new
    drop-ins). Verified against weather-goat: patcher now produces exactly
    4 diffs (3 new drop-ins + root.go), zero noise.
    
    Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 internal/patch/patch.go | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/internal/patch/patch.go b/internal/patch/patch.go
index 4b6db2ca..a02c9563 100644
--- a/internal/patch/patch.go
+++ b/internal/patch/patch.go
@@ -137,7 +137,14 @@ func Patch(opts Options) (*Report, error) {
 		report.FilesCreated = append(report.FilesCreated, d.path)
 	}
 
-	if err := goimportsDir(filepath.Join(opts.Dir, "internal", "cli")); err != nil {
+	// Run goimports only on the files we created or modified. Running it
+	// over the whole internal/cli directory pulls existing files into a
+	// noisy diff where every file's `cobra` import gets shuffled between
+	// import groups — purely cosmetic but drowns the real patch in 5-line
+	// unrelated changes per CLI.
+	touched := append([]string(nil), report.FilesModified...)
+	touched = append(touched, report.FilesCreated...)
+	if err := goimportsFiles(touched); err != nil {
 		return nil, fmt.Errorf("goimports: %w", err)
 	}
 
@@ -179,16 +186,21 @@ func filterFatalCollisions(all []Collision) []Collision {
 	return fatal
 }
 
-// goimportsDir runs `goimports -w` over the given directory. Missing binary
-// falls back to `gofmt -w` since goimports is a superset.
-func goimportsDir(dir string) error {
-	cmd := exec.Command("goimports", "-w", dir)
-	if err := cmd.Run(); err == nil {
+// goimportsFiles runs `goimports -w` over the given file paths. Scoping to
+// specific files (vs. a whole directory) keeps the patcher's diff tight —
+// see the comment at the call site. Missing binary falls back to `gofmt -w`
+// since goimports is a superset.
+func goimportsFiles(paths []string) error {
+	if len(paths) == 0 {
+		return nil
+	}
+	args := append([]string{"-w"}, paths...)
+	if err := exec.Command("goimports", args...).Run(); err == nil {
 		return nil
 	}
 	// Fallback: gofmt only sorts imports within a group, won't add/remove
 	// groups, but it's better than nothing and is always available.
-	out, err := exec.Command("gofmt", "-w", dir).CombinedOutput()
+	out, err := exec.Command("gofmt", args...).CombinedOutput()
 	if err != nil {
 		return fmt.Errorf("gofmt: %v: %s", err, bytes.TrimSpace(out))
 	}

← 331809da feat(cli): patch skips AST mutations owned by colliding feat  ·  back to Cli Printing Press  ·  fix(cli): patch verifies target shape + runs build in target 86bdfd2e →