Skip to content

Commit c270e71

Browse files
adonovangopherbot
authored andcommitted
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 #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>
1 parent 7453497 commit c270e71

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/cmd/go/internal/vet/vet.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ func run(ctx context.Context, cmd *base.Command, args []string) {
262262
// will only be executed in VetxOnly mode, for facts but not
263263
// diagnostics.
264264
for _, p := range pkgs {
265+
// Don't apply fixes to vendored packages, including
266+
// the GOROOT vendor packages that are part of std,
267+
// or to packages from non-main modules (#76479).
268+
if applyFixes {
269+
if p.Standard && strings.HasPrefix(p.ImportPath, "vendor/") ||
270+
p.Module != nil && !p.Module.Main {
271+
continue
272+
}
273+
}
265274
_, ptest, pxtest, perr := load.TestPackagesFor(moduleLoaderState, ctx, pkgOpts, p, nil)
266275
if perr != nil {
267276
base.Errorf("%v", perr.Error)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Test that go fix skips fixes to non-main and/or vendored packages.
2+
# (It uses the interface{} -> any modernizer.)
3+
4+
# Create vendor tree programmatically to avoid
5+
# having to hardcode sums in this txtar archive.
6+
go mod vendor
7+
8+
# Show fixes on two packages, one in the main module
9+
# and one in a vendored dependency.
10+
# Only the main one (a) is shown.
11+
go fix -diff example.com/a example.com/b
12+
stdout 'a[/\\]a.go'
13+
stdout '\-var _ interface\{\}'
14+
stdout '\+var _ any'
15+
! stdout 'b[/\\]b.go'
16+
17+
# Apply fixes to the same two packages.
18+
# Only the main module was modified.
19+
go fix example.com/a example.com/b
20+
grep 'var _ any' a/a.go
21+
grep 'var _ interface{}' b/b.go
22+
grep 'var _ interface{}' vendor/example.com/b/b.go
23+
24+
-- go.mod --
25+
module example.com
26+
go 1.26
27+
28+
require "example.com/b" v0.0.0
29+
replace "example.com/b" => ./b
30+
31+
-- a/a.go --
32+
package a
33+
34+
import _ "example.com/b"
35+
36+
var _ interface{}
37+
38+
-- b/go.mod --
39+
module example.com/b
40+
41+
-- b/b.go --
42+
package b
43+
44+
var _ interface{}

0 commit comments

Comments
 (0)