Support go modules
In Golang, builds are moving away from GOPATH and toward a new module system. As of the latest release (Go 1.13), this is now the default build mode.
https://github.com/golang/go/wiki/Modules
There's an opportunity here to vastly simplify working with go and pants. A go module is defined by a go.mod file in its top-level directory. The go.mod file specifies all of the modules immediate remote dependencies (along with the minimum version required). The go tool can be used to fetch these dependencies (typically from a specified "proxy" server) into its own internal build cache.
Ideally, an entire go module could be built with pants by specifying a single BUILD file next to the go.mod file, containing only go_module(). Pants could assume that all **/*.go files are sources for this target, and configuration directives could specify how to configure go to fetch all the required modules.
This is a great and very necessary step for us to take! They've provided an example repo in the wiki, so I've started hacking away at this. I think that hooking up a go_module() target as you've described to the rest of the go tasks as step 1 sounds like a really feasible plan. Not sure what step 2 might be yet, but potentially looking at converting go_workspace_task.py to actually use go mod under the covers and remove a lot of complexity.
Hm, looking at this again, there are a few more factors/assumptions in play. In particular, go mod appears to be able to completely replace go_remote_library(). However, for the same reason go_remote_library() exists (so that targets can depend on the same version across a monorepo), I feel like we might want to be able to refer to that somehow in the go.mod file (we try to do this a little bit with the package.json in our node.js support).
For now, I'm seeing the quickest path to getting something working for a go mod-based build is to make the go run and go test tasks from scratch, but just as really small wrappers over calling go itself (since go.mod describes dependencies, ish). We can also make use of the new v2 engine to make these really concise. If we can have an experimental frontend that works with go modules working on a test project, we can then consider whether we want to scorch the earth and only accept go modules, or whether we want to structure some sort of migration plan for previous pants go users.
Super new to Pants, but if you need help on anything specific @cosmicexplorer I would like to see if I can help.
@cosmicexplorer for inspiration, I think pantsbuild might adopt similar structure to support go modules: https://github.com/simplebuild/please.make/tree/master/example_go
Thank you so much for the link!!! I am currently hacking away on cargo support and it has been extremely useful to look at other tools for that too.
Here’s more inspiration. I’m sorry I haven’t gotten around to this yet. One of the reasons I really don’t like the bazel ecosystem is its reliance on bazel BUILD file generators to perform functionality that should really be usable by other tools as well (e.g. pants has the separate pex tool for python: https://github.com/pantsbuild/pex) and that seems like it would have made it easier to let us copy from what this article describes: https://eng.uber.com/go-monorepo-bazel/.
I know this is really important to a lot of people and will try to make some time this weekend to start back up on pants go module support.
That was the reason we didn't use bazel/gazelle for go, it adds too much extra work to do what go build already does. The approach we took is quite simple:
- Have users place BUILD files with empty
go_binary()andgo_library()into every package, assuming that defaultsources=['*.go']. That's it for users, they should do nothing more - On build, analyze these .go files, find all dependencies with
go list -f '{{join .Imports "\n"}}'and dynamically add them to the 'dependencies' of go_library - Also let go_library implicitly depend on 'go.mod' and 'go.sum'
- When it's time to build, the build directory would have all .go files of a library/binary, all .go files of its dependencies, and go.mod, go.sum
- Build with regular
go buildwhile overriding GOGACHE so it points to a static directory where go toolchain will maintain and reuse its build cache (and override GOPATH so the modules fetched into GOPATH/pkg/mod are also cached)
This will be coming in 2.8.x thanks to lots of hard work from @tdyas and @Eric-Arellano!
See https://blog.pantsbuild.org/golang-support-pants-28/ for the announcement blog :)