Commit f37ca81c authored by Russ Cox's avatar Russ Cox

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/125657Reviewed-by: 's avatarBryan C. Mills <bcmills@google.com>
parent 0cb6b55f
...@@ -496,7 +496,7 @@ func modPrintJSON() { ...@@ -496,7 +496,7 @@ func modPrintJSON() {
// modPrintGraph prints the -graph output. // modPrintGraph prints the -graph output.
func modPrintGraph() { func modPrintGraph() {
reqs := modload.Reqs() reqs := modload.MinReqs()
format := func(m module.Version) string { format := func(m module.Version) string {
if m.Version == "" { if m.Version == "" {
......
...@@ -91,6 +91,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic { ...@@ -91,6 +91,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
Version: m.Version, Version: m.Version,
Main: true, Main: true,
Dir: ModRoot, Dir: ModRoot,
GoMod: filepath.Join(ModRoot, "go.mod"),
} }
} }
...@@ -114,7 +115,15 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic { ...@@ -114,7 +115,15 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
m.Version = q.Version m.Version = q.Version
m.Time = &q.Time m.Time = &q.Time
} }
dir, err := modfetch.DownloadDir(module.Version{Path: m.Path, Version: m.Version})
mod := module.Version{Path: m.Path, Version: m.Version}
gomod, err := modfetch.CachePath(mod, "mod")
if err == nil {
if info, err := os.Stat(gomod); err == nil && info.Mode().IsRegular() {
m.GoMod = gomod
}
}
dir, err := modfetch.DownloadDir(mod)
if err == nil { if err == nil {
if info, err := os.Stat(dir); err == nil && info.IsDir() { if info, err := os.Stat(dir); err == nil && info.IsDir() {
m.Dir = dir m.Dir = dir
...@@ -142,6 +151,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic { ...@@ -142,6 +151,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
} }
complete(info.Replace) complete(info.Replace)
info.Dir = info.Replace.Dir info.Dir = info.Replace.Dir
info.GoMod = filepath.Join(info.Dir, "go.mod")
info.Error = nil // ignore error loading original module version (it has been replaced) info.Error = nil // ignore error loading original module version (it has been replaced)
} }
......
...@@ -474,6 +474,22 @@ func AllowWriteGoMod() { ...@@ -474,6 +474,22 @@ func AllowWriteGoMod() {
allowWriteGoMod = true allowWriteGoMod = true
} }
// MinReqs returns a Reqs with minimal dependencies of Target,
// as will be written to go.mod.
func MinReqs() mvs.Reqs {
var direct []string
for _, m := range buildList[1:] {
if loaded.direct[m.Path] {
direct = append(direct, m.Path)
}
}
min, err := mvs.Req(Target, buildList, direct, Reqs())
if err != nil {
base.Fatalf("go: %v", err)
}
return &mvsReqs{buildList: append([]module.Version{Target}, min...)}
}
// WriteGoMod writes the current build list back to go.mod. // WriteGoMod writes the current build list back to go.mod.
func WriteGoMod() { func WriteGoMod() {
if !allowWriteGoMod { if !allowWriteGoMod {
...@@ -483,13 +499,8 @@ func WriteGoMod() { ...@@ -483,13 +499,8 @@ func WriteGoMod() {
modfetch.WriteGoSum() modfetch.WriteGoSum()
if loaded != nil { if loaded != nil {
var direct []string reqs := MinReqs()
for _, m := range buildList[1:] { min, err := reqs.Required(Target)
if loaded.direct[m.Path] {
direct = append(direct, m.Path)
}
}
min, err := mvs.Req(Target, buildList, direct, Reqs())
if err != nil { if err != nil {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }
......
env GO111MODULE=on
go mod -graph
stdout '^m rsc.io/quote@v1.5.2$'
stdout '^rsc.io/quote@v1.5.2 rsc.io/sampler@v1.3.0$'
! stdout '^m rsc.io/sampler@v1.3.0$'
-- go.mod --
module m
require rsc.io/quote v1.5.2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment