cmd/go: enable -x in go mod graph

Updates #35849

Change-Id: Ifa18e448c0d436c18d7204ac755cd36bc28cd612
Reviewed-on: https://go-review.googlesource.com/c/go/+/448935
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
ianwoolf 2022-11-09 16:52:41 +08:00 committed by Gopher Robot
parent 86bbcc6a75
commit fcecf3e1fa
4 changed files with 14 additions and 3 deletions

View File

@ -1248,7 +1248,7 @@
// //
// Usage: // Usage:
// //
// go mod graph [-go=version] // go mod graph [-go=version] [-x]
// //
// Graph prints the module requirement graph (with replacements applied) // Graph prints the module requirement graph (with replacements applied)
// in text form. Each line in the output has two space-separated fields: a module // in text form. Each line in the output has two space-separated fields: a module
@ -1259,6 +1259,8 @@
// given Go version, instead of the version indicated by the 'go' directive // given Go version, instead of the version indicated by the 'go' directive
// in the go.mod file. // in the go.mod file.
// //
// The -x flag causes graph to print the commands graph executes.
//
// See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'. // See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'.
// //
// # Initialize new module in current directory // # Initialize new module in current directory

View File

@ -12,13 +12,14 @@ import (
"os" "os"
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/modload" "cmd/go/internal/modload"
"golang.org/x/mod/module" "golang.org/x/mod/module"
) )
var cmdGraph = &base.Command{ var cmdGraph = &base.Command{
UsageLine: "go mod graph [-go=version]", UsageLine: "go mod graph [-go=version] [-x]",
Short: "print module requirement graph", Short: "print module requirement graph",
Long: ` Long: `
Graph prints the module requirement graph (with replacements applied) Graph prints the module requirement graph (with replacements applied)
@ -30,6 +31,8 @@ The -go flag causes graph to report the module graph as loaded by the
given Go version, instead of the version indicated by the 'go' directive given Go version, instead of the version indicated by the 'go' directive
in the go.mod file. in the go.mod file.
The -x flag causes graph to print the commands graph executes.
See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'. See https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'.
`, `,
Run: runGraph, Run: runGraph,
@ -41,6 +44,7 @@ var (
func init() { func init() {
cmdGraph.Flag.Var(&graphGo, "go", "") cmdGraph.Flag.Var(&graphGo, "go", "")
cmdGraph.Flag.BoolVar(&cfg.BuildX, "x", false, "")
base.AddChdirFlag(&cmdGraph.Flag) base.AddChdirFlag(&cmdGraph.Flag)
base.AddModCommonFlags(&cmdGraph.Flag) base.AddModCommonFlags(&cmdGraph.Flag)
} }

View File

@ -4,6 +4,11 @@ go mod graph
stdout '^m rsc.io/quote@v1.5.2$' stdout '^m rsc.io/quote@v1.5.2$'
stdout '^rsc.io/quote@v1.5.2 rsc.io/sampler@v1.3.0$' stdout '^rsc.io/quote@v1.5.2 rsc.io/sampler@v1.3.0$'
! stdout '^m rsc.io/sampler@v1.3.0$' ! stdout '^m rsc.io/sampler@v1.3.0$'
! stderr 'get '$GOPROXY
rm $GOPATH/pkg/mod/cache/download/rsc.io/quote
go mod graph -x
stderr 'get '$GOPROXY
-- go.mod -- -- go.mod --
module m module m

View File

@ -57,7 +57,7 @@ stdout '^example.net/requireincompatible@v0.1.0 example.com/retract/incompatible
# Unsupported go versions should be rejected, since we don't know # Unsupported go versions should be rejected, since we don't know
# what versions they would report. # what versions they would report.
! go mod graph -go=1.99999999999 ! go mod graph -go=1.99999999999
stderr '^invalid value "1\.99999999999" for flag -go: maximum supported Go version is '$goversion'\nusage: go mod graph \[-go=version\]\nRun ''go help mod graph'' for details.$' stderr '^invalid value "1\.99999999999" for flag -go: maximum supported Go version is '$goversion'\nusage: go mod graph \[-go=version\] \[-x\]\nRun ''go help mod graph'' for details.$'
-- go.mod -- -- go.mod --