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: import comments not enforced in module mode? #26367

Closed
myitcv opened this issue Jul 13, 2018 · 7 comments
Closed

cmd/go: import comments not enforced in module mode? #26367

myitcv opened this issue Jul 13, 2018 · 7 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 Jul 13, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version devel +8a330454dc Fri Jul 13 03:53:00 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

No; working from tip.

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/tmp/tmp.g3UOzwTmoR"
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=""
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-build903945026=/tmp/go-build -gno-record-gcc-switches"

What did you do?

cd `mktemp -d`
export GOPATH=$PWD
go get -d rsc.io/hello

(rsc/quote@5d9f230 is the HEAD of master at the time of writing)

What did you expect to see?

Successful go get

What did you see instead?

src/rsc.io/quote/quote.go:8:8: code in directory /tmp/tmp.Vm4iz3yBRc/src/rsc.io/quote/v3 expects import "rsc.io/quote"

/cc @rsc @bcmills

@myitcv myitcv added the modules label Jul 13, 2018
@myitcv myitcv added this to the Go1.11 milestone Jul 13, 2018
@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2018

I think this is actually a bug in rsc.io/quote/v3 itself:
https://github.com/rsc/quote/blob/5d9f230bcfbae514bb6c2215694c2ce7273fc604/v3/quote.go#L6

@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2018

Oh yeah: it's an intentional breakage in rsc.io/quote/v3.

Check out the commit message: rsc/quote@fe488b8#diff-43a03299a3c3fed3d8ce7b820f3aca81

@bcmills bcmills closed this as completed Jul 13, 2018
@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2018

Hmm, there may be another bug here, though: it's possible that in module mode we're not enforcing canonical import path comments.

@bcmills bcmills reopened this Jul 13, 2018
@bcmills bcmills changed the title cmd/go: go get fails for v >= 2 code in GOPATH mode cmd/go: canonical import paths not enforced in module mode? Jul 13, 2018
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 13, 2018
@bcmills bcmills self-assigned this Jul 13, 2018
@mvdan
Copy link
Member

mvdan commented Jul 15, 2018

I'm hitting similar issues when trying to port my v2 packages with vanity imports and // import "..." comments to a Go module:

can't load package: package mvdan.cc/sh/syntax: code in directory /home/travis/gopath/src/mvdan.cc/sh/syntax expects import "mvdan.cc/sh/v2/syntax"

You can see the full build log for 1.10.3 on Linux here: https://travis-ci.org/mvdan/sh/jobs/404138919

Building this outside of GOPATH on the latest tip works fine. I presume that 1.10 needs fixing, unless I'm missing something obvious.

@rsc
Copy link
Contributor

rsc commented Jul 17, 2018

I regret reminding people in the vgo-tour that canonical import paths even existed.

I used the import comment in the tour to show that if you are using them, then vgo could use that information to derive the correct 'module' line for an auto-initialized go.mod. Unfortunately, what lots of people took away instead was that using modules would mean using import comments. In fact, the opposite is true: import comments are basically obsoleted by modules.

The reason for introducing import comments was to make sure that if there were two different paths to name a package (because of custom import domain redirects) that an author could make sure all users were using the preferred name. Before that feature, there was no clear statement of the import path elements above the repository root.

With modules, we now have the clear statement we lacked before: the module statement in go.mod. Because of this, the module loading code ignores import comments entirely, and I expect that once we switch to modules we will probably deprecate them officially.

So it's working as intended that import comments are not enforced in module mode.

@mvdan, I would suggest just dropping the import comments from the v2 revs of your repo.

@rsc rsc changed the title cmd/go: canonical import paths not enforced in module mode? cmd/go: import comments not enforced in module mode? Jul 17, 2018
@rsc rsc closed this as completed Jul 17, 2018
@mvdan
Copy link
Member

mvdan commented Jul 17, 2018

That makes sense - thank you for the clarification. I think we should document this well, though. The reason I was trying to make import comments work was because they were there before, and no part of the Go modules documentation told me they were to be deprecated.

So this will be a non-issue once the feature is clearly marked as deprecated :)

ZymoticB added a commit to ZymoticB/spire that referenced this issue Mar 12, 2019
golang/go#26367 (comment)

That said, this does clean up the potential for the two different
versions of the same code imported in the same file to diverge.

Signed-off-by: Tyler Dixon <tylerd@uber.com>
@dominikh
Copy link
Member

Because of this, the module loading code ignores import comments entirely

Which is of course less than ideal when a user operates in module mode but go gets a package that doesn't have a go.mod file yet. The import comment will be ignored, and things will tend to break in unexpected ways due to github.com/foo/bar importing example.com/foo/baz.

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

6 participants