Skip to content

x/tools/go/analysis/analysistest: Run will pass generated test files to analyzer #41771

@bombsimon

Description

@bombsimon

What version of Go are you using (go version)?

$ go version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/simon.sawert/Library/Caches/go-build"
GOENV="/Users/simon.sawert/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/simon.sawert/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/simon.sawert/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/simon.sawert/git/analyzer-mre/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dy/tkxx13zx3n1d_m28k_3lh6w00000gr/T/go-build111181793=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I created testdata including a test file (a file suffixed _test.go) and then I created a test and used analysistest.Run to test this code.

What did you expect to see?

Only my files from the testdata to be passed to the analyzer run function.

What did you see instead?

The generated test file(s) in the build cache gets passed.


The problem for me is that the analyzer I've created adds a diagnostics report on the test code generated in cache and thus I cannot get my test to pass. I guess this might be related to #40574.

Here's an minimal reproducible example:

% tree
.
├── amre.go
├── amre_test.go
└── testdata
    └── src
        └── mypackage
            ├── mypackage.go
            └── mypackage_test.go

3 directories, 6 files
package amre

import (
    "fmt"

    "golang.org/x/tools/go/analysis"
)

var Analyzer = &analysis.Analyzer{
    Name: "amre",
    Run:  run,
}

func run(pass *analysis.Pass) (interface{}, error) {
    for _, file := range pass.Files {
        filename := pass.Fset.Position(file.Pos()).Filename
        fmt.Println("testing", filename)
    }

    return nil, nil
}
package amre

import (
    "testing"

    "golang.org/x/tools/go/analysis/analysistest"
)

func TestA(t *testing.T) {
    testdata := analysistest.TestData()

    analysistest.Run(t, testdata, Analyzer, "mypackage")
}
% go test ./... -v
go test ./... -v
=== RUN   TestA
testing /Users/simon.sawert/Library/Caches/go-build/00/0090ea545eb3074c3efc79841f1af52ecd6009c127392415405c05125895f58a-d
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage.go
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage.go
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage_test.go
--- PASS: TestA (0.96s)
PASS
ok      analyzer-mre    1.258s

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions