Skip to content

Commit dc77dc2

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent a42a396 commit dc77dc2

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

src/cmd/go/internal/list/list.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,20 @@ func runList(cmd *base.Command, args []string) {
381381
base.Fatalf("go list -test cannot be used with -m")
382382
}
383383

384+
buildModIsDefault := (cfg.BuildMod == "")
384385
if modload.Init(); !modload.Enabled() {
385386
base.Fatalf("go list -m: not using modules")
386387
}
388+
389+
modload.InitMod() // Parses go.mod and sets cfg.BuildMod.
387390
if cfg.BuildMod == "vendor" {
388-
base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory")
391+
if buildModIsDefault {
392+
base.Fatalf("go list -m: can't list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.")
393+
} else {
394+
base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.")
395+
}
389396
}
397+
390398
modload.LoadBuildList()
391399

392400
mods := modload.ListModules(args, *listU, *listVersions)

src/cmd/go/internal/modload/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ func die() {
290290

291291
// InitMod sets Target and, if there is a main module, parses the initial build
292292
// list from its go.mod file, creating and populating that file if needed.
293+
//
294+
// As a side-effect, InitMod sets a default for cfg.BuildMod if it does not
295+
// already have an explicit value.
293296
func InitMod() {
294297
if len(buildList) > 0 {
295298
return

src/cmd/go/testdata/script/mod_convert_vendor_json.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
1010
# The command we suggested should succeed.
1111
cd ..
1212
go mod init
13-
go list -m all
13+
go list -mod=mod -m all
1414
stdout '^m$'
1515

1616
-- $WORK/test/vendor/vendor.json --

src/cmd/go/testdata/script/mod_convert_vendor_manifest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ stderr '\s*cd \.\. && go mod init'
1010
# The command we suggested should succeed.
1111
cd ..
1212
go mod init
13-
go list -m all
13+
go list -mod=mod -m all
1414
stdout '^m$'
1515

1616
-- $WORK/test/vendor/manifest --

src/cmd/go/testdata/script/mod_getmode_vendor.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$'
1111
stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
1212

1313
! go list -mod=vendor -m rsc.io/quote@latest
14-
stderr 'go list -m: can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
14+
stderr 'go list -m: can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
1515
! go get -mod=vendor -u
1616
stderr 'flag provided but not defined: -mod'
1717

src/cmd/go/testdata/script/mod_vendor.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ stdout 'src[\\/]vendor[\\/]x'
3535
# 'go list -mod=vendor -m' does not have enough information to list modules
3636
# accurately, and should fail.
3737
! go list -mod=vendor -f {{.Dir}} -m x
38-
stderr 'can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
38+
stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
3939

4040
# 'go list -mod=mod' should report packages outside the import graph,
4141
# but 'go list -mod=vendor' should error out for them.

src/cmd/go/testdata/script/mod_vendor_auto.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
1717
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
1818

1919
! go list -m all
20-
stderr 'can''t list modules with -mod=vendor'
20+
stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
2121

2222
! go list -m -f '{{.Dir}}' all
23-
stderr 'can''t list modules with -mod=vendor'
23+
stderr 'can''t list modules with -mod=vendor\n\tUse -mod=mod or -mod=readonly to ignore the vendor directory.'
2424

2525
# An explicit -mod=mod should force the vendor directory to be ignored.
2626
env GOFLAGS=-mod=mod
@@ -103,6 +103,15 @@ stdout '^'$WORK'[/\\]auto$'
103103
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
104104
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
105105

106+
# ...but 'go list -m' should continue to fail, this time without
107+
# referring to a -mod default that the user didn't set.
108+
! go list -m all
109+
stderr 'can''t list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.'
110+
111+
! go list -m -f '{{.Dir}}' all
112+
stderr 'can''t list modules using the vendor directory\n\tUse -mod=mod or -mod=readonly to ignore it.'
113+
114+
106115
# 'go mod init' should work if there is already a GOPATH-mode vendor directory
107116
# present. If there are no module dependencies, -mod=vendor should be used by
108117
# default and should not fail the consistency check even though no module

0 commit comments

Comments
 (0)