Skip to content

Improve build.rs protobuf codegen and caching. - #2845

Draft
nathanielford wants to merge 2 commits into
grpc:masterfrom
nathanielford:refactor/build-script-caching
Draft

Improve build.rs protobuf codegen and caching.#2845
nathanielford wants to merge 2 commits into
grpc:masterfrom
nathanielford:refactor/build-script-caching

Conversation

@nathanielford

@nathanielford nathanielford commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Motivation

Improve build script around protobuf code generation. Ensure that:

  • Changes to protos trigger rebuilds.
  • Missing generated code triggers rebuilds.
  • User-passed env var triggers rebuilds.
  • Otherwise we don't trigger a rebuild.

Partially addresses #2661 (resolves build script codegen caching and unnecessary rebuilds; crate package separation between tonic and grpc examples remains open).

Problem

  1. Uncached builds and missing deletion recovery: In-tree generated directories required manually setting GRPC_RUST_REGENERATE_PROTO to regenerate. If generated/ was missing or .proto files were modified, builds failed or remained stale unless the flag was explicitly passed.
  2. Rebuild loop hazards: Emitting cargo:rerun-if-changed on output directory paths (generated/) causes Cargo to register directory timestamp mutations during execution, leading to infinite rebuild loops.
  3. Redundant environment lookups: CARGO_MANIFEST_DIR was read and used for path joins, despite Cargo guaranteeing the build script process working directory is already the package root.

Changes

  • Track specific output marker files (generated/.../generated.rs) and source proto inputs via cargo:rerun-if-changed.
  • Compare source .proto modification timestamps against generated marker files.
  • Automatically trigger CodeGen if:
    1. GRPC_RUST_REGENERATE_PROTO environment flag is provided.
    2. generated/ or output marker files are missing.
    3. Source .proto files have newer modification timestamps than generated output files.
  • Skip CodeGen on unchanged runs, keeping no-op builds cached at ~0.2s.
  • Remove redundant CARGO_MANIFEST_DIR conversions in favor of package-root relative paths.

Affected Files

  • grpc-protobuf/build.rs
  • examples/build.rs

Test Plan

  • Verified clean compilation with cargo check -p grpc-protobuf and cargo check -p examples --features grpc-helloworld,grpc-routeguide.
  • Verified fast-path no-op builds (~0.2s) across sequential executions.
  • Verified automatic regeneration when generated/ is deleted.
  • Verified automatic regeneration when .proto files are touched.
  • Ran formatting and clippy pre-commit checks (cargo fmt --all --check, cargo clippy).

Avoid rebuilding in-tree protobuf files when source protos and generated
markers are unchanged. Detect missing generated directories and proto
timestamp changes automatically while preserving fast-path (~0.2s) builds.
Remove redundant CARGO_MANIFEST_DIR usages.
@nathanielford nathanielford changed the title build: improve in-tree protobuf codegen caching and deletion recovery Improve build.rs protobuf codegen and caching. Sep 1, 2026
…GEN flag

* Check for protoc availability in build scripts and fall back to checked-in code with a warning if missing.
* Add SKIP_GRPC_RUST_PROTO_CODEGEN environment variable detection to unconditionally skip codegen during CI checks.
* Set SKIP_GRPC_RUST_PROTO_CODEGEN: 1 in semver CI workflow job.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant