Commit dc77dc2b authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go/internal/list: ensure that cfg.BuildMod is initialized before reading it in 'go list -m'

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: default avatarJay Conrod <jayconrod@google.com>
parent a42a3968
...@@ -381,12 +381,20 @@ func runList(cmd *base.Command, args []string) { ...@@ -381,12 +381,20 @@ func runList(cmd *base.Command, args []string) {
base.Fatalf("go list -test cannot be used with -m") base.Fatalf("go list -test cannot be used with -m")
} }
buildModIsDefault := (cfg.BuildMod == "")
if modload.Init(); !modload.Enabled() { if modload.Init(); !modload.Enabled() {
base.Fatalf("go list -m: not using modules") base.Fatalf("go list -m: not using modules")
} }
modload.InitMod() // Parses go.mod and sets cfg.BuildMod.
if cfg.BuildMod == "vendor" { 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() modload.LoadBuildList()
mods := modload.ListModules(args, *listU, *listVersions) mods := modload.ListModules(args, *listU, *listVersions)
......
...@@ -290,6 +290,9 @@ func die() { ...@@ -290,6 +290,9 @@ func die() {
// InitMod sets Target and, if there is a main module, parses the initial build // 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. // 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() { func InitMod() {
if len(buildList) > 0 { if len(buildList) > 0 {
return return
......
...@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init' ...@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
# The command we suggested should succeed. # The command we suggested should succeed.
cd .. cd ..
go mod init go mod init
go list -m all go list -mod=mod -m all
stdout '^m$' stdout '^m$'
-- $WORK/test/vendor/vendor.json -- -- $WORK/test/vendor/vendor.json --
......
...@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init' ...@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
# The command we suggested should succeed. # The command we suggested should succeed.
cd .. cd ..
go mod init go mod init
go list -m all go list -mod=mod -m all
stdout '^m$' stdout '^m$'
-- $WORK/test/vendor/manifest -- -- $WORK/test/vendor/manifest --
......
...@@ -11,7 +11,7 @@ stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$' ...@@ -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$' stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
! go list -mod=vendor -m rsc.io/quote@latest ! 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 ! go get -mod=vendor -u
stderr 'flag provided but not defined: -mod' stderr 'flag provided but not defined: -mod'
......
...@@ -35,7 +35,7 @@ stdout 'src[\\/]vendor[\\/]x' ...@@ -35,7 +35,7 @@ stdout 'src[\\/]vendor[\\/]x'
# 'go list -mod=vendor -m' does not have enough information to list modules # 'go list -mod=vendor -m' does not have enough information to list modules
# accurately, and should fail. # accurately, and should fail.
! go list -mod=vendor -f {{.Dir}} -m x ! 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, # 'go list -mod=mod' should report packages outside the import graph,
# but 'go list -mod=vendor' should error out for them. # but 'go list -mod=vendor' should error out for them.
......
...@@ -17,10 +17,10 @@ stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$' ...@@ -17,10 +17,10 @@ stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$' stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
! go list -m all ! 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 ! 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. # An explicit -mod=mod should force the vendor directory to be ignored.
env GOFLAGS=-mod=mod env GOFLAGS=-mod=mod
...@@ -103,6 +103,15 @@ stdout '^'$WORK'[/\\]auto$' ...@@ -103,6 +103,15 @@ stdout '^'$WORK'[/\\]auto$'
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$' stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$' 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 # '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 # 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 # default and should not fail the consistency check even though no module
......
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