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: reference to v2 module incorrectly using subdirectory of module #28083

Closed
hyangah opened this Issue Oct 8, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@hyangah
Copy link
Contributor

hyangah commented Oct 8, 2018

In the source tree of the queried package (and its module)

$ cd /tmp
$ git clone https://github.com/hyangah/explist2
$ cd explist2
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}}' github.com/hyangah/explist2
github.com/hyangah/explist2 in github.com/hyangah/explist2@

vs.

$ mkdir /tmp/temp
$ cd /tmp/temp
$ go mod init temp
go: creating new go.mod: module temp
$ go get github.com/hyangah/explist2
go: finding github.com/hyangah/explist2 v1.0.0
go: downloading github.com/hyangah/explist2 v1.0.0
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}}' github.com/hyangah/explist2
github.com/hyangah/explist2 in github.com/hyangah/explist2@v1.0.0

@hyangah hyangah added the modules label Oct 8, 2018

@myitcv

This comment has been minimized.

Copy link
Member

myitcv commented Oct 9, 2018

In the first case you are asking for the version of a module within the source tree for that module. To my mind the version of a module in this situation is undefined.

$ cd /tmp
$ git clone https://github.com/hyangah/explist2
Cloning into 'explist2'...
$ cd explist2
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}} [{{.Module.Dir}}]' github.com/hyangah/explist2
github.com/hyangah/explist2 in github.com/hyangah/explist2@ [/tmp/explist2]

In the second case you are querying a module as a dependency (that is present within the readonly-module cache). In this situation the module has been resolved via MVS and hence has a version associated with it.

$ mkdir /tmp/temp
$ cd /tmp/temp
$ go mod init temp
go: creating new go.mod: module temp
$ go get github.com/hyangah/explist2
go: finding github.com/hyangah/explist2 v1.0.0
go: downloading github.com/hyangah/explist2 v1.0.0
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}} [{{.Module.Dir}}]' github.com/hyangah/explist2
github.com/hyangah/explist2 in github.com/hyangah/explist2@v1.0.0 [/root/pkg/mod/github.com/hyangah/explist2@v1.0.0]

What version would you expect to be returned in the first instance out of interest?

@hyangah

This comment has been minimized.

Copy link
Contributor Author

hyangah commented Oct 9, 2018

Thanks for the explanation @myitcv . That makes sense.
I thought the above was a simple repro case for what I originally wanted to report. But clearly, that is not the case.

$ pwd
/tmp/explist2
$ tree
.
├── f.go
├── go.mod
├── go.sum
└── v2
    ├── f.go
    ├── go.mod
    └── go.sum
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}}' github.com/hyangah/explist2/v2
github.com/hyangah/explist2/v2 in github.com/hyangah/explist2@

The v2 is set up following the major subdirectory model. I expect querying v2 from the repo root directory to return the same result as the query outside the repo. But it does not. Moreover, it doesn't seem to recognize the v2 as the major version mark.

If I query from the v2 directory, at list the go list seemed to recognize v2 as a version mark.

$ cd v2
$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}}' github.com/hyangah/explist2/v2
github.com/hyangah/explist2/v2 in github.com/hyangah/explist2/v2@
@myitcv

This comment has been minimized.

Copy link
Member

myitcv commented Oct 9, 2018

If I query from the v2 directory, at list the go list seemed to recognize v2 as a version mark.

I don't think the "v2" in "github.com/hyangah/explist2/v2" can be considered a version per se, rather a convention on import paths. Indeed that import path comes entirely from the go.mod, not the fact that the go.mod is in a directory called v2 within github.com/hyangah/explist2.

cc @bcmills who will undoubtedly be tighter on the wording/definitions here than I am.

@hyangah hyangah changed the title cmd/go: `go list` without -m gives different answers depending on where the command runs cmd/go: `go list` without -m does not recognize the go.mod file in its subdirectory Oct 9, 2018

@hyangah

This comment has been minimized.

Copy link
Contributor Author

hyangah commented Oct 9, 2018

I should've used better wording.
The confusing part is the go.mod file in the subdirectory is not recognized.
The go list command considers any package under subdirectory to be part of the main module.

Even when I added a dependency on v2 from v1, the result is same.

$ cat f.go
package explist2

import "github.com/hyangah/explist2/v2"

var Name = explist2.Name

$ cat go.mod
module github.com/hyangah/explist2

require github.com/hyangah/explist2/v2 v2.0.1

$ go list -f '{{.ImportPath}} in {{.Module.Path}}@{{.Module.Version}}' github.com/hyangah/explist2/v2
github.com/hyangah/explist2/v2 in github.com/hyangah/explist2@

@bcmills bcmills modified the milestones: Go1.11.2, Go1.12 Oct 9, 2018

@bcmills bcmills added the NeedsFix label Oct 24, 2018

@rsc rsc changed the title cmd/go: `go list` without -m does not recognize the go.mod file in its subdirectory cmd/go: reference to v2 module incorrectly using subdirectory of module Oct 24, 2018

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 15, 2019

I don't know what was going on before, but as far as I can tell this is working as designed with go1.12beta2.

The v2/go.mod file prevents go from finding v2 in the main module, so it fetches from the tagged v2.0.1 instead:

explist2$ cat f.go
package explist2

import _ "github.com/hyangah/explist2/v2"

var Name = "explist2"

explist2$ go list -f '{{.ImportPath}} (from {{with .Module}}{{.Path}}@{{.Version}}{{end}}) {{.Dir}}' all
github.com/hyangah/explist2 (from github.com/hyangah/explist2@) /tmp/tmp.dhjf2me7MN/explist2
github.com/hyangah/explist2/v2 (from github.com/hyangah/explist2/v2@v2.0.1) /tmp/tmp.bNoat8kDP6/_gopath/pkg/mod/github.com/hyangah/explist2/v2@v2.0.1

@bcmills bcmills closed this Jan 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.