Skip to content

Commit e726e2a

Browse files
author
Bryan C. Mills
committed
cmd/go: suppress errors for 'go get' of module paths that are also constrained-out packages
Fixes #33526 Change-Id: Iedd2d6dbe440499bf074ac632513319a22f2d648 Reviewed-on: https://go-review.googlesource.com/c/go/+/297009 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 6b6ea32 commit e726e2a

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,23 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
369369
// directory.
370370
if !*getD && len(pkgPatterns) > 0 {
371371
work.BuildInit()
372-
pkgs := load.PackagesAndErrors(ctx, pkgPatterns)
372+
373+
var pkgs []*load.Package
374+
for _, pkg := range load.PackagesAndErrors(ctx, pkgPatterns) {
375+
if pkg.Error != nil {
376+
var noGo *load.NoGoError
377+
if errors.As(pkg.Error.Err, &noGo) {
378+
if m := modload.PackageModule(pkg.ImportPath); m.Path == pkg.ImportPath {
379+
// pkg is at the root of a module, and doesn't exist with the current
380+
// build tags. Probably the user just wanted to change the version of
381+
// that module — not also build the package — so suppress the error.
382+
// (See https://golang.org/issue/33526.)
383+
continue
384+
}
385+
}
386+
}
387+
pkgs = append(pkgs, pkg)
388+
}
373389
load.CheckPackageErrors(pkgs)
374390
work.InstallPackages(ctx, pkgPatterns, pkgs)
375391
// TODO(#40276): After Go 1.16, print a deprecation notice when building and
@@ -1453,6 +1469,7 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
14531469
LoadTests: *getT,
14541470
ResolveMissingImports: false,
14551471
AllowErrors: true,
1472+
SilenceNoGoErrors: true,
14561473
}
14571474
matches, pkgs := modload.LoadPackages(ctx, pkgOpts, pkgPatterns...)
14581475
for _, m := range matches {
@@ -1468,9 +1485,9 @@ func (r *resolver) checkPackagesAndRetractions(ctx context.Context, pkgPatterns
14681485
// associated with either the package or its test — ErrNoGo must
14691486
// indicate that none of those source files happen to apply in this
14701487
// configuration. If we are actually building the package (no -d
1471-
// flag), the compiler will report the problem; otherwise, assume that
1472-
// the user is going to build or test it in some other configuration
1473-
// and suppress the error.
1488+
// flag), we will report the problem then; otherwise, assume that the
1489+
// user is going to build or test this package in some other
1490+
// configuration and suppress the error.
14741491
continue
14751492
}
14761493

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

+25-11
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ type PackageOpts struct {
181181
// future version).
182182
SilenceMissingStdImports bool
183183

184+
// SilenceNoGoErrors indicates that LoadPackages should not print
185+
// imports.ErrNoGo errors.
186+
// This allows the caller to invoke LoadPackages (and report other errors)
187+
// without knowing whether the requested packages exist for the given tags.
188+
//
189+
// Note that if a requested package does not exist *at all*, it will fail
190+
// during module resolution and the error will not be suppressed.
191+
SilenceNoGoErrors bool
192+
184193
// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
185194
// patterns that did not match any packages.
186195
SilenceUnmatchedWarnings bool
@@ -290,6 +299,10 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
290299
// Report errors, if any.
291300
checkMultiplePaths()
292301
for _, pkg := range loaded.pkgs {
302+
if !pkg.isTest() {
303+
loadedPackages = append(loadedPackages, pkg.path)
304+
}
305+
293306
if pkg.err != nil {
294307
if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
295308
if importer := pkg.stack; importer != nil {
@@ -298,23 +311,24 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
298311
sumErr.importerIsTest = importer.testOf != nil
299312
}
300313
}
301-
silence := opts.SilenceErrors
314+
315+
if opts.SilenceErrors {
316+
continue
317+
}
302318
if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) &&
303319
stdErr.isStd && opts.SilenceMissingStdImports {
304-
silence = true
320+
continue
321+
}
322+
if opts.SilenceNoGoErrors && errors.Is(pkg.err, imports.ErrNoGo) {
323+
continue
305324
}
306325

307-
if !silence {
308-
if opts.AllowErrors {
309-
fmt.Fprintf(os.Stderr, "%s: %v\n", pkg.stackText(), pkg.err)
310-
} else {
311-
base.Errorf("%s: %v", pkg.stackText(), pkg.err)
312-
}
326+
if opts.AllowErrors {
327+
fmt.Fprintf(os.Stderr, "%s: %v\n", pkg.stackText(), pkg.err)
328+
} else {
329+
base.Errorf("%s: %v", pkg.stackText(), pkg.err)
313330
}
314331
}
315-
if !pkg.isTest() {
316-
loadedPackages = append(loadedPackages, pkg.path)
317-
}
318332
}
319333
if !opts.SilenceErrors {
320334
// Also list errors in matching patterns (such as directory permission

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

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go mod edit -droprequire example.net/tools
1616
# error out if dependencies of tag-guarded files are missing.
1717

1818
go get -d example.net/[email protected]
19+
! stderr 'no Go source files'
1920

2021
! go list example.net/tools
2122
stderr '^package example.net/tools: build constraints exclude all Go files in .*[/\\]tools$'
@@ -30,6 +31,19 @@ go list -deps example.net/cmd/tool
3031
stderr '^no required module provides package example.net/missing; to add it:\n\tgo get example.net/missing$'
3132

3233

34+
# https://golang.org/issue/33526: 'go get' without '-d' should succeed
35+
# for a module whose root is a constrained-out package.
36+
#
37+
# Ideally it should silently succeed, but today it logs the "no Go source files"
38+
# error and succeeds anyway.
39+
40+
go get example.net/[email protected]
41+
! stderr .
42+
43+
! go build example.net/tools
44+
stderr '^package example.net/tools: build constraints exclude all Go files in .*[/\\]tools$'
45+
46+
3347
# https://golang.org/issue/29268
3448
# 'go get' should fetch modules whose roots contain test-only packages, but
3549
# without the -t flag shouldn't error out if the test has missing dependencies.

0 commit comments

Comments
 (0)