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: default avatarBryan C. Mills <bcmills@google.com>
parent 011b6ff8
...@@ -203,6 +203,7 @@ applied to a Go struct, but now a Module struct: ...@@ -203,6 +203,7 @@ applied to a Go struct, but now a Module struct:
Main bool // is this the main module? Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module? Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any 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 Error *ModuleError // error loading module
} }
......
...@@ -19,6 +19,7 @@ type ModulePublic struct { ...@@ -19,6 +19,7 @@ type ModulePublic struct {
Main bool `json:",omitempty"` // is this the main module? Main bool `json:",omitempty"` // is this the main module?
Indirect bool `json:",omitempty"` // module is only indirectly needed by 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 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 Error *ModuleError `json:",omitempty"` // error loading module
} }
......
...@@ -4,6 +4,8 @@ env GO111MODULE=auto ...@@ -4,6 +4,8 @@ env GO111MODULE=auto
cd $GOPATH/src/x/y/z cd $GOPATH/src/x/y/z
go env GOMOD go env GOMOD
! stdout . # no non-empty lines ! stdout . # no non-empty lines
! go list -m -f {{.GoMod}}
stderr 'not using modules'
cd $GOPATH/src/x/y/z/w cd $GOPATH/src/x/y/z/w
go env GOMOD go env GOMOD
...@@ -16,6 +18,8 @@ go env GOMOD ...@@ -16,6 +18,8 @@ go env GOMOD
cd $GOPATH/foo cd $GOPATH/foo
go env GOMOD go env GOMOD
stdout foo[/\\]go.mod stdout foo[/\\]go.mod
go list -m -f {{.GoMod}}
stdout foo[/\\]go.mod
cd $GOPATH/foo/bar/baz cd $GOPATH/foo/bar/baz
go env GOMOD go env GOMOD
......
env GO111MODULE=on env GO111MODULE=on
# list {{.Dir}} shows main module but not not-yet-downloaded dependency # list {{.Dir}} shows main module and go.mod but not not-yet-downloaded dependency dir.
go list -m -f '{{.Path}} {{.Main}} {{.Dir}}' all go list -m -f '{{.Path}} {{.Main}} {{.GoMod}} {{.Dir}}' all
stdout '^x true .*[\\/]src$' stdout '^x true .*[\\/]src[\\/]go.mod .*[\\/]src$'
stdout '^rsc.io/quote false $' stdout '^rsc.io/quote false .*[\\/]v1.5.2.mod $'
# list {{.Dir}} shows dependency after download # list {{.Dir}} shows dependency after download (and go list without -m downloads it)
go list -f {{.Dir}} rsc.io/quote go list -f '{{.Dir}}' rsc.io/quote
stdout 'mod[\\/]rsc.io[\\/]quote@v1.5.2' stdout '.*mod[\\/]rsc.io[\\/]quote@v1.5.2$'
# downloaded dependencies are read-only # downloaded dependencies are read-only
exists -readonly $GOPATH/src/mod/rsc.io/quote@v1.5.2 exists -readonly $GOPATH/src/mod/rsc.io/quote@v1.5.2
...@@ -20,9 +20,9 @@ go clean -modcache ...@@ -20,9 +20,9 @@ go clean -modcache
# list {{.Dir}} shows replaced directories # list {{.Dir}} shows replaced directories
cp go.mod2 go.mod cp go.mod2 go.mod
go list -f {{.Dir}} rsc.io/quote 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 '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 # list std should work
go list std 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