This repository is a extremely rudimentary example of how to build and test Go code with Pantsbuild without usnig the Pants Go backend, but rather using the Go SDK directly. The goal is to provice a minimal integration between Pants and Go.
-
Enable the shell and adhoc backends by ensuring that
[GLOBAL].backend_packagesin yourpants.tomlincludespants.backend.shellandpants.backend.experimental.adhoc. -
Copy the
pants-macros/golang.pyPants prelude file into your repository. Then set[GLOBAL].build_file_prelude_globsin yourpants.tomlto include that file. This exposes two macros to your codeshoalsoft_go_binaryandshoalsoft_go_tests(described below). -
Configure a "workspace" environment by adding an
experimental_workspace_environmenttarget in the rootBUILDfile. Then set[environments-preview.names]in yourpants.tomlto include configure that environment by settingworkspace = "//:workspace"in that section (assuming the target was itself calledworkspace). -
Copy the
//:find_goroottarget from this repository into your repository's root BUILD file. -
In each Go source directory which builds a binary, add a
shoalsoft_go_binary(...)call to the PantsBUILDfile in that diretory. Set thedist_bin_nameon the macro call to be a unique name for the repository (which is used when writing the output underdist/). -
In the BUILD file at the top of the repository, add a call to
shoalsoft_go_tests(). This creates ago_teststarget in thatBUILDfile which runs all tests in the Go module rooted at the top of the repository. Note: This does not yet support modules defined in subdirectories.
Build a binary:
- On Pants v2.30.x and higher, run
pants package path/to/dir:bin, then look indist/. - On prior Pants versions,instead run
pants run path/to/dir:pkg-bin, then look indist/.
Run tests: pants test path/to/dir:go_tests