proposal: go test should run analyzers specified by the workspace [freeze exception] · Issue #82464 · golang/go · GitHub
Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 New issue Copy link New issue Copy link Open 1 / 1 1 of 1 issue completed Open proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 1 / 1 1 of 1 issue completed Copy link Assignees Labels Analysis Issues related to static analysis (vet, x/tools/go/analysis) Issues related to static analysis (vet, x/tools/go/analysis) Proposal Milestone Go1.28 Description adonovan opened on Oct 23, 2026 Issue body actions Links to salient comments: See proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 (comment) for current "Mk.5" design, plus addenda at proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 (comment) . --adonovan, Nov 4 Full design documented here: proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 (comment) . for freeze exception rationale, see proposal: go test should run analyzers specified by the workspace [freeze exception] #82464 (comment) .
Background: go test today runs a fixed set of analyzers, a subset of the suite used by go vet. This set is hard-wired into the tool, and therefore these analyzers must be public and general. Yet there are a great many analyzers that users could and would write, either to enforce internal invariants of their own codebase ("housekeeping") or to help clients of their public packages use the APIs correctly, analogous to how the printf analyzer checks calls to fmt.Printf.
This would allow both human users and coding agents to discover problems faster in their edit/test "inner loop", without waiting for later involvement of the CI systems that usually enforce such rules. Proposal: We propose to change go test so that it runs not only the current subset of cmd/vet analyzers, but analyzers designated by annotations in the workspace itself. These analyzers should be fully specified by the source code of each module, so that test behaviors are deterministic, versioned, and predictable.
Because Go users already expect that go test executes code in the workspace, there is no security risk in additionally running static analysis logic provided by the workspace. (By contrast, making go vet or gopls execute tests specified in this way would be insecure, since these are assumed to be static analysis tools and are thus not permitted to execute the code being studied.) I will update this issue in due course with a full proposal, including the specification of the annotations , the proposed user documentation, a design, and a prototype implementation.
In the meantime this issue will serve as a placeholder for some preparatory work and experimentation. All new interfaces and behavior changes will be subject to the GODEBUG=vetfiles flag, until this proposal is decided one way or another. Reacted by thepudds, Sean Liao, Kemal Akkoyun, Jamie Visker, Dominik Honnef, Caleb Spare, Quinn Klassen, Andrew Yuan, Simon Law, Redowan Delowar and 15 more Reacted by thepudds, jaloren, Caleb Spare, Alex Putman, Redowan Delowar, Hendrikto, Adam Shannon, Mike Schinkel, Sasha Casada, GeneriK and 2 more Activity Next adonovan added this to Proposals on Oct 23, 2026 gopherbot added Proposal on Oct 23, 2026 gopherbot added this to the Proposal milestone on Oct 23, 2026 adonovan added Analysis Issues related to static analysis (vet, x/tools/go/analysis) Issues related to static analysis (vet, x/tools/go/analysis) on Oct 23, 2026 adonovan moved this to Hold in Proposals on Oct 23, 2026 adonovan added a sub-issue on Oct 23, 2026 go/analysis: publish x/tools/go/analysis types in the standard library #82465 gopherbot commented on Oct 23, 2026 gopherbot on Oct 23, 2026 Contributor More actions Change https://go.dev/cl/853445 mentions this issue: go/analysis: add core analysis types under GOEXPERIMENT=goanalysis adonovan commented on Oct 26, 2026 adonovan on Oct 26, 2026 Member Author More actions (This revision is Mk.III.) This note briefly describes the entire proposed change to the UX.
Motivation, rationale, copious implementation details, evaluation, and open questions will be covered by the design doc (forthcoming). We propose to change the behavior of go test , so that, in addition to running a hard-wired subset of the analyzer suite of cmd/vet , it runs arbitrary analyzers as defined by annotations in the source code. Consider a database named example.com/db that defines two static checkers: one for various internal project housekeeping tasks and another that validates calls to its SQL query API from client code.
The database maintainer adds the goanalysis.go file below. Each time the maintainer subsequently runs go test , it runs the Housekeeping analyzers over their database implementation. And each time a customer using this version of the database runs go test, it runs the Query checker over each customer package that transitively imports the database. //go:build goanalysis package db import ( //go:analyze ./... example.com/internal/check.Housekeeping //go:analyze importers example.com/internal/check.Query _ "example.com/internal/check" ) In this way, the set of static analysis checks that is ordinarily run by go test can be deterministically extended beyond the usual subset of go vet simply by adding code to your project.
Annotations take the form of //go:analyze directive comments, to enable an analyzer. Each such directive must precede an import spec. You can only enable an analyzer to which you have import access. Annotations must appear in a file named goanalysis.go . This restriction reduces the number of files the build system needs to parse to find all applicable annotations. The annotation file must have a build tag of the form //go:build goanalysis .
This ensures that the file is not part of the ordinary build. (We are effectively clawing back a reserved tag, albeit one unlikely to have ever been used in practice.) Annotation files thus do not create true import dependencies; no real import cycle arises from a package ( fmt , say) declaring an analyzer that depends on fmt itself. However, import declarations in annotation files are visible to go mod tidy , so analyzer dependencies are versioned by go.mod, just like any other dependency.
The first argument of each annotation is its scope , which denotes a set of packages relative to the current package. It must have one of these forms: . this package ./... all packages of this module beneath this directory importers all transitive importers of this package (There is no "direct importers" scope for now. Though it would be useful and easy to implement, it can be efficiently simulated with a cheap direct-import check in an analyzer using importers scope.) The ./... and . scopes are used to enable "housekeeping" analyzers, intended to check packages within the same module.
The importers scope is used to enable API checking in client packages, analogous to the way that the printf analyzer checks all uses of the fmt.Printf function and wrappers such as log.Printf . The second argument of each annotation is its name , which denotes an exported package-level variable. This name identifies an analyzer to the build system. When an annotation precedes an import spec, as in the example above, the package path may be omitted (for example, //go:analyze importers Query ).
The example above thus enables the Housekeeping rules within the entire example.com/db/... tree, and enables the Query analyzer on all packages that transitively import the example.com/db package. A //go:noanalyze directive disables an analyzer. It need not precede an import spec. This ensures that you can disable an analyzer without
The rest of this page is on the live site.