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: get / build fails if a commented import declaration exists after package declaration #37747

Closed
42wim opened this issue Mar 8, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@42wim
Copy link

42wim commented Mar 8, 2020

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

$ go version
go version go1.14 linux/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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build075745121=/tmp/go-build -gno-record-gcc-switches"

What did you do?

go get github.com/42wim/matterbridge (which includes a vendor that has a commented import path)

Simplified version below:

package main // import "something"

func main() {
}

What did you expect to see?

A working build.

What did you see instead?

root@363dc3fb8e36:/testing# go get github.com/42wim/matterbridge
/go/src/github.com/42wim/matterbridge/vendor/github.com/shazow/ssh-chat/sshd/terminal.go:10:2: code in directory /go/src/github.com/42wim/matterbridge/vendor/github.com/shazow/ssh-chat/sshd/terminal expects import "golang.org/x/crypto/ssh/terminal"

Or for the simplified version

root@363dc3fb8e36:/mytest# go build
can't load package: package .: code in directory /mytest expects import "something"

go run main.go works fine

Issue seems to be introduced in go 1.13
Can not reproduce it in go 1.12.17

42wim added a commit to 42wim/ssh-chat that referenced this issue Mar 8, 2020
@toothrot toothrot changed the title go get / go build fails if a commented import declaration exists after package declaration cmd/go: get / build fails if a commented import declaration exists after package declaration Mar 9, 2020
@toothrot
Copy link
Contributor

toothrot commented Mar 9, 2020

This is a feature of go import paths, see the full text of go help importpath, in the section titled "Import path checking":

https://pkg.go.dev/cmd/go?tab=doc#hdr-Import_path_checking

It is disabled if you are using modules. What is the output of go env when you are using go 1.12.17?

/cc @bcmills @jayconrod

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 9, 2020
@toothrot toothrot added this to the Backlog milestone Mar 9, 2020
@42wim
Copy link
Author

42wim commented Mar 9, 2020

The go env output with go1.12.17 below

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build400551887=/tmp/go-build -gno-record-gcc-switches"

@jayconrod
Copy link
Contributor

Seems like this is working as intended, so I'll close.

In GOPATH mode, // import comments force a package to only be imported with a canonical path. If a package is forked (which seems to be the case here), then // import comments must be updated or removed.

As @toothrot said, // import comments are not applied in module mode, but a similar restriction is applied through the module path in go.mod files.

I'm not sure why you wouldn't see errors in go1.12.17. The error is the correct behavior though.

@golang golang locked and limited conversation to collaborators Mar 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

4 participants