Skip to content

Commit 7e4fb8b

Browse files
author
Jay Conrod
committed
cmd/go: make 'go mod why -m' work in inconsistent, pruned module
'go mod why -m' works by listing modules matching command line arguments, then loading "all" packages and finding which of the listed modules provide packages imported by the main module. If go.mod is inconsistent (that is, a requirement has a lower version than MVS would select when the module graph is loaded) and pruned (that is, the module graph is only loaded when necessary), then modload.ListModules may return modules with different versions than would be selected in modload.LoadPackages. 'go mod why -m' was too strict about this, mapping module paths and versions to packages. With this fix, it maps module paths without versions to packages. Fixes #48613 Change-Id: I836c46289bb647d6c46ec65e7589531da532d5e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/352115 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent cfd0868 commit 7e4fb8b

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

src/cmd/go/internal/modcmd/why.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"cmd/go/internal/base"
1313
"cmd/go/internal/imports"
1414
"cmd/go/internal/modload"
15-
16-
"golang.org/x/mod/module"
1715
)
1816

1917
var cmdWhy = &base.Command{
@@ -90,19 +88,19 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
9088
base.Fatalf("go: %v", err)
9189
}
9290

93-
byModule := make(map[module.Version][]string)
91+
byModule := make(map[string][]string)
9492
_, pkgs := modload.LoadPackages(ctx, loadOpts, "all")
9593
for _, path := range pkgs {
9694
m := modload.PackageModule(path)
9795
if m.Path != "" {
98-
byModule[m] = append(byModule[m], path)
96+
byModule[m.Path] = append(byModule[m.Path], path)
9997
}
10098
}
10199
sep := ""
102100
for _, m := range mods {
103101
best := ""
104102
bestDepth := 1000000000
105-
for _, path := range byModule[module.Version{Path: m.Path, Version: m.Version}] {
103+
for _, path := range byModule[m.Path] {
106104
d := modload.WhyDepth(path)
107105
if d > 0 && d < bestDepth {
108106
best = path

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ go mod why rsc.io/sampler
2424
cmp stdout why.want
2525
cmp go.mod go.mod.edit
2626

27-
# TODO(#48613): 'go mod why -m' incorrectly reports sampler is not needed.
2827
go mod why -m rsc.io/sampler
29-
cmp stdout why-broken.want
28+
cmp stdout why.want
3029
cmp go.mod go.mod.edit
3130

3231
cp go.mod.orig go.mod
@@ -91,6 +90,3 @@ rsc.io/[email protected] golang.org/x/[email protected]
9190
m
9291
rsc.io/quote
9392
rsc.io/sampler
94-
-- why-broken.want --
95-
# rsc.io/sampler
96-
(main module does not need module rsc.io/sampler)

0 commit comments

Comments
 (0)