Commit 0cb6b55f authored by Russ Cox's avatar Russ Cox

cmd/go: add go list -m -f {{.GoMod}} to show path to go.mod file

"go env GOMOD" gives this for the main module already
but it's useful to be able to query other modules.
Using {{.Dir}} does not work if the go.mod was auto-synthesized.

Change-Id: If4844571e9e429b541de0d40c36ff4c5743b2031
Reviewed-on: https://go-review.googlesource.com/125656Reviewed-by: 's avatarBryan C. Mills <bcmills@google.com>
parent 011b6ff8
......@@ -203,6 +203,7 @@ applied to a Go struct, but now a Module struct:
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
Error *ModuleError // error loading module
}
......
......@@ -19,6 +19,7 @@ type ModulePublic struct {
Main bool `json:",omitempty"` // is this the main module?
Indirect bool `json:",omitempty"` // module is only indirectly needed by main module
Dir string `json:",omitempty"` // directory holding local copy of files, if any
GoMod string `json:",omitempty"` // path to go.mod file describing module, if any
Error *ModuleError `json:",omitempty"` // error loading module
}
......
......@@ -4,6 +4,8 @@ env GO111MODULE=auto
cd $GOPATH/src/x/y/z
go env GOMOD
! stdout . # no non-empty lines
! go list -m -f {{.GoMod}}
stderr 'not using modules'
cd $GOPATH/src/x/y/z/w
go env GOMOD
......@@ -16,6 +18,8 @@ go env GOMOD
cd $GOPATH/foo
go env GOMOD
stdout foo[/\\]go.mod
go list -m -f {{.GoMod}}
stdout foo[/\\]go.mod
cd $GOPATH/foo/bar/baz
go env GOMOD
......
env GO111MODULE=on
# list {{.Dir}} shows main module but not not-yet-downloaded dependency
go list -m -f '{{.Path}} {{.Main}} {{.Dir}}' all
stdout '^x true .*[\\/]src$'
stdout '^rsc.io/quote false $'
# list {{.Dir}} shows main module and go.mod but not not-yet-downloaded dependency dir.
go list -m -f '{{.Path}} {{.Main}} {{.GoMod}} {{.Dir}}' all
stdout '^x true .*[\\/]src[\\/]go.mod .*[\\/]src$'
stdout '^rsc.io/quote false .*[\\/]v1.5.2.mod $'
# list {{.Dir}} shows dependency after download
go list -f {{.Dir}} rsc.io/quote
stdout 'mod[\\/]rsc.io[\\/]quote@v1.5.2'
# list {{.Dir}} shows dependency after download (and go list without -m downloads it)
go list -f '{{.Dir}}' rsc.io/quote
stdout '.*mod[\\/]rsc.io[\\/]quote@v1.5.2$'
# downloaded dependencies are read-only
exists -readonly $GOPATH/src/mod/rsc.io/quote@v1.5.2
......@@ -20,9 +20,9 @@ go clean -modcache
# list {{.Dir}} shows replaced directories
cp go.mod2 go.mod
go list -f {{.Dir}} rsc.io/quote
go list -m -f '{{.Path}} {{.Version}} {{.Dir}}{{with .Replace}} => {{.Version}} {{.Dir}}{{end}}' all
go list -m -f '{{.Path}} {{.Version}} {{.Dir}}{{with .Replace}} {{.GoMod}} => {{.Version}} {{.Dir}} {{.GoMod}}{{end}}' all
stdout 'mod[\\/]rsc.io[\\/]quote@v1.5.1'
stdout 'v1.3.0.*mod[\\/]rsc.io[\\/]sampler@v1.3.1 => v1.3.1.*sampler@v1.3.1'
stdout 'v1.3.0.*mod[\\/]rsc.io[\\/]sampler@v1.3.1 .*[\\/]v1.3.1.mod => v1.3.1.*sampler@v1.3.1 .*[\\/]v1.3.1.mod'
# list std should work
go list std
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment