Commit 0f834bb7 authored by Jay Conrod's avatar Jay Conrod

cmd/go: in 'go list -m', print effective go.mod file

When the -modfile flag is in use (either explicitly or from GOFLAGS),
'go list -m' will now print the effective go.mod file for the main
module in the GoMod field in -f or -json output.

Fixes #36220
Updates #34506

Change-Id: I89c2ee40f20e07854bb37c6e4e13eeea0cce7b0d
Reviewed-on: https://go-review.googlesource.com/c/go/+/212100
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent a197c7bb
...@@ -907,7 +907,7 @@ ...@@ -907,7 +907,7 @@
// Main bool // is this the main module? // Main bool // is this the main module?
// Indirect bool // is this module only an indirect dependency of main module? // Indirect bool // is this module only an indirect dependency of main module?
// Dir string // directory holding files for this module, if any // Dir string // directory holding files for this module, if any
// GoMod string // path to go.mod file for this module, if any // GoMod string // path to go.mod file used when loading this module, if any
// GoVersion string // go version used in module // GoVersion string // go version used in module
// Error *ModuleError // error loading module // Error *ModuleError // error loading module
// } // }
...@@ -916,6 +916,9 @@ ...@@ -916,6 +916,9 @@
// Err string // the error itself // Err string // the error itself
// } // }
// //
// The file GoMod refers to may be outside the module directory if the
// module is in the module cache or if the -modfile flag is used.
//
// The default output is to print the module path and then // The default output is to print the module path and then
// information about the version and replacement if any. // information about the version and replacement if any.
// For example, 'go list -m all' might print: // For example, 'go list -m all' might print:
......
...@@ -211,7 +211,7 @@ applied to a Go struct, but now a Module struct: ...@@ -211,7 +211,7 @@ applied to a Go struct, but now a Module struct:
Main bool // is this the main module? Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module? Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any GoMod string // path to go.mod file used when loading this module, if any
GoVersion string // go version used in module GoVersion string // go version used in module
Error *ModuleError // error loading module Error *ModuleError // error loading module
} }
...@@ -220,6 +220,9 @@ applied to a Go struct, but now a Module struct: ...@@ -220,6 +220,9 @@ applied to a Go struct, but now a Module struct:
Err string // the error itself Err string // the error itself
} }
The file GoMod refers to may be outside the module directory if the
module is in the module cache or if the -modfile flag is used.
The default output is to print the module path and then The default output is to print the module path and then
information about the version and replacement if any. information about the version and replacement if any.
For example, 'go list -m all' might print: For example, 'go list -m all' might print:
......
...@@ -112,7 +112,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic { ...@@ -112,7 +112,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
} }
if HasModRoot() { if HasModRoot() {
info.Dir = ModRoot() info.Dir = ModRoot()
info.GoMod = filepath.Join(info.Dir, "go.mod") info.GoMod = ModFilePath()
if modFile.Go != nil { if modFile.Go != nil {
info.GoVersion = modFile.Go.Version info.GoVersion = modFile.Go.Version
} }
......
...@@ -11,6 +11,15 @@ cp go.sum go.sum.orig ...@@ -11,6 +11,15 @@ cp go.sum go.sum.orig
go mod init example.com/m go mod init example.com/m
grep example.com/m go.alt.mod grep example.com/m go.alt.mod
# 'go env GOMOD' should print the path to the real file.
# 'go env' does not recognize the '-modfile' flag.
go env GOMOD
stdout '^\$WORK[/\\]gopath[/\\]src[/\\]go.mod$'
# 'go list -m' should print the effective go.mod file as GoMod though.
go list -m -f '{{.GoMod}}'
stdout '^go.alt.mod$'
# go mod edit should operate on the alternate file # go mod edit should operate on the alternate file
go mod edit -require rsc.io/quote@v1.5.2 go mod edit -require rsc.io/quote@v1.5.2
grep rsc.io/quote go.alt.mod grep rsc.io/quote go.alt.mod
......
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