Skip to content

cmd/go: go list -e -json with syntax error behaves differently in test and non-test files #28491

@myitcv

Description

@myitcv

Please answer these questions before submitting your issue. Thanks!

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

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/myitcv/go-modules-by-example/.gopath"
GOPROXY=""
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/myitcv/go-modules-by-example/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build914474323=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The context of this problem is:

  • https://github.com/stamblerre/gocode, which is the module-aware version of gocode, was failing to offer a completion in a _test.go file but succeeding in a regular .go file with the same content
  • This was traced down to a difference in behaviour in go/packages
  • @ianthehat then suggested we look at the go list level; hence this issue and repro

The following behaves as expected and lists the main package, despite the syntax error in main.go:

$ cd $(mktemp -d)
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
$ cat <<EOD >main.go
package main

func InTest() {
        var x interface{}
        x.
}
EOD
$ go list -e -json .
{
        "Dir": "/tmp/tmp.qlrtnE4Htk",
        "ImportPath": "example.com/hello",
        "Name": "main",
        "Target": "/home/gopher/gopath/bin/hello",
        "Module": {
                "Path": "example.com/hello",
                "Main": true,
                "Dir": "/tmp/tmp.qlrtnE4Htk",
                "GoMod": "/tmp/tmp.qlrtnE4Htk/go.mod"
        },
        "Match": [
                "."
        ],
        "Stale": true,
        "StaleReason": "build ID mismatch",
        "GoFiles": [
                "main.go"
        ],
        "Deps": [
                "internal/bytealg",
                "internal/cpu",
                "runtime",
                "runtime/internal/atomic",
                "runtime/internal/sys",
                "unsafe"
        ]
}

However, if we create the same syntax error in a test file (continuing the example above):

$ rm main.go
$ cat <<EOD >main_test.go
package main

func InTest() {
        var x interface{}
        x.
}
EOD
$ go list -e -test -json .
{
        "Dir": "/tmp/tmp.qlrtnE4Htk",
        "ImportPath": "example.com/hello",
        "Name": "main",
        "Target": "/home/gopher/gopath/bin/hello",
        "Module": {
                "Path": "example.com/hello",
                "Main": true,
                "Dir": "/tmp/tmp.qlrtnE4Htk",
                "GoMod": "/tmp/tmp.qlrtnE4Htk/go.mod"
        },
        "Match": [
                "."
        ],
        "Deps": [
                "internal/bytealg",
                "internal/cpu",
                "runtime",
                "runtime/internal/atomic",
                "runtime/internal/sys",
                "unsafe"
        ],
        "TestGoFiles": [
                "main_test.go"
        ]
}
{
        "ImportPath": "example.com/hello.test",
        "Error": {
                "ImportStack": null,
                "Pos": "",
                "Err": "\nmain_test.go:6:1: expected selector or type assertion, found '}'"
        }
}

the test package, example.com/hello.test, fails to list.

What did you expect to see?

Given the successful list of the main package despite the syntax error, I think it makes sense for the example.com/hello.test test package to list successfully too.

What did you see instead?

An error in the list of example.com/hello.test

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.modules

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions