[go] cmd/go/internal/modcmd/graph: add option for selected versions graph

6 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
May 28, 2023, 9:06:09 AM5/28/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

cmd/go/internal/modcmd/graph: add option for selected versions graph

The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
`go mod graph -selectedVersions`

Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
GitHub-Last-Rev: 2b0928e2a08d6945a143396a1e3f4ad5c511bffd
GitHub-Pull-Request: golang/go#60478
---
M src/cmd/go/alldocs.go
M src/cmd/go/internal/modcmd/graph.go
2 files changed, 63 insertions(+), 22 deletions(-)

diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go
index 4314d77..ba93847 100644
--- a/src/cmd/go/alldocs.go
+++ b/src/cmd/go/alldocs.go
@@ -1275,6 +1275,9 @@
//
// The -x flag causes graph to print the commands graph executes.
//
+// The -selectedVersions flag cause graph to contains only packages with versions that were selected
+// by the minimal version selected algorithm.
+//
// See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'.
//
// # Initialize new module in current directory
diff --git a/src/cmd/go/internal/modcmd/graph.go b/src/cmd/go/internal/modcmd/graph.go
index 555604d..c499935 100644
--- a/src/cmd/go/internal/modcmd/graph.go
+++ b/src/cmd/go/internal/modcmd/graph.go
@@ -8,18 +8,17 @@

import (
"bufio"
- "context"
- "os"
-
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/modload"
+ "context"
+ "os"

"golang.org/x/mod/module"
)

var cmdGraph = &base.Command{
- UsageLine: "go mod graph [-go=version] [-x]",
+ UsageLine: "go mod graph [-go=version] [-x] [-selectedVersions]",
Short: "print module requirement graph",
Long: `
Graph prints the module requirement graph (with replacements applied)
@@ -33,22 +32,64 @@

The -x flag causes graph to print the commands graph executes.

+The -selectedVersions flag cause graph to contains only packages with versions that were selected
+by the minimal version selected algorithm.
+
See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'.
`,
Run: runGraph,
}

var (
- graphGo goVersionFlag
+ graphGo goVersionFlag
+ selectedVersions bool
)

func init() {
cmdGraph.Flag.Var(&graphGo, "go", "")
cmdGraph.Flag.BoolVar(&cfg.BuildX, "x", false, "")
+ cmdGraph.Flag.BoolVar(&selectedVersions, "selectedVersions", false, "")
base.AddChdirFlag(&cmdGraph.Flag)
base.AddModCommonFlags(&cmdGraph.Flag)
}

+func walkOnSelectedVersions(version module.Version, walkNodes map[string]bool, w *bufio.Writer, mg *modload.ModuleGraph) {
+ selectedVersion := mg.Selected(version.Path)
+
+ // Walk only selected versions
+ if selectedVersion != version.Version {
+ return
+ }
+
+ walkNodes[version.String()] = true
+
+ reqs, ok := mg.RequiredBy(version)
+ if ok {
+ // print the dependencies of the selected package
+ for _, req := range reqs {
+ format(version, w)
+ w.WriteByte(' ')
+ format(req, w)
+ w.WriteByte('\n')
+ }
+
+ // iterate on all the dependencies of the selected package
+ for _, req := range reqs {
+ if _, exists := walkNodes[req.String()]; !exists {
+ walkOnSelectedVersions(req, walkNodes, w, mg)
+ }
+ }
+ }
+}
+
+func format(m module.Version, w *bufio.Writer) {
+ w.WriteString(m.Path)
+ if m.Version != "" {
+ w.WriteString("@")
+ w.WriteString(m.Version)
+ }
+}
+
func runGraph(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()

@@ -58,25 +99,22 @@
modload.ForceUseModules = true
modload.RootMode = modload.NeedRoot
mg := modload.LoadModGraph(ctx, graphGo.String())
-
w := bufio.NewWriter(os.Stdout)
defer w.Flush()

- format := func(m module.Version) {
- w.WriteString(m.Path)
- if m.Version != "" {
- w.WriteString("@")
- w.WriteString(m.Version)
- }
+ if selectedVersions {
+ rootNode := mg.BuildList()[0]
+ walkNodes := make(map[string]bool)
+ walkOnSelectedVersions(rootNode, walkNodes, w, mg)
+ } else {
+ mg.WalkBreadthFirst(func(m module.Version) {
+ reqs, _ := mg.RequiredBy(m)
+ for _, r := range reqs {
+ format(m, w)
+ w.WriteByte(' ')
+ format(r, w)
+ w.WriteByte('\n')
+ }
+ })
}
-
- mg.WalkBreadthFirst(func(m module.Version) {
- reqs, _ := mg.RequiredBy(m)
- for _, r := range reqs {
- format(m)
- w.WriteByte(' ')
- format(r)
- w.WriteByte('\n')
- }
- })
}

To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
Gerrit-Change-Number: 498995
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>

Gopher Robot (Gerrit)

unread,
May 28, 2023, 9:06:58 AM5/28/23
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

View Change

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Sun, 28 May 2023 13:06:55 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No

    Gerrit Bot (Gerrit)

    unread,
    May 29, 2023, 4:59:05 AM5/29/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Michael Matloob.

    Gerrit Bot uploaded patch set #2 to this change.

    View Change

    cmd/go/internal/modcmd/graph: add option for selected versions graph

    The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

    This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

    To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
    `go mod graph -selectedVersions`

    Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    GitHub-Last-Rev: bdcbb2d25f60baaf616a1eca0dcd68a5725e1a29

    GitHub-Pull-Request: golang/go#60478
    ---
    M src/cmd/go/alldocs.go
    M src/cmd/go/internal/modcmd/graph.go
    2 files changed, 81 insertions(+), 22 deletions(-)

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>

    Gerrit Bot (Gerrit)

    unread,
    May 29, 2023, 5:05:28 AM5/29/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Michael Matloob.

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    cmd/go/internal/modcmd/graph: add option for selected versions graph

    The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

    This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

    To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
    `go mod graph -selectedVersions`

    Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    GitHub-Last-Rev: f949d9b7adeb4b469d0b1dee3ba4209c740db793

    GitHub-Pull-Request: golang/go#60478
    ---
    M src/cmd/go/alldocs.go
    M src/cmd/go/internal/modcmd/graph.go
    M src/run.bash
    3 files changed, 82 insertions(+), 22 deletions(-)

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 3

    Gerrit Bot (Gerrit)

    unread,
    May 29, 2023, 5:11:39 AM5/29/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Michael Matloob.

    Gerrit Bot uploaded patch set #4 to this change.

    View Change

    cmd/go/internal/modcmd/graph: add option for selected versions graph

    The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

    This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

    To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
    `go mod graph -selectedVersions`

    Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    GitHub-Last-Rev: 1cfa3b487872c9ad971ad10225d63d37ebf375e9

    GitHub-Pull-Request: golang/go#60478
    ---
    M src/cmd/go/alldocs.go
    M src/cmd/go/internal/modcmd/graph.go
    2 files changed, 81 insertions(+), 22 deletions(-)

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 4

    Gerrit Bot (Gerrit)

    unread,
    May 29, 2023, 5:17:39 AM5/29/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Michael Matloob.

    Gerrit Bot uploaded patch set #5 to this change.

    View Change

    cmd/go/internal/modcmd/graph: add option for selected versions graph

    The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

    This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

    To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
    `go mod graph -selectedVersions`

    Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    GitHub-Last-Rev: 3417702a8cabc2e0cf37a44944a630d6825266c4

    GitHub-Pull-Request: golang/go#60478
    ---
    M src/cmd/go/alldocs.go
    M src/cmd/go/internal/modcmd/graph.go
    2 files changed, 74 insertions(+), 22 deletions(-)

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 5

    Gerrit Bot (Gerrit)

    unread,
    Jun 4, 2023, 5:33:58 AM6/4/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Michael Matloob.

    Gerrit Bot uploaded patch set #6 to this change.

    View Change

    cmd/go/internal/modcmd/graph: add option for selected versions graph

    The current issue is that when using `go mod graph`, it generates packages that are not actually selected by the [Minimal version selection (MVS)](https://go.dev/ref/mod#minimal-version-selection) process. As a result, it creates a significantly large graph with packages that are not in use or downloaded.

    This pull request aims to improve the graph's accuracy by cleaning it up and retaining only the packages with versions that were chosen by MVS. This issue primarily occurs in 'go.mod' files with a Go version of 1.16 and below.

    To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:
    `go mod graph -selectedVersions`

    Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    GitHub-Last-Rev: 8eab4c14812468ca0f981e9c71329406725e8958

    GitHub-Pull-Request: golang/go#60478
    ---
    M src/cmd/go/alldocs.go
    M src/cmd/go/internal/modcmd/graph.go
    2 files changed, 74 insertions(+), 22 deletions(-)

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 6

    Bryan Mills (Gerrit)

    unread,
    Jun 26, 2023, 1:44:18 PM6/26/23
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Michael Matloob, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Michael Matloob.

    Patch set 6:Hold +1

    View Change

    2 comments:

    • Commit Message:

      • Patch Set #6, Line 13: To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:

        API changes — including significant additions of command-line flags — need to go through the Go proposal process:
        https://go.dev/s/proposal

    • File src/cmd/go/internal/modcmd/graph.go:

      • Patch Set #6, Line 37:

        The -selectedVersions flag cause graph to contains only packages with versions that were selected 


      • by the minimal version selected algorithm.

      • This will not produce a coherent graph in general. If any dependency does not support [graph pruning](https://go.dev/ref/mod#graph-pruning), the path from the main module to the selected version may in general include arbitrarily many older-than-selected module versions.

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 6
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Mon, 26 Jun 2023 17:44:13 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes

    אדם ורסנו (Gerrit)

    unread,
    Jul 3, 2023, 7:44:44 AM7/3/23
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Michael Matloob, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills.

    View Change

    3 comments:

    • Commit Message:

      • Patch Set #6, Line 13: To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:

      • API changes — including significant additions of command-line flags — need to go through the Go prop […]

        Sure I'll do it.

      • Patch Set #6, Line 13: To enable this feature, you can use the '-selectedVersions' flag with the 'go mod graph' command. For example:

      • API changes — including significant additions of command-line flags — need to go through the Go prop […]

        Sure I'll do it thanks

    • File src/cmd/go/internal/modcmd/graph.go:

      • Patch Set #6, Line 37:

        The -selectedVersions flag cause graph to contains only packages with versions that were selected 
        by the minimal version selected algorithm.

      • This will not produce a coherent graph in general. […]

        I use an algorithm that filters packages based on two conditions:
        1. They must be selected using ModuleGraph.Selected.
        2. The main module must have a path to them.

        Are you asserting that if there are dependencies in Go versions 1.16 and earlier (not supporting graph pruning), the resulting graph may not be coherent?
        Can you please clarify? maybe give an example?
        Thanks

    To view, visit change 498995. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibe4498ad34965e6378682e0d14a3f816855526ee
    Gerrit-Change-Number: 498995
    Gerrit-PatchSet: 6
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-CC: אדם ורסנו <adamv...@gmail.com>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Comment-Date: Mon, 03 Jul 2023 07:02:48 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Bryan Mills <bcm...@google.com>
    Reply all
    Reply to author
    Forward
    0 new messages