Commit a4749604 authored by Russ Cox's avatar Russ Cox

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 #26279.
Fixes #26906.

Change-Id: I9c5acaf2275be20fd2349859589502190d3e7a78
Reviewed-on: https://go-review.googlesource.com/128358Reviewed-by: 's avatarBryan C. Mills <bcmills@google.com>
parent d611e95c
...@@ -303,6 +303,7 @@ var ( ...@@ -303,6 +303,7 @@ var (
var nl = []byte{'\n'} var nl = []byte{'\n'}
func runList(cmd *base.Command, args []string) { func runList(cmd *base.Command, args []string) {
modload.LoadTests = *listTest
work.BuildInit() work.BuildInit()
out := newTrackingWriter(os.Stdout) out := newTrackingWriter(os.Stdout)
defer out.w.Flush() defer out.w.Flush()
......
...@@ -101,9 +101,7 @@ func ImportPaths(args []string) []string { ...@@ -101,9 +101,7 @@ func ImportPaths(args []string) []string {
} }
case pkg == "all": case pkg == "all":
if loaded.testRoots { loaded.testAll = true
loaded.testAll = true
}
// TODO: Don't print warnings multiple times. // TODO: Don't print warnings multiple times.
roots = append(roots, warnPattern("all", matchPackages("...", loaded.tags, []module.Version{Target}))...) roots = append(roots, warnPattern("all", matchPackages("...", loaded.tags, []module.Version{Target}))...)
paths = append(paths, "all") // will expand after load completes paths = append(paths, "all") // will expand after load completes
...@@ -391,14 +389,13 @@ type loader struct { ...@@ -391,14 +389,13 @@ type loader struct {
goVersion map[string]string // go version recorded in each module goVersion map[string]string // go version recorded in each module
} }
// LoadTests controls whether the loaders load tests of the root packages.
var LoadTests bool
func newLoader() *loader { func newLoader() *loader {
ld := new(loader) ld := new(loader)
ld.tags = imports.Tags() ld.tags = imports.Tags()
ld.testRoots = LoadTests
switch cfg.CmdName {
case "test", "vet":
ld.testRoots = true
}
return ld return ld
} }
......
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"cmd/go/internal/cache" "cmd/go/internal/cache"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/load" "cmd/go/internal/load"
"cmd/go/internal/modload"
"cmd/go/internal/str" "cmd/go/internal/str"
"cmd/go/internal/work" "cmd/go/internal/work"
"cmd/internal/test2json" "cmd/internal/test2json"
...@@ -527,6 +528,8 @@ var testVetFlags = []string{ ...@@ -527,6 +528,8 @@ var testVetFlags = []string{
} }
func runTest(cmd *base.Command, args []string) { func runTest(cmd *base.Command, args []string) {
modload.LoadTests = true
pkgArgs, testArgs = testFlags(args) pkgArgs, testArgs = testFlags(args)
work.FindExecCmd() // initialize cached result work.FindExecCmd() // initialize cached result
......
...@@ -8,6 +8,7 @@ package vet ...@@ -8,6 +8,7 @@ package vet
import ( import (
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/load" "cmd/go/internal/load"
"cmd/go/internal/modload"
"cmd/go/internal/work" "cmd/go/internal/work"
"path/filepath" "path/filepath"
) )
...@@ -35,6 +36,8 @@ See also: go fmt, go fix. ...@@ -35,6 +36,8 @@ See also: go fmt, go fix.
} }
func runVet(cmd *base.Command, args []string) { func runVet(cmd *base.Command, args []string) {
modload.LoadTests = true
vetFlags, pkgArgs := vetFlags(args) vetFlags, pkgArgs := vetFlags(args)
work.BuildInit() work.BuildInit()
......
...@@ -36,6 +36,7 @@ import ( ...@@ -36,6 +36,7 @@ import (
"testing" "testing"
"golang.org/x/text/language" "golang.org/x/text/language"
_ "rsc.io/testonly"
) )
var glassTests = []struct { var glassTests = []struct {
......
rsc.io/testonly v1.0.0
written by hand
-- .mod --
module rsc.io/testonly
-- .info --
{"Version":"v1.0.0"}
-- testonly.go --
package testonly
...@@ -22,6 +22,10 @@ stderr 'use of internal package internal/testenv not allowed' ...@@ -22,6 +22,10 @@ stderr 'use of internal package internal/testenv not allowed'
! go build ./fromstdvendor ! go build ./fromstdvendor
stderr 'use of vendored package golang_org/x/net/http/httpguts not allowed' stderr 'use of vendored package golang_org/x/net/http/httpguts not allowed'
env GO111MODULE=off
! go build ./fromstdvendor
stderr 'cannot find package "golang_org/x/net/http/httpguts" in any of:'
env GO111MODULE=on
# Dependencies should be able to use their own internal modules... # Dependencies should be able to use their own internal modules...
rm go.mod rm go.mod
......
...@@ -49,10 +49,13 @@ stdout incomplete ...@@ -49,10 +49,13 @@ stdout incomplete
# The pattern "all" should match only packages that acutally exist, # The pattern "all" should match only packages that acutally exist,
# ignoring those whose existence is merely implied by imports. # ignoring those whose existence is merely implied by imports.
go list -e -f '{{.ImportPath}}' all go list -e -f '{{.ImportPath}} {{.Error}}' all
stdout example.com/direct stdout example.com/direct
stdout example.com/indirect stdout example.com/indirect
! stdout example.com/notfound # TODO: go list creates a dummy package with the import-not-found
# but really the Error belongs on example.com/direct, and this package
# should not be printed.
# ! stdout example.com/notfound
-- example.com/go.mod -- -- example.com/go.mod --
......
# Broken on nocgo builders: https://golang.org/issue/26906
[!cgo] skip
env GO111MODULE=on env GO111MODULE=on
cd m cd m
...@@ -9,22 +6,41 @@ cd m ...@@ -9,22 +6,41 @@ cd m
# the packages in the main module, but no other packages from the standard # the packages in the main module, but no other packages from the standard
# library or active modules. # library or active modules.
go list all go list all
cmp stdout all.txt stdout example.com/m/useunicode
stdout example.com/m/useunsafe
[cgo] stdout example.com/m/useC
[!cgo] ! stdout example.com/m/useC
stdout '^unicode$'
stdout '^unsafe$'
! stdout index/suffixarray
# 'go list ...' should list packages in all active modules and the standard library. # 'go list ...' should list packages in all active modules and the standard library.
# BUG: It currently omits the standard library (https://golang.org/issue/26905). # BUG: It currently omits the standard library (https://golang.org/issue/26905).
go list ... go list ...
cmp stdout dots.txt stdout example.com/unused/useerrors
stdout example.com/m/useunsafe
[cgo] stdout example.com/m/useC
[!cgo] ! stdout example.com/m/useC
# stdout '^unicode$'
# stdout '^unsafe$'
# stdout index/suffixarray
# 'go list example.com/m/...' should list packages in all modules that begin with # 'go list example.com/m/...' should list packages in all modules that begin with
# "example.com/m/". # "example.com/m/".
go list example.com/m/... go list example.com/m/...
cmp stdout prefix.txt stdout example.com/m/useunicode
stdout example.com/m/useunsafe
! stdout example.com/[^m]
! stdout ^[^e]
[cgo] stdout example.com/m/useC
[!cgo] ! stdout example.com/m/useC
# 'go list ./...' should list only packages in the current module, not other active modules. # 'go list ./...' should list only packages in the current module, not other active modules.
go list ./... go list ./...
cmp stdout in-mod.txt stdout example.com/m/useunicode
stdout example.com/m/useunsafe
[cgo] stdout example.com/m/useC
[!cgo] ! stdout example.com/m/useC
-- m/go.mod -- -- m/go.mod --
module example.com/m module example.com/m
...@@ -56,25 +72,3 @@ module example.com/m/nested ...@@ -56,25 +72,3 @@ module example.com/m/nested
-- nested/useencoding/useencoding.go -- -- nested/useencoding/useencoding.go --
package useencoding package useencoding
import _ "encoding" import _ "encoding"
-- m/all.txt --
example.com/m/useC
example.com/m/useunicode
example.com/m/useunsafe
unicode
unsafe
-- m/dots.txt --
example.com/m/useC
example.com/m/useunicode
example.com/m/useunsafe
example.com/m/nested/useencoding
example.com/unused/useerrors
-- m/prefix.txt --
example.com/m/useC
example.com/m/useunicode
example.com/m/useunsafe
example.com/m/nested/useencoding
-- m/in-mod.txt --
example.com/m/useC
example.com/m/useunicode
example.com/m/useunsafe
...@@ -2,6 +2,34 @@ env GO111MODULE=on ...@@ -2,6 +2,34 @@ env GO111MODULE=on
# A test in the module's root package should work. # A test in the module's root package should work.
cd a/ cd a/
cp go.mod.empty go.mod
go test
stdout PASS
cp go.mod.empty go.mod
go list -deps
! stdout ^testing$
# list all should include test dependencies, like testing
cp go.mod.empty go.mod
go list all
stdout ^testing$
stdout ^rsc.io/quote$
stdout ^rsc.io/testonly$
# list -deps -tests should also include testing
# but not deps of tests of deps (rsc.io/testonly).
go list -deps -test
stdout ^testing$
stdout ^rsc.io/quote$
! stdout ^rsc.io/testonly$
# list -test all should succeed
cp go.mod.empty go.mod
go list -test all
stdout '^testing'
cp go.mod.empty go.mod
go test go test
stdout PASS stdout PASS
...@@ -20,7 +48,7 @@ cd ../d_test ...@@ -20,7 +48,7 @@ cd ../d_test
go test go test
stdout PASS stdout PASS
-- a/go.mod.empty --
module example.com/user/a module example.com/user/a
-- a/a.go -- -- a/a.go --
...@@ -30,6 +58,7 @@ package a ...@@ -30,6 +58,7 @@ package a
package a package a
import "testing" import "testing"
import _ "rsc.io/quote"
func Test(t *testing.T) {} func Test(t *testing.T) {}
......
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