Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
myitcv opened this issue Oct 30, 2018 · 4 comments
Labels
FrozenDueToAge modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@myitcv
Copy link
Member

myitcv commented Oct 30, 2018

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

@katiehockman katiehockman added modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 31, 2018
@katiehockman katiehockman added this to the Go1.12 milestone Oct 31, 2018
@katiehockman
Copy link
Contributor

/cc @bcmills @rsc

@myitcv
Copy link
Member Author

myitcv commented Oct 31, 2018

Could be related to #25842, but this particular issue does not involve -export

@myitcv
Copy link
Member Author

myitcv commented Nov 28, 2018

@jayconrod this is also affecting the ability to use gocode etc in situations where this issue is triggered. In case you were looking for more low-hanging fruit 👍

@jayconrod jayconrod self-assigned this Nov 28, 2018
@andybons andybons modified the milestones: Go1.12, Go1.13 Feb 12, 2019
@gopherbot
Copy link

Change https://golang.org/cl/164357 mentions this issue: cmd/go: make go list error behavior consistent in tests

@golang golang locked and limited conversation to collaborators Mar 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants