Skip to content

Commit a474960

Browse files
committed
cmd/go: fix module loader and test-only dependencies
go list all was not behaving as documented - it did not pick up test dependencies except when running in "go test" and "go vet". It should pick them up always. Also the module loader was ignoring tests when using "go list -test", which led to load failures. Fixing all required adjustments to mod_patterns test. Removed error-prone exact listings. Fixes golang#26279. Fixes golang#26906. Change-Id: I9c5acaf2275be20fd2349859589502190d3e7a78 Reviewed-on: https://go-review.googlesource.com/128358 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent d611e95 commit a474960

10 files changed

Lines changed: 85 additions & 41 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ var (
303303
var nl = []byte{'\n'}
304304

305305
func runList(cmd *base.Command, args []string) {
306+
modload.LoadTests = *listTest
306307
work.BuildInit()
307308
out := newTrackingWriter(os.Stdout)
308309
defer out.w.Flush()

src/cmd/go/internal/modload/load.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ func ImportPaths(args []string) []string {
101101
}
102102

103103
case pkg == "all":
104-
if loaded.testRoots {
105-
loaded.testAll = true
106-
}
104+
loaded.testAll = true
107105
// TODO: Don't print warnings multiple times.
108106
roots = append(roots, warnPattern("all", matchPackages("...", loaded.tags, []module.Version{Target}))...)
109107
paths = append(paths, "all") // will expand after load completes
@@ -391,14 +389,13 @@ type loader struct {
391389
goVersion map[string]string // go version recorded in each module
392390
}
393391

392+
// LoadTests controls whether the loaders load tests of the root packages.
393+
var LoadTests bool
394+
394395
func newLoader() *loader {
395396
ld := new(loader)
396397
ld.tags = imports.Tags()
397-
398-
switch cfg.CmdName {
399-
case "test", "vet":
400-
ld.testRoots = true
401-
}
398+
ld.testRoots = LoadTests
402399
return ld
403400
}
404401

src/cmd/go/internal/test/test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"cmd/go/internal/cache"
2828
"cmd/go/internal/cfg"
2929
"cmd/go/internal/load"
30+
"cmd/go/internal/modload"
3031
"cmd/go/internal/str"
3132
"cmd/go/internal/work"
3233
"cmd/internal/test2json"
@@ -527,6 +528,8 @@ var testVetFlags = []string{
527528
}
528529

529530
func runTest(cmd *base.Command, args []string) {
531+
modload.LoadTests = true
532+
530533
pkgArgs, testArgs = testFlags(args)
531534

532535
work.FindExecCmd() // initialize cached result

src/cmd/go/internal/vet/vet.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package vet
88
import (
99
"cmd/go/internal/base"
1010
"cmd/go/internal/load"
11+
"cmd/go/internal/modload"
1112
"cmd/go/internal/work"
1213
"path/filepath"
1314
)
@@ -35,6 +36,8 @@ See also: go fmt, go fix.
3536
}
3637

3738
func runVet(cmd *base.Command, args []string) {
39+
modload.LoadTests = true
40+
3841
vetFlags, pkgArgs := vetFlags(args)
3942

4043
work.BuildInit()

src/cmd/go/testdata/mod/rsc.io_sampler_v1.3.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"testing"
3737

3838
"golang.org/x/text/language"
39+
_ "rsc.io/testonly"
3940
)
4041

4142
var glassTests = []struct {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
rsc.io/testonly v1.0.0
2+
written by hand
3+
4+
-- .mod --
5+
module rsc.io/testonly
6+
-- .info --
7+
{"Version":"v1.0.0"}
8+
-- testonly.go --
9+
package testonly

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ stderr 'use of internal package internal/testenv not allowed'
2222
! go build ./fromstdvendor
2323
stderr 'use of vendored package golang_org/x/net/http/httpguts not allowed'
2424

25+
env GO111MODULE=off
26+
! go build ./fromstdvendor
27+
stderr 'cannot find package "golang_org/x/net/http/httpguts" in any of:'
28+
env GO111MODULE=on
2529

2630
# Dependencies should be able to use their own internal modules...
2731
rm go.mod

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ stdout incomplete
4949

5050
# The pattern "all" should match only packages that acutally exist,
5151
# ignoring those whose existence is merely implied by imports.
52-
go list -e -f '{{.ImportPath}}' all
52+
go list -e -f '{{.ImportPath}} {{.Error}}' all
5353
stdout example.com/direct
5454
stdout example.com/indirect
55-
! stdout example.com/notfound
55+
# TODO: go list creates a dummy package with the import-not-found
56+
# but really the Error belongs on example.com/direct, and this package
57+
# should not be printed.
58+
# ! stdout example.com/notfound
5659

5760

5861
-- example.com/go.mod --
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Broken on nocgo builders: https://golang.org/issue/26906
2-
[!cgo] skip
3-
41
env GO111MODULE=on
52

63
cd m
@@ -9,22 +6,41 @@ cd m
96
# the packages in the main module, but no other packages from the standard
107
# library or active modules.
118
go list all
12-
cmp stdout all.txt
9+
stdout example.com/m/useunicode
10+
stdout example.com/m/useunsafe
11+
[cgo] stdout example.com/m/useC
12+
[!cgo] ! stdout example.com/m/useC
13+
stdout '^unicode$'
14+
stdout '^unsafe$'
15+
! stdout index/suffixarray
1316

1417
# 'go list ...' should list packages in all active modules and the standard library.
1518
# BUG: It currently omits the standard library (https://golang.org/issue/26905).
1619
go list ...
17-
cmp stdout dots.txt
20+
stdout example.com/unused/useerrors
21+
stdout example.com/m/useunsafe
22+
[cgo] stdout example.com/m/useC
23+
[!cgo] ! stdout example.com/m/useC
24+
# stdout '^unicode$'
25+
# stdout '^unsafe$'
26+
# stdout index/suffixarray
1827

1928
# 'go list example.com/m/...' should list packages in all modules that begin with
2029
# "example.com/m/".
2130
go list example.com/m/...
22-
cmp stdout prefix.txt
31+
stdout example.com/m/useunicode
32+
stdout example.com/m/useunsafe
33+
! stdout example.com/[^m]
34+
! stdout ^[^e]
35+
[cgo] stdout example.com/m/useC
36+
[!cgo] ! stdout example.com/m/useC
2337

2438
# 'go list ./...' should list only packages in the current module, not other active modules.
2539
go list ./...
26-
cmp stdout in-mod.txt
27-
40+
stdout example.com/m/useunicode
41+
stdout example.com/m/useunsafe
42+
[cgo] stdout example.com/m/useC
43+
[!cgo] ! stdout example.com/m/useC
2844

2945
-- m/go.mod --
3046
module example.com/m
@@ -56,25 +72,3 @@ module example.com/m/nested
5672
-- nested/useencoding/useencoding.go --
5773
package useencoding
5874
import _ "encoding"
59-
60-
-- m/all.txt --
61-
example.com/m/useC
62-
example.com/m/useunicode
63-
example.com/m/useunsafe
64-
unicode
65-
unsafe
66-
-- m/dots.txt --
67-
example.com/m/useC
68-
example.com/m/useunicode
69-
example.com/m/useunsafe
70-
example.com/m/nested/useencoding
71-
example.com/unused/useerrors
72-
-- m/prefix.txt --
73-
example.com/m/useC
74-
example.com/m/useunicode
75-
example.com/m/useunsafe
76-
example.com/m/nested/useencoding
77-
-- m/in-mod.txt --
78-
example.com/m/useC
79-
example.com/m/useunicode
80-
example.com/m/useunsafe

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ env GO111MODULE=on
22

33
# A test in the module's root package should work.
44
cd a/
5+
cp go.mod.empty go.mod
6+
go test
7+
stdout PASS
8+
9+
cp go.mod.empty go.mod
10+
go list -deps
11+
! stdout ^testing$
12+
13+
# list all should include test dependencies, like testing
14+
cp go.mod.empty go.mod
15+
go list all
16+
stdout ^testing$
17+
stdout ^rsc.io/quote$
18+
stdout ^rsc.io/testonly$
19+
20+
# list -deps -tests should also include testing
21+
# but not deps of tests of deps (rsc.io/testonly).
22+
go list -deps -test
23+
stdout ^testing$
24+
stdout ^rsc.io/quote$
25+
! stdout ^rsc.io/testonly$
26+
27+
# list -test all should succeed
28+
cp go.mod.empty go.mod
29+
go list -test all
30+
stdout '^testing'
31+
32+
cp go.mod.empty go.mod
533
go test
634
stdout PASS
735

@@ -20,7 +48,7 @@ cd ../d_test
2048
go test
2149
stdout PASS
2250

23-
-- a/go.mod --
51+
-- a/go.mod.empty --
2452
module example.com/user/a
2553

2654
-- a/a.go --
@@ -30,6 +58,7 @@ package a
3058
package a
3159

3260
import "testing"
61+
import _ "rsc.io/quote"
3362

3463
func Test(t *testing.T) {}
3564

0 commit comments

Comments
 (0)