]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add go list -m -f {{.GoMod}} to show path to go.mod file
authorRuss Cox <rsc@golang.org>
Tue, 24 Jul 2018 19:01:22 +0000 (15:01 -0400)
committerRuss Cox <rsc@golang.org>
Sat, 28 Jul 2018 01:14:56 +0000 (01:14 +0000)
"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/125656
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modinfo/info.go
src/cmd/go/testdata/script/mod_enabled.txt
src/cmd/go/testdata/script/mod_list.txt

index c1d008ce381213b3522d05678f8736d0cad9b76a..d21e89683474319f9af1a89887d00d595042b9df 100644 (file)
@@ -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
     }
 
index 3920546c949ad113caa1ebc704a0a461308680d1..761b526b936926a4fdc7f7fbd2f64683767852ef 100644 (file)
@@ -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
 }
 
index 189d4edc91f22ba03447c3725dddffae3ced5026..828194da4a58ddfd10b403f481d9628dcf064804 100644 (file)
@@ -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
index 1597ebbf12d9110822ac46dfa24fdcb0e048d99b..bdf90074ab5e381ca3c55ba18ad1a68bf6c4da5a 100644 (file)
@@ -1,13 +1,13 @@
 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