Skip to content

cmd/go: bad error message for local imports in module mode #47088

Open
@bcmills

Description

@bcmills

In module mode, if an import statement refers to a relative path it is currently supposed to fail with relative import not supported:

if build.IsLocalImport(path) {
return module.Version{}, "", fmt.Errorf("relative import not supported")
}

However, we seem to be missing test coverage for that error message, and the actual error that is printed for that case is substantially different:

$ go version
go version devel go1.17-912f07504 Fri Jul 2 21:06:08 2021 +0000 linux/amd64

$ go build .
main.go:4:2: local import "./foo" in non-local package

-- foo/foo.go --
package foo
-- go.mod --
module example.com/m

go 1.17
-- main.go --
package main

import (
	_ "./foo"
)

func main() {}
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/usr/local/google/home/bcmills/.cache/go-build"
GOENV="/usr/local/google/home/bcmills/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/tmp/tmp.8tQ48Pjrac/.gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/tmp/tmp.8tQ48Pjrac/.gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/google/home/bcmills/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/google/home/bcmills/sdk/gotip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.17-912f07504 Fri Jul 2 21:06:08 2021 +0000"
GCCGO="/usr/local/google/home/bcmills/bin/gccgo"
AR="ar"
CC="gcc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/tmp/tmp.8tQ48Pjrac/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-build181655066=/tmp/go-build -gno-record-gcc-switches"

That error message comes from here:

err = ImportErrorf(path, "local import %q in non-local package", path)

which implies that the error returned by importFromModules is either not produced or not propagated as we expect.

Discovered via https://stackoverflow.com/q/68261085.

CC @matloob @jayconrod

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions