In-Workspace Execution in Pants v2.23.x

Photo by Getty Images on Unsplash
Pants is just one of many build orchestration tools in the world. As of Pants v2.23.0, Pants better supports integrating seamlessly with other tools in your development workflow via the new "workspace environments" feature. With workspace environments, you can run processes in the repository itself (i.e., the workspace) instead of in the usual execution sandbox. This support is useful for better integrating Pants with third party tooling which assumes it runs from your repository.
Read on for an example of how to use this support to integrate Bazel with Pants.
Motivation
Why might you want to run processes in the workspace?
One reason is if your development workflow uses third party tools not already supported by Pants. Such third party tools may assume they are the only build tool in use and expect that they will run from the workspace. Trying to run them from the Pants execution sandbox then ends up being harder than just running the tool directly because of the need to work around the tool's "run from workspace" assumption.
With workspace environments, Pants can now run those tools in the way that they expect, and let you avoid having to work around the tool's assumptions about how it should be invoked.
The Example: Running Bazel
The goal for this example is to let Pants invoke Bazel to build a JVM jar file and then make use of that jar within Pants build logic using the workspace environment feature. [0] We will use this GitHub repository for the example.
This article assumes you have some familiarity with the Pants environments system. The in-workspace execution support is modeled as "just another environment" and so most of the concepts applicable to other environments such as local_environment, docker_environment, and remote_environment are applicable to experimental_workspace_environment. For example, you can override any environment-aware configuration option in the same manner for experimental_workspace_environment as you would have for any of the other environment target types.
First make sure Pants, Bazel, and Docker are all installed.
Next, simply clone the repository with git clone https://github.com/pantsbuild/example-workspace-execution and then run pants run //:project-image.
Pants will invoke Bazel, Bazel will build the jar, and then Pants will build a Docker image from that jar and run the resulting Docker image. It may take some time for Bazel to build the jar the first time, and Pants will not display any output from Bazel until Bazel completes. You should see Hello! as the final output.
Let's walk through the code and configuration.
Repository layout
The repository is laid out as follows:
| Path | Description |
|---|---|
BUILD.pants | The root BUILD file for Pants. We are calling it |
pants.toml | The Pants configuration file |
bazel-jvm/** | A Bazel project which produces a jar file. |
bazel-jvm/src/main/java/com/example/**/*.java | The Java files to be built into the jar by Bazel. |
Pants configuration
-
Configuring the workspace environment. The first thing to do is configure a workspace environment to enable in-workspace execution support. In this example, we added an
experimental_workspace_environmenttarget to the repository in the rootBUILD.pants. Then we added the address for this target (//:workspace) topants.tomlunder the[environments-preview.names]key which gives the environment a name. -
Setting up the integration target. The example uses the
shell_commandtarget at address//:bazel-jvm-binaryto invoke Bazel.
-
That target is configured to use the workspace environment by virtue of setting its
environmentfield to the special symbol__local_workspace__which selects whateverexperimental_workspace_environmentmatches the current platform. (If there is only one such environment, then it will always match.) We could have also just used the name of the workspace directly. -
The new
path_env_modifyfield onshell_commandis set tooffso that Pants does not modify thePATHenvironment variable. By default, Pants will inject a directory with symlinks to thetoolsset on ashell_commandtarget and prepend that directory to thePATH. Bazel incorporates thePATHinto its own cache key and so we need to disable Pants changing that value so that Bazel does not invalidate the jar every time it is invoked. -
The output from Bazel is copied to the
{chroot}directory. Ordinarily, when Pants invokes a process,{chroot}refers to the execution sandbox. With workspace execution, this is no longer the case because the working directory is now within the repository. Pants, however, will still create a (now separate) temporary directory to allow materializing dependencies and to allow capture of outputs. Restated, Pants will not capture outputs from the repository, only from the temporary directory created during execution; that is, the{chroot}directory.
- Using the output from Bazel. The
docker_imagetarget at address//:project_imageconsumes the output from the//:bazel-jvm-binaryshell_commandtarget by listing it as a dependency in thedependenciesfield. The Docker image is setup to invoke the jar. It is that simple to consume Bazel's output in Pants!
Limitations & Caveats
There are some limitations with the in-workspace execution support:
-
The main issue is that it has only been designed to work seamlessly with the
shell_commandandadhoc_tooltarget types. Using this support with other target types (for example,pex_binaryorgo_binary) has not been tested and you may encounter odd behavior because workspace environments violate the core Pants assumption that all execution occurs in temporary sandboxes. We have not tested those other use cases in any meaningful way. -
Any non-deterministic behavior in the external build tool or in the integration target may impact the ability of Pants to maintain reproducibility of the build. This is not a problem with workspace execution per se, but workspace execution can exacerbate any existing non-determinisms because it removes the execution sandbox as a mitigation. You, as the developer, always have the responsibility to configure Pants to operate in a deterministic way.
Conclusion & Credits
Hopefully the user community will find this support useful. We look forward to what you all build with it!
This work was awesomely sponsored by Proxima Fusion GmbH.
[0] Having Bazel build a jar is a contrived example since Pants does have JVM support. But using Bazel’s JVM support for this example made it more straightforward to demonstrate integration between Pants and Bazel.