Skip to content

cmd/go: vet ignores custom tools’ -test=false flag #81598

Open

Description

@tamird

A custom analysis tool built with golang.org/x/tools/go/analysis/singlechecker advertises and accepts -test=false, but go vet still loads and analyzes test files. The same tool honors the option when invoked directly.

Reproduced on linux/arm64 in Docker with Go built from 89066c7:

go version go1.28-devel_89066c7425 Fri Sep 18 10:09:44 2026 -0700 linux/arm64

Using golang.org/x/tools v0.45.0, create these files:

// vettool/main.go
package main

import (
    "golang.org/x/tools/go/analysis/passes/printf"
    "golang.org/x/tools/go/analysis/singlechecker"
)

func main() { singlechecker.Main(printf.Analyzer) }
// p.go
package p
// p_test.go
package p

import "fmt"

func bad() { fmt.Printf("%d", "wrong") }

Then run:

go mod init example.com/repro
go get golang.org/x/tools/go/analysis/singlechecker@v0.45.0
go build -o printfcheck ./vettool
go vet -vettool=./printfcheck -test=false .

Expected: no diagnostics from p_test.go. Actual:

p_test.go:5:26: fmt.Printf format %d has arg "wrong" of wrong type string

Invoking ./printfcheck -test=false . succeeds without diagnostics.

The flag is discovered and forwarded by cmd/go, but cmd/go unconditionally sets ModResolveTests and calls TestPackagesFor. Singlechecker dispatches the resulting .cfg file to unitchecker, bypassing the standalone package loader that applies -test. This also means unresolved test-only imports can fail the command despite -test=false.

This was reported downstream in google/gvisor#8596 in 2023. Adin identified the driver interaction and suggested standalone invocation as a workaround. I could not find an existing Go issue for it.

A local cmd/go change applies the advertised boolean flag before package loading; a script regression fails before the change and passes after it. The flag-discovery protocol does not distinguish driver flags from analyzer flags, so treating other tools' boolean flags named test as package-selection flags is a compatibility consideration for this approach.

Assisted-by: Codex

Activity

  1. gopherbot commented on Sep 18, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/835045 mentions this issue: cmd/go: honor custom vet tools' -test flag

  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Sep 18, 2026
  3. added this to the Backlog milestone on Sep 18, 2026
  4. cherrymui commented on Sep 18, 2026

    @cherrymui
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions