golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.97k stars 17.67k forks source link

cmd/go: `go list -json -e` does not store location for invalid import path #69334

Open aykevl opened 2 months ago

aykevl commented 2 months ago

Go version

go version go1.23.1 linux/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/home/ayke/.cache/go-build'
GOENV='/home/ayke/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ayke/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ayke'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go1.23.1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go1.23.1/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/ayke/.config/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3054841504=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I have the following invalid Go file

package main

import _ "#"

func main() {
}

I ran go list -json -e importpath.go on it, to see the loader error.

What did you see happen?

The JSON contains this part:

        "Error": {
                "ImportStack": [],
                "Pos": "",
                "Err": "/home/ayke/tmp/importpath.go:3:8: invalid import path: #"
        }

That's an error, of course, but the Pos field is not set even though go list clearly knows the source location (it's stored in the error message itself).

What did you expect to see?

I expected an output like this:

        "Error": {
                "ImportStack": [],
                "Pos": "importpath.go:3:8",
                "Err": "invalid import path: #"
        }

That is, the position information should be in Pos and the path should be relative and not absolute. For example, when setting the import path to one that doesn't exist, the error looks like this:

        "Error": {
                "ImportStack": [
                        "command-line-arguments"
                ],
                "Pos": "importpath.go:3:8",
                "Err": "no required module provides package foo.bar: go.mod file not found in current directory or any parent directory; see 'go help modules'"
        }
gabyhelp commented 2 months ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

aykevl commented 2 months ago

Interestingly, @gabyhelp didn't show exact duplicates but does show this bug has been around for a while: it's also visible in #28491.

timothy-king commented 2 months ago

CC @matloob, @samthanawalla.