Skip to content

Commit e9c9683

Browse files
author
Bryan C. Mills
committed
cmd/go: suppress errors from 'go get -d' for packages that only conditionally exist
Fixes #44106 Fixes #29268 Change-Id: Id113f2ced274d43fbf66cb804581448218996f81 Reviewed-on: https://go-review.googlesource.com/c/go/+/289769 TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]>
1 parent e0ac989 commit e9c9683

File tree

2 files changed

+131
-5
lines changed

2 files changed

+131
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,9 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
380380
pkgs := load.PackagesAndErrors(ctx, pkgPatterns)
381381
load.CheckPackageErrors(pkgs)
382382
work.InstallPackages(ctx, pkgPatterns, pkgs)
383-
// TODO(#40276): After Go 1.16, print a deprecation notice when building
384-
// and installing main packages. 'go install pkg' or
385-
// 'go install pkg@version' should be used instead.
386-
// Give the specific argument to use if possible.
383+
// TODO(#40276): After Go 1.16, print a deprecation notice when building and
384+
// installing main packages. 'go install pkg' or 'go install pkg@version'
385+
// should be used instead. Give the specific argument to use if possible.
387386
}
388387

389388
if !modload.HasModRoot() {
@@ -1453,7 +1452,18 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
14531452
}
14541453
}
14551454
for _, pkg := range pkgs {
1456-
if _, _, err := modload.Lookup("", false, pkg); err != nil {
1455+
if dir, _, err := modload.Lookup("", false, pkg); err != nil {
1456+
if dir != "" && errors.Is(err, imports.ErrNoGo) {
1457+
// Since dir is non-empty, we must have located source files
1458+
// associated with either the package or its test — ErrNoGo must
1459+
// indicate that none of those source files happen to apply in this
1460+
// configuration. If we are actually building the package (no -d
1461+
// flag), the compiler will report the problem; otherwise, assume that
1462+
// the user is going to build or test it in some other configuration
1463+
// and suppress the error.
1464+
continue
1465+
}
1466+
14571467
base.SetExitStatus(1)
14581468
if ambiguousErr := (*modload.AmbiguousImportError)(nil); errors.As(err, &ambiguousErr) {
14591469
for _, m := range ambiguousErr.Modules {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# https://golang.org/issue/44106
2+
# 'go get' should fetch the transitive dependencies of packages regardless of
3+
# tags, but shouldn't error out if the package is missing tag-guarded
4+
# dependencies.
5+
6+
# Control case: just adding the top-level module to the go.mod file does not
7+
# fetch its dependencies.
8+
9+
go mod edit -require example.net/[email protected]
10+
! go list -deps example.net/cmd/tool
11+
stderr '^module example\.net/cmd provides package example\.net/cmd/tool and is replaced but not required; to add it:\n\tgo get example\.net/cmd@v0\.1\.0$'
12+
go mod edit -droprequire example.net/tools
13+
14+
15+
# 'go get -d' makes a best effort to fetch those dependencies, but shouldn't
16+
# error out if dependencies of tag-guarded files are missing.
17+
18+
go get -d example.net/[email protected]
19+
20+
! go list example.net/tools
21+
stderr '^package example.net/tools: build constraints exclude all Go files in .*[/\\]tools$'
22+
23+
go list -tags=tools -e -deps example.net/tools
24+
stdout '^example.net/cmd/tool$'
25+
stdout '^example.net/missing$'
26+
27+
go list -deps example.net/cmd/tool
28+
29+
! go list example.net/missing
30+
stderr '^no required module provides package example.net/missing; to add it:\n\tgo get example.net/missing$'
31+
32+
33+
# https://golang.org/issue/29268
34+
# 'go get' should fetch modules whose roots contain test-only packages, but
35+
# without the -t flag shouldn't error out if the test has missing dependencies.
36+
37+
go get -d example.net/[email protected]
38+
39+
# With the -t flag, the test dependencies must resolve successfully.
40+
! go get -d -t example.net/[email protected]
41+
stderr '^example.net/testonly tested by\n\texample.net/testonly\.test imports\n\texample.net/missing: cannot find module providing package example.net/missing$'
42+
43+
44+
# 'go get -d' should succeed for a module path that does not contain a package,
45+
# but fail for a non-package subdirectory of a module.
46+
47+
! go get -d example.net/missing/[email protected]
48+
stderr '^go get: module example.net/[email protected] found \(replaced by ./missing\), but does not contain package example.net/missing/subdir$'
49+
50+
go get -d example.net/[email protected]
51+
52+
53+
# Getting the subdirectory should continue to fail even if the corresponding
54+
# module is already present in the build list.
55+
56+
! go get -d example.net/missing/[email protected]
57+
stderr '^go get: module example.net/[email protected] found \(replaced by ./missing\), but does not contain package example.net/missing/subdir$'
58+
59+
60+
-- go.mod --
61+
module example.net/m
62+
63+
go 1.15
64+
65+
replace (
66+
example.net/tools v0.1.0 => ./tools
67+
example.net/cmd v0.1.0 => ./cmd
68+
example.net/testonly v0.1.0 => ./testonly
69+
example.net/missing v0.1.0 => ./missing
70+
)
71+
72+
-- tools/go.mod --
73+
module example.net/tools
74+
75+
go 1.15
76+
77+
// Requirements intentionally omitted.
78+
79+
-- tools/tools.go --
80+
// +build tools
81+
82+
package tools
83+
84+
import (
85+
_ "example.net/cmd/tool"
86+
_ "example.net/missing"
87+
)
88+
89+
-- cmd/go.mod --
90+
module example.net/cmd
91+
92+
go 1.16
93+
-- cmd/tool/tool.go --
94+
package main
95+
96+
func main() {}
97+
98+
-- testonly/go.mod --
99+
module example.net/testonly
100+
101+
go 1.15
102+
-- testonly/testonly_test.go --
103+
package testonly_test
104+
105+
import _ "example.net/missing"
106+
107+
func Test(t *testing.T) {}
108+
109+
-- missing/go.mod --
110+
module example.net/missing
111+
112+
go 1.15
113+
-- missing/README.txt --
114+
There are no Go source files here.
115+
-- missing/subdir/README.txt --
116+
There are no Go source files here either.

0 commit comments

Comments
 (0)