Skip to content

Commit ab724d4

Browse files
author
Jay Conrod
committed
cmd/go: make 'go get -t' consider test dependencies in module mode
Fixes golang#32037 Change-Id: I696fe2029e383746252f37fe8d30df71b5ac8a6c Reviewed-on: https://go-review.googlesource.com/c/go/+/177677 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 776e170 commit ab724d4

4 files changed

Lines changed: 100 additions & 22 deletions

File tree

src/cmd/go/alldocs.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modget/get.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
var CmdGet = &base.Command{
3232
// Note: -d -m -u are listed explicitly because they are the most common get flags.
3333
// Do not send CLs removing them because they're covered by [get flags].
34-
UsageLine: "go get [-d] [-m] [-u] [-v] [-insecure] [build flags] [packages]",
34+
UsageLine: "go get [-d] [-m] [-t] [-u] [-v] [-insecure] [build flags] [packages]",
3535
Short: "add dependencies to current module and install them",
3636
Long: `
3737
Get resolves and adds dependencies to the current development module
@@ -75,6 +75,9 @@ will use the latest A but then use B v1.2.3, as requested by A. (If there
7575
are competing requirements for a particular module, then 'go get' resolves
7676
those requirements by taking the maximum requested version.)
7777
78+
The -t flag instructs get to consider modules needed to build tests of
79+
packages specified on the command line.
80+
7881
The -u flag instructs get to update dependencies to use newer minor or
7982
patch releases when available. Continuing the previous example,
8083
'go get -u A' will use the latest A with B v1.3.1 (not B v1.2.3).
@@ -85,6 +88,9 @@ Continuing the previous example,
8588
'go get -u=patch A@latest' will use the latest A with B v1.2.4 (not B v1.2.3),
8689
while 'go get -u=patch A' will use a patch release of A instead.
8790
91+
When the -t and -u flags are used together, get will update
92+
test dependencies as well.
93+
8894
In general, adding a new dependency may require upgrading
8995
existing dependencies to keep a working build, and 'go get' does
9096
this automatically. Similarly, downgrading one dependency may
@@ -261,9 +267,7 @@ func runGet(cmd *base.Command, args []string) {
261267
if *getFix {
262268
fmt.Fprintf(os.Stderr, "go get: -fix flag is a no-op when using modules\n")
263269
}
264-
if *getT {
265-
fmt.Fprintf(os.Stderr, "go get: -t flag is a no-op when using modules\n")
266-
}
270+
modload.LoadTests = *getT
267271

268272
if cfg.BuildMod == "vendor" {
269273
base.Fatalf("go get: disabled by -mod=%s", cfg.BuildMod)
@@ -781,25 +785,26 @@ func newUpgrader(cmdline map[string]*query, pkgs map[string]bool) *upgrader {
781785
// Initialize work queue with root packages.
782786
seen := make(map[string]bool)
783787
var work []string
784-
for pkg := range pkgs {
785-
seen[pkg] = true
786-
for _, imp := range modload.PackageImports(pkg) {
787-
if !pkgs[imp] && !seen[imp] {
788-
seen[imp] = true
789-
work = append(work, imp)
790-
}
788+
add := func(path string) {
789+
if !seen[path] {
790+
seen[path] = true
791+
work = append(work, path)
791792
}
792793
}
794+
for pkg := range pkgs {
795+
add(pkg)
796+
}
793797
for len(work) > 0 {
794798
pkg := work[0]
795799
work = work[1:]
796800
m := modload.PackageModule(pkg)
797801
u.upgrade[m.Path] = true
798-
for _, imp := range modload.PackageImports(pkg) {
799-
if !seen[imp] {
800-
seen[imp] = true
801-
work = append(work, imp)
802-
}
802+
imports, testImports := modload.PackageImports(pkg)
803+
for _, imp := range imports {
804+
add(imp)
805+
}
806+
for _, imp := range testImports {
807+
add(imp)
803808
}
804809
}
805810
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,26 @@ func PackageModule(path string) module.Version {
496496
}
497497

498498
// PackageImports returns the imports for the package named by the import path.
499-
// It does not include test imports. It returns nil for unknown packages.
500-
func PackageImports(path string) []string {
499+
// Test imports will be returned as well if tests were loaded for the package
500+
// (i.e., if "all" was loaded or if LoadTests was set and the path was matched
501+
// by a command line argument). PackageImports will return nil for
502+
// unknown package paths.
503+
func PackageImports(path string) (imports, testImports []string) {
501504
pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
502505
if !ok {
503-
return nil
506+
return nil, nil
504507
}
505-
imports := make([]string, len(pkg.imports))
508+
imports = make([]string, len(pkg.imports))
506509
for i, p := range pkg.imports {
507510
imports[i] = p.path
508511
}
509-
return imports
512+
if pkg.test != nil {
513+
testImports = make([]string, len(pkg.test.imports))
514+
for i, p := range pkg.test.imports {
515+
testImports[i] = p.path
516+
}
517+
}
518+
return imports, testImports
510519
}
511520

512521
// ModuleUsedDirectly reports whether the main module directly imports
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
env GO111MODULE=on
2+
3+
# By default, 'go get' should ignore tests
4+
cp go.mod.empty go.mod
5+
go get m/a
6+
! grep rsc.io/quote go.mod
7+
8+
# 'go get -t' should consider test dependencies of the named package.
9+
cp go.mod.empty go.mod
10+
go get -d -t m/a
11+
grep 'rsc.io/quote v1.5.2$' go.mod
12+
13+
# 'go get -t' should not consider test dependencies of imported packages,
14+
# including packages imported from tests.
15+
cp go.mod.empty go.mod
16+
go get -d -t m/b
17+
! grep rsc.io/quote go.mod
18+
19+
# 'go get -t -u' should update test dependencies of the named package.
20+
cp go.mod.empty go.mod
21+
go mod edit -require=rsc.io/[email protected]
22+
go get -d -t -u m/a
23+
grep 'rsc.io/quote v1.5.2$' go.mod
24+
25+
# 'go get -t -u' should not add or update test dependencies
26+
# of imported packages, including packages imported from tests.
27+
cp go.mod.empty go.mod
28+
go get -d -t -u m/b
29+
! grep rsc.io/quote go.mod
30+
go mod edit -require=rsc.io/[email protected]
31+
go get -d -t -u m/b
32+
grep 'rsc.io/quote v1.5.1$' go.mod
33+
34+
# 'go get all' should consider test dependencies with or without -t.
35+
cp go.mod.empty go.mod
36+
go get all
37+
grep 'rsc.io/quote v1.5.2$' go.mod
38+
39+
-- go.mod.empty --
40+
module m
41+
42+
-- a/a.go --
43+
package a
44+
45+
-- a/a_test.go --
46+
package a_test
47+
48+
import _ "rsc.io/quote"
49+
50+
-- b/b.go --
51+
package b
52+
53+
import _ "m/a"
54+
55+
-- b/b_test.go --
56+
package b_test
57+
58+
import _ "m/a"

0 commit comments

Comments
 (0)