Skip to main content

pants-backend-generate

A Pants backend for code generators whose outputs are committed because something outside Pants reads them from git. Inside Pants a generator is ordinary codegen, so consumers get fresh outputs in their sandboxes. pants generate writes the committed copies and pants lint fails when one is stale. Both use the same cached codegen rule, so the committed bytes are the bytes Pants consumers see.

This release targets Pants 2.32.x. Install it with pants-backend-generate==0.1.0 in [GLOBAL].plugins in pants.toml, and enable pants_backend_generate in [GLOBAL].backend_packages.

Status: alpha.

file_generate

Generated files (FileSourceField), kept as written. Language backends ship their own specializations with a formatter, for example cpp_generate in pants-backend-cmake. Every *_generate target has these fields:

  • runnable (required): the generator, a target pants run can run in a sandbox (such as a pex_binary), with args.
  • sources: files the generator reads, relative to the BUILD file. Optional.
  • outputs (required): every file the generator writes, as exact, normalised paths relative to the build root.
  • execution_dependencies: targets whose files are present in the sandbox.
  • runnable_dependencies: runnable targets put on PATH.
  • extra_env_vars, timeout, environment.

The generator runs at the build root with its sources and execution_dependencies at their repo-relative paths, and the parent directory of every output already exists. The outputs are exact. Each of these is an error:

  • a declared file that is not written
  • an undeclared file written next to a declared one
  • an input in an output directory that the generator overwrites
  • an output that is also an input (exclude the committed copy with a ! glob)

Put outputs in a directory: files at the build root are not checked for extras.

The host environment is not inherited, PATH included. A generator that runs host tools needs extra_env_vars=["PATH"] or the tools as runnable_dependencies.

The committed outputs belong to no target:

  • If another target's sources glob matches a committed output, a consumer of both targets gets two copies. Once the copy is stale, that fails with an opaque MergeDigests error. Exclude the output from that glob with !.
  • File-address specs and --changed-since do not reach the generating target, so run the check as pants lint ::.

pants generate

Runs the generators of the given *_generate targets and writes their outputs into the workspace. It prints one <address>: wrote <files> line per target. Two targets writing the same file is an error, and then nothing is written.

pants lint

The generate linter reports each committed output that differs from the generated one (stale) or does not exist (missing), followed by the pants generate command that refreshes them. --generate-skip turns it off.

Specializing

Codegen dispatches on the class of the target's sources field, so each output type needs its own target type. GenerateSpecialization makes one. In the sketch below, MySourceField is the sources field class your consumers ask for:

from pants.engine.rules import collect_rules, rule
from pants.engine.unions import UnionRule

from pants_backend_generate.specialization import GenerateSpecialization
from pants_backend_generate.util_rules.formatting import FormatOutputsRequest, FormattedOutputs


class MyFormatOutputsRequest(FormatOutputsRequest):
    pass


@rule
async def format_my_outputs(request: MyFormatOutputsRequest) -> FormattedOutputs:
    ...  # format request.snapshot; return exactly the same files


my_generate = GenerateSpecialization(
    "my_generate",
    output=MySourceField,
    formatter=MyFormatOutputsRequest,  # or None
    help="Sources written by a generator.",
)


def target_types():
    return my_generate.target_types()


def rules():
    return [
        *collect_rules(),
        *my_generate.rules(),
        UnionRule(FormatOutputsRequest, MyFormatOutputsRequest),
    ]

A specialization's rules() carries everything codegen needs, so another backend can register it without pants_backend_generate in backend_packages. Codegen then works, and a one-time warning says that pants generate and the linter need the backend enabled.

A formatter runs inside the codegen rule. It receives snapshot and address with EnvironmentName in scope, and it must return the same set of files. Aliases must be unique. Several specializations may share an output.

Metadata

Release files for pants-backend-generate 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pants-backend-generate 0.1.0
File Size Uploaded Provenance Actions
pants_backend_generate-0.1.0.tar.gz 12.6 kB Without provenance Details for pants_backend_generate-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pants-backend-generate 0.1.0
File Interpreter ABI Platform Actions
pants_backend_generate-0.1.0-py3-none-any.whl Python 3 none any Details for pants_backend_generate-0.1.0-py3-none-any.whl

Total release size: 28.0 kB

Release files / pants_backend_generate-0.1.0.tar.gz

Download URL pants_backend_generate-0.1.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8a891fc00d92594edfed39c067219fb55f6952c5008c9a00f2dc134682f33d23
BLAKE2b-256 checksum
How to use checksums
78f6b4554f152248cb2cc024507b0f919c30277a63ccf4b771535c43856883f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release files / pants_backend_generate-0.1.0-py3-none-any.whl

Download URL pants_backend_generate-0.1.0-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
676aff0f3e954ca229e301a555a6dc4bd1b7d6dbaa4b028953da0c08b5b19d52
BLAKE2b-256 checksum
How to use checksums
7faacb6faf4dcf7701de5e3a6357e483cbc84789dda8debc842554b67a52bc5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Security for pants-backend-generate 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page