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
Proposal Details
To help building dev tools which are
go tool-aware, especially with a separatego.moddedicated to manage tools dependencies (ex:go tool -modfile=tools.mod), I propose to extendgo toolto propagate its-modfileflag value to the child processes via an environment variable:go toolruns with-modfile, expose the flag value as variableGOTOOLMOD(similar to the existingGOMOD) when running (NOT building) the toolgo toolruns without-modfile, use the value ofGOTOOLMODenvironment variable, if available, as the first fallback value for locating thego.modto use for resolving (and building) tools binaries. If building a tool is necessary, clearGOTOOLMODfrom the environment before callinggo build. KeepGOTOOLMODin the environment when executing the tool.Use cases
go toolcalled from a toolA tool called via
go toolmight want to call another tool, also viago tool.A tool called via
go tool -modfile=tools.modmight want to call another tool, also viago tool, but with the samego.modfile. For that case, the default value for-modfileforgo toolwould be set from theGOTOOLMODvalue. The first tool would just executego tooland would not have to specially care about-modfile.Example: a
go generatecommand 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 whichgo.modto use/tweak. While it could have its own-modfileflag, it would be simpler to make it aware implicitly of thego.modvia an environment variableGOTOOLMOD.Related proposals
go tool#78673: a wider proposal about exposing some environment variable(s) that allow a tool to be aware of being launched viago tool