Commit e81d1ed3da for golang
commit e81d1ed3da60a813c4833624e8a9859c65dd4a21
Author: Alan Donovan <adonovan@google.com>
Date: Thu Dec 3 21:29:56 2026 -0500
cmd/go: test: support go.vet disable by reference
Before, go test's go.vet mechanism supporited only
disable-by-name (e.g. disable . "printf").
This CL adds support for disable-by-reference
(e.g. disable . pkg.Var or pkg.Var[A,B]).
It works in three phases:
- static (load.AnalyzersFor): a "." or "./..." negation of a whole
variable (pkg.Var or pkg.Var[*]) is applied textually, removing
the positive references to that variable, and is marked as having
matched.
- linking (load.AnalysisToolPackage): a negated variable is linked
into the tool only if its package is already in the import closure
of the packages of the positive references, since otherwise there
is nothing for it to match. The package of a negation that is not
linked must still resolve, unless the negation matched textually.
- dynamic (work.resolveAnalyzers): a negation disables, by index,
each analyzer it selects, however that analyzer was enabled.
For #82464
Change-Id: I4fd6a2bbce0fe7f955b16d77b0b67ee83d104988
Reviewed-on: https://go-review.googlesource.com/c/go/+/869961
Reviewed-by: PJ Malloy <pjmalloy@google.com>
Reviewed-by: t hepudds <thepudds1460@gmail.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
diff --git a/src/cmd/go/internal/load/analysis.go b/src/cmd/go/internal/load/analysis.go
index 8f0e9e2587..e89929b851 100644
--- a/src/cmd/go/internal/load/analysis.go
+++ b/src/cmd/go/internal/load/analysis.go
@@ -13,10 +13,11 @@ package load
//
// Resolution of the configuration has two stages. The static stage,
// here, determines which variables the tool links, from references
-// to them, before the tool exists. The dynamic stage (see
+// to them, before the tool exists, and applies negations of whole
+// variables textually. The dynamic stage (see
// work.Builder.planAnalysis) uses the tool's metadata to expand
-// suites into their members, apply negations, and select the
-// analyzers that apply to each package.
+// suites into their members, apply the remaining negations, and
+// select the analyzers that apply to each package.
//
// The configuration of go test's analysis comes from its built-in
// configuration (which the -vet flag can override) and from the
@@ -54,8 +55,9 @@ const analysisVetPackage = "internal/analysis/suite/vet"
// built-in configuration, as with an explicit -vet flag) a synthesized
// declaration, whose Scope and Pos are zero.
type VetRef struct {
- Decl *vetfile.Decl // syntactic declaration in go.vet file (or 'go test -vet' flag)
- Std bool // declaration is in the standard library
+ Decl *vetfile.Decl // syntactic declaration in go.vet file (or 'go test -vet' flag)
+ Std bool // declaration is in the standard library
+ matched bool // whether this ref negated a positive ref for some root package
// Problems records problems with the reference encountered
// before analysis (e.g. unresolvable package, nonexistent
@@ -85,6 +87,13 @@ func (ref *VetRef) AllMembers() bool {
return len(ref.Decl.Members) == 1 && ref.Decl.Members[0] == "*"
}
+// wholeVar reports whether the reference denotes the whole variable:
+// an analyzer, or all members of a suite. A negation of a whole
+// variable is applied textually (statically).
+func (ref *VetRef) wholeVar() bool {
+ return !ref.ByName() && (!ref.IsSuite() || ref.AllMembers())
+}
+
// HasPos reports whether the reference was declared in a file.
func (ref *VetRef) HasPos() bool { return ref.Decl.File != nil }
@@ -98,6 +107,11 @@ func (ref *VetRef) Position() string {
return ref.Decl.Position()
}
+// Matched reports whether a negation textually matched (and removed)
+// a positive reference for some root package. A negation that
+// does not resolve is accepted if it matched.
+func (ref *VetRef) Matched() bool { return ref.matched }
+
// VetSuiteRef returns the reference denoted by an explicit -vet
// flag of go test: the members of the vet suite with the specified
// names, or all of them if names is nil. (Without the flag, go test's
@@ -142,25 +156,31 @@ var vetLegacyNames = map[string]string{
// AnalysisToolPackage returns a synthesized main package for an
// analysis tool and the source of its sole file. The tool contains
-// the variables referenced by the specified positive references (those
-// that enable analyzers for some root package), subject to the checks
-// below.
+// the variables referenced by the specified references, positive
+// (those that enable analyzers for some root package, after textual
+// negation) and negative, subject to the checks below.
//
// The package of each positive reference must load. A reference
// whose package does not load is reported at each declaration that
// uses it, and dropped. (Whether the package declares the variable
// is discovered only if the tool fails to compile.)
//
-// A negation (for now, always a disable by name; see readVetFile) is
-// not linked, and needs no checks.
+// A negated variable is linked only if its package is already in the
+// import closure of the packages of the positive references, since
+// otherwise there is nothing for the negation to match. The package
+// of a negation that is not linked must still resolve, unless the
+// negation textually matched a positive reference.
+//
+// A disable by name is not linked, and needs no checks.
//
// It records the problems of the offending references (see
// VetRef.Problems).
//
// The package's imports are loaded without regard to the rule for
// internal packages, like the generated testmain: the tool must
-// import analyzers from internal packages, and the driver itself is
-// an internal package of the standard library.
+// import analyzers (and negated variables) from internal packages,
+// and the driver itself is an internal package of the standard
+// library.
func AnalysisToolPackage(ld *modload.Loader, ctx context.Context, opts PackageOpts, refs []*VetRef) (pmain *Package, src []byte) {
pmain = &Package{
// Avoid de-nesting this literal until bootstrap has go1.27.
@@ -193,10 +213,12 @@ func AnalysisToolPackage(ld *modload.Loader, ctx context.Context, opts PackageOp
}
}
}
- var positive []*VetRef
+ var positive, negative []*VetRef
for _, ref := range refs {
if ref.Decl.Enable {
positive = append(positive, ref)
+ } else {
+ negative = append(negative, ref)
}
}
load(append([]string{AnalysisToolDriver}, refPkgPaths(positive)...))
@@ -225,15 +247,53 @@ func AnalysisToolPackage(ld *modload.Loader, ctx context.Context, opts PackageOp
return false
}
+ // importClosure is the import closure of the packages of the
+ // linked positive references.
+ importClosure := make(map[string]bool)
+ var visit func(p *Package)
+ visit = func(p *Package) {
+ if !importClosure[p.ImportPath] {
+ importClosure[p.ImportPath] = true
+ for _, imp := range p.Internal.Imports {
+ visit(imp)
+ }
+ }
+ }
+
+ // Pass 1: enable
+ //
// Check the positive references, and link the variables of the
// valid ones. (checkRef depends only on the package, so all
// references to one variable succeed or fail together.)
linked := make(map[vetfile.Var]bool) // set of variables to link
for _, ref := range positive {
- if checkRef(ref) {
+ if !checkRef(ref) {
+ continue
+ }
+ linked[ref.Decl.Var] = true
+ visit(pkgs[ref.Decl.Var.PkgPath])
+ }
+
+ // Pass 2: disable
+ //
+ // Link the negative references in the (now complete) import closure.
+ // The package of each other negation must resolve,
+ // unless the negation textually matched a positive reference.
+ var unlinked []*VetRef
+ for _, ref := range negative {
+ switch {
+ case ref.ByName():
+ // applied by name in the dynamic stage
+ case importClosure[ref.Decl.Var.PkgPath]:
linked[ref.Decl.Var] = true
+ case !ref.Matched():
+ unlinked = append(unlinked, ref)
}
}
+ load(refPkgPaths(unlinked))
+ for _, ref := range unlinked {
+ checkRef(ref)
+ }
// The main package imports the driver and the packages of the
// linked variables, plus the packages needed by the linker.
diff --git a/src/cmd/go/internal/load/vetfile.go b/src/cmd/go/internal/load/vetfile.go
index 7d77897f5b..5808389b78 100644
--- a/src/cmd/go/internal/load/vetfile.go
+++ b/src/cmd/go/internal/load/vetfile.go
@@ -14,7 +14,7 @@ package load
//
// This file implements the static stage of resolution for the
// declarations: parsing them, and computing which apply to each
-// root package.
+// root package (with textual negation).
//
// See go.dev/issue/82464.
@@ -39,8 +39,8 @@ import (
// unreadable, or invalid file yields no references.
//
// The results are cached, so each declaration yields a single
-// VetRef, whose identity matters: references are keys of sets.
-// Likewise, the problems
+// VetRef, whose identity matters: references are keys of sets,
+// and record whether a negation matched. Likewise, the problems
// with the file (a failure to read it, other than its absence, or
// errors in it) are reported once, when it is first read,
// and cause go test to fail. A file with any error is ignored
@@ -69,20 +69,6 @@ func readVetFile(dir, dirPath string, std bool) []*VetRef {
}
return nil
}
- // For now, a disable declaration must name analyzers
- // by Analyzer.Name, not by reference.
- //
- // TODO(adonovan): support disable by reference.
- ok := true
- for _, decl := range f.Decls {
- if !decl.Enable && decl.Name == "" {
- base.Errorf("go: %s: %s: disable by reference is not yet supported; disable by analyzer name", decl.Position(), decl)
- ok = false
- }
- }
- if !ok {
- return nil
- }
var refs []*VetRef
for _, decl := range f.Decls {
refs = append(refs, &VetRef{Decl: decl, Std: std})
@@ -162,7 +148,8 @@ func importersRefs(p *Package) []*VetRef {
})
}
-// AnalyzersFor returns the references that apply to root package p.
+// AnalyzersFor returns the references that apply to root package p,
+// after textual negation.
//
// A root package is a test variant "P [P.test]", an external test
// package "P_test", or a package without tests.
@@ -175,8 +162,10 @@ func importersRefs(p *Package) []*VetRef {
// specificity. Order is immaterial for negations since a negation
// prevails over every positive reference, however specific.
//
-// Negations (for now, always by name; see [readVetFile]) are applied
-// once the analyzers and their names are known.
+// Negations of whole variables have already been applied textually,
+// removing the positive references they match. All negations must be
+// applied again, after positive references by Analyzer index have
+// been resolved and the analyzers names and aliasing are known.
//
// The "." and "./..." declarations of p's directory and its ancestors
// (up to the root of its module) apply if p belongs to a module (main
@@ -190,12 +179,17 @@ func importersRefs(p *Package) []*VetRef {
// configuration of the vet checks (see [vetFlagOverrides]), and
// applies to every root package, least specifically.
//
+// All calls must precede [AnalysisToolPackage], which consults the
+// negations they matched (see [VetRef.Matched]).
+//
// Not safe for concurrent use.
func AnalyzersFor(p *Package, vetFlag *VetRef) []*VetRef {
- // refs, in order of specificity
var (
+ // refs, in order of specificity
ancestors []*VetRef // "." and "./..."
importers []*VetRef // "importers" and "global"
+
+ wholeVarNegations = make(map[vetfile.Var][]*VetRef) // whole-var negations (among ancestors), by var
)
// Scan ancestor directories for "." and "./..." decls.
@@ -216,6 +210,9 @@ func AnalyzersFor(p *Package, vetFlag *VetRef) []*VetRef {
// (Only p's own directory has p's import path.)
if ref.Decl.Scope == "./..." || ref.Decl.Scope == "." && dirPath == pkgPath {
ancestors = append(ancestors, ref)
+ if !ref.Decl.Enable && ref.wholeVar() {
+ wholeVarNegations[ref.Decl.Var] = append(wholeVarNegations[ref.Decl.Var], ref)
+ }
}
}
}
@@ -241,7 +238,20 @@ func AnalyzersFor(p *Package, vetFlag *VetRef) []*VetRef {
}
}
- result := slices.Concat(ancestors, importers)
+ // Apply negations of whole variables textually.
+ var result []*VetRef
+ for _, ref := range slices.Concat(ancestors, importers) {
+ if ref.Decl.Enable {
+ if negs := wholeVarNegations[ref.Decl.Var]; negs != nil {
+ // All these negative refs matched this positive ref.
+ for _, neg := range negs {
+ neg.matched = true
+ }
+ continue // discard this positive ref as its var was wholly negated
+ }
+ }
+ result = append(result, ref)
+ }
// The -vet=... flag disables the standard library's
// configuration of the vet checks and enables only the
diff --git a/src/cmd/go/internal/work/analysis.go b/src/cmd/go/internal/work/analysis.go
index f8d72d45a5..475f7e4b4d 100644
--- a/src/cmd/go/internal/work/analysis.go
+++ b/src/cmd/go/internal/work/analysis.go
@@ -642,8 +642,8 @@ func (b *Builder) planAnalysis(md *protocol.Metadata, driverID cache.ActionID, i
// resolveAnalyzers performs the dynamic stage of resolution for one
// root package, whose references (see load.AnalyzersFor) are refs: it
// enables the members of each positive reference, as computed by
-// [resolveMembers], and applies negations (for now, always disables
-// by name) by Analyzer.Name. It returns the analyzers whose
+// [resolveMembers], and applies negations by pointer (index), or, for a
+// disable by name, by Analyzer.Name. It returns the analyzers whose
// diagnostics are reported, by index, each with the most specific
// reference that enabled it.
func resolveAnalyzers(md *protocol.Metadata, refs []*load.VetRef, members map[*load.VetRef][]*protocol.Member) map[int]*load.VetRef {
@@ -661,18 +661,25 @@ func resolveAnalyzers(md *protocol.Metadata, refs []*load.VetRef, members map[*l
}
}
- // Apply negations: a disable by name disables every enabled
- // analyzer of that name.
+ // Apply negations by pointer: a negation of any reference to
+ // an analyzer disables it, however it was enabled. A disable by
+ // name disables every enabled analyzer of that name.
// (This must be a separate pass after all the positive
// references, since a negation prevails over all of them.)
for _, ref := range refs {
if ref.Decl.Enable {
continue
}
- for h := range enabled {
- if md.Analyzers[h].Name == ref.Decl.Name {
- delete(enabled, h)
+ if ref.ByName() {
+ for h := range enabled {
+ if md.Analyzers[h].Name == ref.Decl.Name {
+ delete(enabled, h)
+ }
}
+ continue
+ }
+ for _, m := range members[ref] {
+ delete(enabled, m.Analyzer)
}
}
return enabled
diff --git a/src/cmd/go/testdata/script/test_vetfiles_std.txt b/src/cmd/go/testdata/script/test_vetfiles_std.txt
index f1d9d60c7e..d8ee16fbf2 100644
--- a/src/cmd/go/testdata/script/test_vetfiles_std.txt
+++ b/src/cmd/go/testdata/script/test_vetfiles_std.txt
@@ -55,19 +55,10 @@ stderr 'e.go:1:1: zzcheck$'
cd $WORK/gopath/src
env GO111MODULE=
-# For now, a disable declaration must be by name, not by reference;
-# a file containing one is reported and ignored.
-echo {"Replace":{"$GOROOT/src/fmt/go.vet":"$WORK/gopath/src/fmt_disable_go.vet"}}
-cp stdout overlay_disable.json
-! go test -overlay=overlay_disable.json ./d
-stderr 'fmt[/\\]go.vet:1:1: disable . internal/analysis/suite/vet.Suite\[printf\]: disable by reference is not yet supported; disable by analyzer name'
-
-- go.mod --
module example.com/m
go 1.26
--- fmt_disable_go.vet --
-disable . internal/analysis/suite/vet.Suite[printf]
-- fmt_go.vet --
enable . internal/zzcheck.Analyzer
enable importers internal/zzcheck.Analyzer