Skip to content

Commit 4b1d9c4

Browse files
author
Bryan C. Mills
committed
[release-branch.go1.13] cmd/go/internal/modload: annotate replacements in PackageNotInModuleError
Updates #34085 Fixes #34118 Change-Id: I3111f5997466ad33f51e80c71f5fb2ccebdcc6e4 Reviewed-on: https://go-review.googlesource.com/c/go/+/193617 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> (cherry picked from commit 8189a06) Reviewed-on: https://go-review.googlesource.com/c/go/+/197317
1 parent a462e9f commit 4b1d9c4

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

src/cmd/go/internal/modload/import_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var importTests = []struct {
2121
},
2222
{
2323
path: "golang.org/x/net",
24-
err: "module golang.org/x/net@.* found, but does not contain package golang.org/x/net",
24+
err: `module golang.org/x/net@.* found \(v0.0.0-.*\), but does not contain package golang.org/x/net`,
2525
},
2626
{
2727
path: "golang.org/x/text",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,11 @@ func (*mvsReqs) next(m module.Version) (module.Version, error) {
12051205
return module.Version{Path: m.Path, Version: "none"}, nil
12061206
}
12071207

1208+
// fetch downloads the given module (or its replacement)
1209+
// and returns its location.
1210+
//
1211+
// The isLocal return value reports whether the replacement,
1212+
// if any, is local to the filesystem.
12081213
func fetch(mod module.Version) (dir string, isLocal bool, err error) {
12091214
if mod == Target {
12101215
return ModRoot(), true, nil

src/cmd/go/internal/modload/query.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,10 @@ func QueryPattern(pattern, query string, allowed func(module.Version) bool) ([]Q
381381
r.Packages = match(r.Mod, root, isLocal)
382382
if len(r.Packages) == 0 {
383383
return r, &PackageNotInModuleError{
384-
Mod: r.Mod,
385-
Query: query,
386-
Pattern: pattern,
384+
Mod: r.Mod,
385+
Replacement: Replacement(r.Mod),
386+
Query: query,
387+
Pattern: pattern,
387388
}
388389
}
389390
return r, nil
@@ -536,21 +537,32 @@ func (e *NoMatchingVersionError) Error() string {
536537
// code for the versions it knows about, and thus did not have the opportunity
537538
// to return a non-400 status code to suppress fallback.
538539
type PackageNotInModuleError struct {
539-
Mod module.Version
540-
Query string
541-
Pattern string
540+
Mod module.Version
541+
Replacement module.Version
542+
Query string
543+
Pattern string
542544
}
543545

544546
func (e *PackageNotInModuleError) Error() string {
545547
found := ""
546-
if e.Query != e.Mod.Version {
548+
if r := e.Replacement; r.Path != "" {
549+
replacement := r.Path
550+
if r.Version != "" {
551+
replacement = fmt.Sprintf("%s@%s", r.Path, r.Version)
552+
}
553+
if e.Query == e.Mod.Version {
554+
found = fmt.Sprintf(" (replaced by %s)", replacement)
555+
} else {
556+
found = fmt.Sprintf(" (%s, replaced by %s)", e.Mod.Version, replacement)
557+
}
558+
} else if e.Query != e.Mod.Version {
547559
found = fmt.Sprintf(" (%s)", e.Mod.Version)
548560
}
549561

550562
if strings.Contains(e.Pattern, "...") {
551-
return fmt.Sprintf("module %s@%s%s found, but does not contain packages matching %s", e.Mod.Path, e.Query, found, e.Pattern)
563+
return fmt.Sprintf("module %s@%s found%s, but does not contain packages matching %s", e.Mod.Path, e.Query, found, e.Pattern)
552564
}
553-
return fmt.Sprintf("module %s@%s%s found, but does not contain package %s", e.Mod.Path, e.Query, found, e.Pattern)
565+
return fmt.Sprintf("module %s@%s found%s, but does not contain package %s", e.Mod.Path, e.Query, found, e.Pattern)
554566
}
555567

556568
// ModuleHasRootPackage returns whether module m contains a package m.Path.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ grep 'require rsc.io/quote' go.mod
1010

1111
cp go.mod.orig go.mod
1212
! go get -d rsc.io/quote/x...
13-
stderr 'go get rsc.io/quote/x...: module rsc.io/quote@upgrade \(v1.5.2\) found, but does not contain packages matching rsc.io/quote/x...'
13+
stderr 'go get rsc.io/quote/x...: module rsc.io/quote@upgrade found \(v1.5.2\), but does not contain packages matching rsc.io/quote/x...'
1414
! grep 'require rsc.io/quote' go.mod
1515

1616
! go get -d rsc.io/quote/x/...
17-
stderr 'go get rsc.io/quote/x/...: module rsc.io/quote@upgrade \(v1.5.2\) found, but does not contain packages matching rsc.io/quote/x/...'
17+
stderr 'go get rsc.io/quote/x/...: module rsc.io/quote@upgrade found \(v1.5.2\), but does not contain packages matching rsc.io/quote/x/...'
1818
! grep 'require rsc.io/quote' go.mod
1919

2020
# If a pattern matches no packages within a module, the module should not

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ grep 'not-rsc.io/quote/v3 v3.1.0' go.mod
3838
exec ./a5.exe
3939
stdout 'Concurrency is not parallelism.'
4040

41+
# Error messages for modules not found in replacements should
42+
# indicate the replacement module.
43+
cp go.mod.orig go.mod
44+
go mod edit -replace=rsc.io/quote/v3=./local/rsc.io/quote/v3
45+
! go get -d rsc.io/quote/v3/missing-package
46+
stderr 'module rsc.io/quote/v3@upgrade found \(v3.0.0, replaced by ./local/rsc.io/quote/v3\), but does not contain package'
47+
4148
-- go.mod --
4249
module quoter
4350

0 commit comments

Comments
 (0)