Skip to content

Commit 0f834bb

Browse files
author
Jay Conrod
committed
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: Bryan C. Mills <bcmills@google.com>
1 parent a197c7b commit 0f834bb

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/cmd/go/alldocs.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/list/list.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ applied to a Go struct, but now a Module struct:
211211
Main bool // is this the main module?
212212
Indirect bool // is this module only an indirect dependency of main module?
213213
Dir string // directory holding files for this module, if any
214-
GoMod string // path to go.mod file for this module, if any
214+
GoMod string // path to go.mod file used when loading this module, if any
215215
GoVersion string // go version used in module
216216
Error *ModuleError // error loading module
217217
}
@@ -220,6 +220,9 @@ applied to a Go struct, but now a Module struct:
220220
Err string // the error itself
221221
}
222222
223+
The file GoMod refers to may be outside the module directory if the
224+
module is in the module cache or if the -modfile flag is used.
225+
223226
The default output is to print the module path and then
224227
information about the version and replacement if any.
225228
For example, 'go list -m all' might print:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
112112
}
113113
if HasModRoot() {
114114
info.Dir = ModRoot()
115-
info.GoMod = filepath.Join(info.Dir, "go.mod")
115+
info.GoMod = ModFilePath()
116116
if modFile.Go != nil {
117117
info.GoVersion = modFile.Go.Version
118118
}

src/cmd/go/testdata/script/modfile_flag.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ cp go.sum go.sum.orig
1111
go mod init example.com/m
1212
grep example.com/m go.alt.mod
1313

14+
# 'go env GOMOD' should print the path to the real file.
15+
# 'go env' does not recognize the '-modfile' flag.
16+
go env GOMOD
17+
stdout '^\$WORK[/\\]gopath[/\\]src[/\\]go.mod$'
18+
19+
# 'go list -m' should print the effective go.mod file as GoMod though.
20+
go list -m -f '{{.GoMod}}'
21+
stdout '^go.alt.mod$'
22+
1423
# go mod edit should operate on the alternate file
1524
go mod edit -require rsc.io/quote@v1.5.2
1625
grep rsc.io/quote go.alt.mod

0 commit comments

Comments
 (0)