aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorAlan Donovan <adonovan@google.com>2025-12-05 12:41:21 -0500
committerGopher Robot <gobot@golang.org>2025-12-05 14:29:25 -0800
commitc270e7183582600aa54dcc8bb14aeecf61fc4275 (patch)
tree2d6a589f1f7e04ab0dd3d2ba996cd13913f2fff8 /src/cmd
parent745349712e837ef77eb7b5a21c4d4e5c7ca0371a (diff)
downloadgo-c270e7183582600aa54dcc8bb14aeecf61fc4275.tar.xz
cmd/go/internal/vet: skip -fix on pkgs from vendor or non-main mod
This change causes go fix (and go vet -fix) to skip applying fixes to any package in the vendor/ tree, including the GOROOT vendor packages that are part of std, and to any package from a non-main module (since these usually come from the readonly module cache). + test Fixes golang/go#76479 Change-Id: Ifdb73e09fbe413b4d99a92e5081b8ea43460be0b Reviewed-on: https://go-review.googlesource.com/c/go/+/727300 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@google.com> Auto-Submit: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/go/internal/vet/vet.go9
-rw-r--r--src/cmd/go/testdata/script/fix_vendor.txt44
2 files changed, 53 insertions, 0 deletions
diff --git a/src/cmd/go/internal/vet/vet.go b/src/cmd/go/internal/vet/vet.go
index 34d904cffc..e7d01782bf 100644
--- a/src/cmd/go/internal/vet/vet.go
+++ b/src/cmd/go/internal/vet/vet.go
@@ -262,6 +262,15 @@ func run(ctx context.Context, cmd *base.Command, args []string) {
// will only be executed in VetxOnly mode, for facts but not
// diagnostics.
for _, p := range pkgs {
+ // Don't apply fixes to vendored packages, including
+ // the GOROOT vendor packages that are part of std,
+ // or to packages from non-main modules (#76479).
+ if applyFixes {
+ if p.Standard && strings.HasPrefix(p.ImportPath, "vendor/") ||
+ p.Module != nil && !p.Module.Main {
+ continue
+ }
+ }
_, ptest, pxtest, perr := load.TestPackagesFor(moduleLoaderState, ctx, pkgOpts, p, nil)
if perr != nil {
base.Errorf("%v", perr.Error)
diff --git a/src/cmd/go/testdata/script/fix_vendor.txt b/src/cmd/go/testdata/script/fix_vendor.txt
new file mode 100644
index 0000000000..f03f326922
--- /dev/null
+++ b/src/cmd/go/testdata/script/fix_vendor.txt
@@ -0,0 +1,44 @@
+# Test that go fix skips fixes to non-main and/or vendored packages.
+# (It uses the interface{} -> any modernizer.)
+
+# Create vendor tree programmatically to avoid
+# having to hardcode sums in this txtar archive.
+go mod vendor
+
+# Show fixes on two packages, one in the main module
+# and one in a vendored dependency.
+# Only the main one (a) is shown.
+go fix -diff example.com/a example.com/b
+stdout 'a[/\\]a.go'
+stdout '\-var _ interface\{\}'
+stdout '\+var _ any'
+! stdout 'b[/\\]b.go'
+
+# Apply fixes to the same two packages.
+# Only the main module was modified.
+go fix example.com/a example.com/b
+grep 'var _ any' a/a.go
+grep 'var _ interface{}' b/b.go
+grep 'var _ interface{}' vendor/example.com/b/b.go
+
+-- go.mod --
+module example.com
+go 1.26
+
+require "example.com/b" v0.0.0
+replace "example.com/b" => ./b
+
+-- a/a.go --
+package a
+
+import _ "example.com/b"
+
+var _ interface{}
+
+-- b/go.mod --
+module example.com/b
+
+-- b/b.go --
+package b
+
+var _ interface{}