]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/list: ensure that cfg.BuildMod is initialized before reading it in...
authorBryan C. Mills <bcmills@google.com>
Wed, 23 Oct 2019 20:10:55 +0000 (16:10 -0400)
committerBryan C. Mills <bcmills@google.com>
Thu, 24 Oct 2019 13:21:45 +0000 (13:21 +0000)
The default value of cfg.BuildMod depends on the 'go' version in the
go.mod file. The go.mod file is read and parsed, and its settings are
applied, in modload.InitMod.

As it turns out, modload.Enabled does not invoke InitMod, so
cfg.BuildMod is not necessarily set even if modload.Enabled returns
true.

Updates #33848

Change-Id: I13a4dd80730528e6f1a5acc492fcfe07cb59d94e
Reviewed-on: https://go-review.googlesource.com/c/go/+/202917
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_convert_vendor_json.txt
src/cmd/go/testdata/script/mod_convert_vendor_manifest.txt
src/cmd/go/testdata/script/mod_getmode_vendor.txt
src/cmd/go/testdata/script/mod_vendor.txt
src/cmd/go/testdata/script/mod_vendor_auto.txt

index d8c75776bbac741703bc8c4c1f61ce62b4a1eab1..7965a84f99c79e07be4b469d7698e564e241b2e2 100644 (file)
@@ -381,12 +381,20 @@ func runList(cmd *base.Command, args []string) {
                        base.Fatalf("go list -test cannot be used with -m")
                }
 
+               buildModIsDefault := (cfg.BuildMod == "")
                if modload.Init(); !modload.Enabled() {
                        base.Fatalf("go list -m: not using modules")
                }
+
+               modload.InitMod() // Parses go.mod and sets cfg.BuildMod.
                if cfg.BuildMod == "vendor" {
-                       base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory")
+                       if buildModIsDefault {
+                               base.Fatalf("go list -m: can't list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.")
+                       } else {
+                               base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.")
+                       }
                }
+
                modload.LoadBuildList()
 
                mods := modload.ListModules(args, *listU, *listVersions)
index cda6c93652d9a3b950b23c22df3dc77fd743ca46..2f1509315890a31bd03d21058cca190f1bcb8eaf 100644 (file)
@@ -290,6 +290,9 @@ func die() {
 
 // InitMod sets Target and, if there is a main module, parses the initial build
 // list from its go.mod file, creating and populating that file if needed.
+//
+// As a side-effect, InitMod sets a default for cfg.BuildMod if it does not
+// already have an explicit value.
 func InitMod() {
        if len(buildList) > 0 {
                return
index 47d111d4c13b32965eb993d92d43805160aa59ea..2f5ccec32c615b5eb323d0af636a9577af9d5b3c 100644 (file)
@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
 # The command we suggested should succeed.
 cd ..
 go mod init
-go list -m all
+go list -mod=mod -m all
 stdout '^m$'
 
 -- $WORK/test/vendor/vendor.json --
index 68edb9dc292e8a953e00cae65a05e6ee92939205..6f3a145430ad959b423ff525be1920acc06cb235 100644 (file)
@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
 # The command we suggested should succeed.
 cd ..
 go mod init
-go list -m all
+go list -mod=mod -m all
 stdout '^m$'
 
 -- $WORK/test/vendor/manifest --
index 21fec5b85f97a90052381ecf9ee7434b20767801..baa79a083a9f9c5608bdbb0d863b095f2dbd2c9a 100644 (file)
@@ -11,7 +11,7 @@ stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$'
 stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
 
 ! go list -mod=vendor -m rsc.io/quote@latest
-stderr 'go list -m: can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
+stderr 'go list -m: can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
 ! go get -mod=vendor -u
 stderr 'flag provided but not defined: -mod'
 
index 9b716906e5915e94e4704531fde6af681cef268d..17fb2f3c1609d36af955bcbb4f9098f6558c3926 100644 (file)
@@ -35,7 +35,7 @@ stdout 'src[\\/]vendor[\\/]x'
 # 'go list -mod=vendor -m' does not have enough information to list modules
 # accurately, and should fail.
 ! go list -mod=vendor -f {{.Dir}} -m x
-stderr 'can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
+stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
 
 # 'go list -mod=mod' should report packages outside the import graph,
 # but 'go list -mod=vendor' should error out for them.
index 7abe833f57c9edb971e98ef2b211efabf9777881..6e79f6b99438e31712f64dc1589643829cf3787b 100644 (file)
@@ -17,10 +17,10 @@ stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
 
 ! go list -m all
-stderr 'can''t list modules with -mod=vendor'
+stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
 
 ! go list -m -f '{{.Dir}}' all
-stderr 'can''t list modules with -mod=vendor'
+stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
 
 # An explicit -mod=mod should force the vendor directory to be ignored.
 env GOFLAGS=-mod=mod
@@ -103,6 +103,15 @@ stdout '^'$WORK'[/\\]auto$'
 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
 
+# ...but 'go list -m' should continue to fail, this time without
+# referring to a -mod default that the user didn't set.
+! go list -m all
+stderr 'can''t list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.'
+
+! go list -m -f '{{.Dir}}' all
+stderr 'can''t list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.'
+
+
 # 'go mod init' should work if there is already a GOPATH-mode vendor directory
 # present. If there are no module dependencies, -mod=vendor should be used by
 # default and should not fail the consistency check even though no module