Skip to content

cmd/go: 'go list' should set the 'GoMod' field to reflect -modfile #36220

@bcmills

Description

@bcmills

The -modfile flag added for #34506 allows users to substitute an arbitrary file in place of the go.mod file found at the module root. We (@jayconrod, @matloob, and I) were talking through the implications of that this morning, and realized that that has a very subtle interaction with go env GOMOD, go list, and tools in general.

With Go 1.13, tools may execute either go env GOMOD go list -m -f '{{.GoMod}}' to discover the location of the main module's go.mod file, and (if in a module) may execute dirname $(go env GOMOD) in order to locate the module root.

The go env command does not support the -modfile flag, so if someone sets, say, GOFLAGS=-modfile=/tmp/foo.mod, then go env GOMOD will continue to report the go.mod file found in the module root, rather than the one in use. That seems fine.

However, go list does support the -modfile flag, so it should report the replacement go.mod file in the GoMod field. Today it does not.

(Tools that need to locate the directory containing the main module should already be using the Dir field from go list, rather than the GoMod field.)

Today I see:

~/go/src$ GOFLAGS=-modfile=/tmp/foo.mod go env GOMOD
/usr/local/google/home/bcmills/go/src/go.mod

~/go/src$ GOFLAGS=-modfile=/tmp/foo.mod go list -m -f '{{.GoMod}}'
/usr/local/google/home/bcmills/go/src/go.mod

However, that should probably instead be:

~/go/src$ GOFLAGS=-modfile=/tmp/foo.mod go env GOMOD
/usr/local/google/home/bcmills/go/src/go.mod

~/go/src$ GOFLAGS=-modfile=/tmp/foo.mod go list -m -f '{{.GoMod}}'
/tmp/foo.mod

CC @heschik @dmitshur (in case this affects goimports and/or godoc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions