Skip to content

proposal: cmd/go: "go tool" propagate -modfile via env GOTOOLMOD #79449

Description

@dolmen

Proposal Details

To help building dev tools which are go tool-aware, especially with a separate go.mod dedicated to manage tools dependencies (ex: go tool -modfile=tools.mod), I propose to extend go tool to propagate its -modfile flag value to the child processes via an environment variable:

  • When go tool runs with -modfile, expose the flag value as variable GOTOOLMOD (similar to the existing GOMOD) when running (NOT building) the tool
  • When go tool runs without -modfile, use the value of GOTOOLMOD environment variable, if available, as the first fallback value for locating the go.mod to use for resolving (and building) tools binaries. If building a tool is necessary, clear GOTOOLMOD from the environment before calling go build. Keep GOTOOLMOD in the environment when executing the tool.

Use cases

go tool called from a tool

A tool called via go tool might want to call another tool, also via go tool.

A tool called via go tool -modfile=tools.mod might want to call another tool, also via go tool, but with the same go.mod file. For that case, the default value for -modfile for go tool would be set from the GOTOOLMOD value. The first tool would just execute go tool and would not have to specially care about -modfile.

Example: a go generate command that calls a tool local to the module which then calls an external tool.

A tool that manages tools

A tool that manages tools need to be aware that it is called via go tool (see #78673) but also which go.mod to use/tweak. While it could have its own -modfile flag, it would be simpler to make it aware implicitly of the go.mod via an environment variable GOTOOLMOD.

Related proposals

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions