Skip to content

Commit f37ca81

Browse files
committed
cmd/go: fix spurious edges in mod -graph output
The mod -graph output was showing every dependency as an edge from the main module, instead of showing only the things that are listed in go.mod. Fixes #26489. Change-Id: I248fedb1fc9225e2a7a9ddc2f4a84520b3a96138 Reviewed-on: https://go-review.googlesource.com/125657 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 0cb6b55 commit f37ca81

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

src/cmd/go/internal/modcmd/mod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func modPrintJSON() {
496496

497497
// modPrintGraph prints the -graph output.
498498
func modPrintGraph() {
499-
reqs := modload.Reqs()
499+
reqs := modload.MinReqs()
500500

501501
format := func(m module.Version) string {
502502
if m.Version == "" {

src/cmd/go/internal/modload/build.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
9191
Version: m.Version,
9292
Main: true,
9393
Dir: ModRoot,
94+
GoMod: filepath.Join(ModRoot, "go.mod"),
9495
}
9596
}
9697

@@ -114,7 +115,15 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
114115
m.Version = q.Version
115116
m.Time = &q.Time
116117
}
117-
dir, err := modfetch.DownloadDir(module.Version{Path: m.Path, Version: m.Version})
118+
119+
mod := module.Version{Path: m.Path, Version: m.Version}
120+
gomod, err := modfetch.CachePath(mod, "mod")
121+
if err == nil {
122+
if info, err := os.Stat(gomod); err == nil && info.Mode().IsRegular() {
123+
m.GoMod = gomod
124+
}
125+
}
126+
dir, err := modfetch.DownloadDir(mod)
118127
if err == nil {
119128
if info, err := os.Stat(dir); err == nil && info.IsDir() {
120129
m.Dir = dir
@@ -142,6 +151,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
142151
}
143152
complete(info.Replace)
144153
info.Dir = info.Replace.Dir
154+
info.GoMod = filepath.Join(info.Dir, "go.mod")
145155
info.Error = nil // ignore error loading original module version (it has been replaced)
146156
}
147157

src/cmd/go/internal/modload/init.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,22 @@ func AllowWriteGoMod() {
474474
allowWriteGoMod = true
475475
}
476476

477+
// MinReqs returns a Reqs with minimal dependencies of Target,
478+
// as will be written to go.mod.
479+
func MinReqs() mvs.Reqs {
480+
var direct []string
481+
for _, m := range buildList[1:] {
482+
if loaded.direct[m.Path] {
483+
direct = append(direct, m.Path)
484+
}
485+
}
486+
min, err := mvs.Req(Target, buildList, direct, Reqs())
487+
if err != nil {
488+
base.Fatalf("go: %v", err)
489+
}
490+
return &mvsReqs{buildList: append([]module.Version{Target}, min...)}
491+
}
492+
477493
// WriteGoMod writes the current build list back to go.mod.
478494
func WriteGoMod() {
479495
if !allowWriteGoMod {
@@ -483,13 +499,8 @@ func WriteGoMod() {
483499
modfetch.WriteGoSum()
484500

485501
if loaded != nil {
486-
var direct []string
487-
for _, m := range buildList[1:] {
488-
if loaded.direct[m.Path] {
489-
direct = append(direct, m.Path)
490-
}
491-
}
492-
min, err := mvs.Req(Target, buildList, direct, Reqs())
502+
reqs := MinReqs()
503+
min, err := reqs.Required(Target)
493504
if err != nil {
494505
base.Fatalf("go: %v", err)
495506
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
env GO111MODULE=on
2+
3+
go mod -graph
4+
stdout '^m rsc.io/[email protected]$'
5+
stdout '^rsc.io/[email protected] rsc.io/[email protected]$'
6+
! stdout '^m rsc.io/[email protected]$'
7+
8+
-- go.mod --
9+
module m
10+
require rsc.io/quote v1.5.2

0 commit comments

Comments
 (0)