From 66fa2460b4270e0e93623c39e0ad764a34dcace7 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 25 Nov 2026 15:29:37 -0300 Subject: [PATCH 1/4] Ship kit-rpc's generated code as ordinary source, with no build script --- .github/actions/setup-rust/action.yml | 9 +- CHANGELOG.md | 1 + docs/ARCHITECTURE.md | 3 +- docs/RELEASE.md | 2 +- rust/Cargo.lock | 10 +- rust/Cargo.toml | 1 + rust/crates/kit-rpc-codegen/Cargo.toml | 23 +++ rust/crates/kit-rpc-codegen/src/main.rs | 107 ++++++++++++ rust/crates/kit-rpc/Cargo.toml | 21 +-- rust/crates/kit-rpc/README.md | 9 +- rust/crates/kit-rpc/build.rs | 175 -------------------- rust/crates/kit-rpc/generated/README.md | 12 +- rust/crates/kit-rpc/src/lib.rs | 10 +- rust/crates/kit-transport-connect/README.md | 10 +- rust/release-plz.toml | 3 + scripts/codegen-out-dir.py | 18 -- scripts/regen-rpc-proto.sh | 31 ++-- scripts/regen-transport-proto.sh | 30 +--- scripts/test-codegen-out-dir.py | 29 ---- 19 files changed, 201 insertions(+), 303 deletions(-) create mode 100644 rust/crates/kit-rpc-codegen/Cargo.toml create mode 100644 rust/crates/kit-rpc-codegen/src/main.rs delete mode 100644 rust/crates/kit-rpc/build.rs delete mode 100644 scripts/codegen-out-dir.py delete mode 100644 scripts/test-codegen-out-dir.py diff --git a/.github/actions/setup-rust/action.yml b/.github/actions/setup-rust/action.yml index 38198f851..5797f28ca 100644 --- a/.github/actions/setup-rust/action.yml +++ b/.github/actions/setup-rust/action.yml @@ -23,10 +23,11 @@ inputs: required: true protoc: description: >- - Install protoc. Required by any job that builds kit-rpc (buffa-build - shells out to protoc; the .proto schemas use Edition 2023, needing - protoc >= 25, newer than apt's protobuf-compiler). Set "false" for jobs - that never compile kit-rpc (e.g. the keystore-only matrix). + Install protoc. Required by any job that runs kit-rpc-codegen + (buffa-build shells out to protoc; the .proto schemas use Edition 2023, + needing protoc >= 25, newer than apt's protobuf-compiler). Building + kit-rpc needs no protoc. Set "false" for jobs that never run the + generators. required: false default: "true" protoc-version: diff --git a/CHANGELOG.md b/CHANGELOG.md index 03825357f..bb1a115e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Changed +- `kit-rpc` compiles its checked-in generated code as ordinary source: it has no build script and no build dependencies, so no crate that depends on it builds `buffa-build`, `buffa-codegen`, `buffa-descriptor`, `connectrpc-build`, `connectrpc-codegen`, `prettyplease` or `anyhow` for it. The generators moved to the unpublished `kit-rpc-codegen` crate, which `scripts/regen-rpc-proto.sh` and `scripts/regen-transport-proto.sh` run. - `kit checkout` in a repository with no `.kit/index` file, as `kit clone --no-checkout` leaves it, is the first checkout, as in git: nothing is tracked yet, so no file counts as a local change and no tracked path is removed. A file the checkout would write over still refuses it. - **Breaking:** the ssh `ListRefs` request and reply page, as Connect's do (SPEC-TRANSPORT §4.2.3): `kit serve` returns at most 1,000 refs a page, and `kit+ssh` remotes read every page. A new `kit serve` refuses a `ListRefs` with no `page_size`, so a client from before this change cannot list refs on it and has to be upgraded; a new client against an old `kit serve` works as before, up to the old limit of one frame. - `kit_core::protocol::RefListing::OneFrame`, `kit_sync::push::MAX_TAG_LISTING_BYTES` and `TagPushRejection::TooManyTags` are removed: no remote lists in one frame any more, so a tag push no longer refuses a create over a listing bound. A push of tags to a `kit serve` that predates paging is not bounded either, so a tag listing there that grows past one frame fails for every client until that server is upgraded. The `tag listing failed` message no longer says "possibly too many tags". diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index fafabd71b..f13f49d8c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -112,7 +112,8 @@ Each Total is Source plus Tests plus Generated. Generated code is a documented exception to the rule against checked-in generated output. The code generator needs `protoc`, and many hosts ship a `protoc` too old for protobuf edition 2023. A normal build needs neither, because -`rust/crates/kit-rpc/generated/` stays in the tree. The +`rust/crates/kit-rpc/generated/` stays in the tree and compiles as ordinary +source. The generators live in the `kit-rpc-codegen` tool crate. The README in that directory gives the reason. `scripts/check-generated-fresh.sh` regenerates the code and fails when the checked-in copy differs. Only `kit-rpc` holds generated code. diff --git a/docs/RELEASE.md b/docs/RELEASE.md index 84f70d96d..90d6dc57f 100644 --- a/docs/RELEASE.md +++ b/docs/RELEASE.md @@ -390,7 +390,7 @@ Review the file list of a crate with `cargo package --list -p `. | Publishes to crates.io | Stays off crates.io (`publish = false`) | |---|---| -| In publish (dependency) order: `kit-core`, `kit-rpc`, `kit-attest`, `kit-keystore`, `kit-transport-file`, `kit-serve`, `kit-client`, `kit-sync`, `kit-transport-memory`, `kit-transport-connect`, `kit-transport-s3`, `kit-transport-ssh`, `kit-git-bridge`, `libkit`, and `kit-bin` (so `cargo install kit-bin` works) | `kit-sign-file`, `kit-test-util`, `kit-formal-conformance`, `fuzz`, `benches`, and `kit-wasm`, which `release.yml` publishes to npm. | +| In publish (dependency) order: `kit-core`, `kit-rpc`, `kit-attest`, `kit-keystore`, `kit-transport-file`, `kit-serve`, `kit-client`, `kit-sync`, `kit-transport-memory`, `kit-transport-connect`, `kit-transport-s3`, `kit-transport-ssh`, `kit-git-bridge`, `libkit`, and `kit-bin` (so `cargo install kit-bin` works) | `kit-sign-file`, `kit-test-util`, `kit-formal-conformance`, `kit-rpc-codegen`, `fuzz`, `benches`, and `kit-wasm`, which `release.yml` publishes to npm. | The published crates depend only on each other, forming a closed, dependency-ordered set. The table lists them in that order. diff --git a/rust/Cargo.lock b/rust/Cargo.lock index e3cf0e917..39b80b29b 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2398,14 +2398,20 @@ version = "0.10.0" dependencies = [ "arbitrary", "buffa", - "buffa-build", "connectrpc", - "connectrpc-build", "kit-core", "serde", "thiserror", ] +[[package]] +name = "kit-rpc-codegen" +version = "0.10.0" +dependencies = [ + "buffa-build", + "connectrpc-build", +] + [[package]] name = "kit-serve" version = "0.10.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index a02af7e41..f33c89089 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,6 +5,7 @@ members = [ "crates/kit-core", "crates/kit-test-util", "crates/kit-rpc", + "crates/kit-rpc-codegen", "crates/kit-attest", "crates/kit-keystore", "crates/kit-git-bridge", diff --git a/rust/crates/kit-rpc-codegen/Cargo.toml b/rust/crates/kit-rpc-codegen/Cargo.toml new file mode 100644 index 000000000..7f3d57a25 --- /dev/null +++ b/rust/crates/kit-rpc-codegen/Cargo.toml @@ -0,0 +1,23 @@ +# Regenerates the protocol code that `kit-rpc` checks in under `generated/`. +# Run by `scripts/regen-rpc-proto.sh` and `scripts/regen-transport-proto.sh`. +# +# `publish = false`, never a dependency of anything. Keeping the code +# generators here keeps `buffa-build`, `connectrpc-build` and their tree out of +# the build of every crate that depends on `kit-rpc`. +[package] +name = "kit-rpc-codegen" +description = "Regenerates the protocol code kit-rpc checks in" +publish = false +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +authors.workspace = true + +[lints] +workspace = true + +[dependencies] +buffa-build = "0.9.2" +connectrpc-build = "0.9.0" diff --git a/rust/crates/kit-rpc-codegen/src/main.rs b/rust/crates/kit-rpc-codegen/src/main.rs new file mode 100644 index 000000000..81345e370 --- /dev/null +++ b/rust/crates/kit-rpc-codegen/src/main.rs @@ -0,0 +1,107 @@ +//! Regenerates the protocol code that `kit-rpc` checks in. +//! +//! `kit-rpc-codegen rpc` writes `generated/` from the signer, SSH, verify and +//! common schemas. `kit-rpc-codegen transport` writes `generated/transport/` +//! from the canonical transport and health schemas. Both need `protoc` 27 or +//! newer on `PATH` (or in `PROTOC`), because the schemas use protobuf edition +//! 2023. Run them through `scripts/regen-rpc-proto.sh` and +//! `scripts/regen-transport-proto.sh`. + +use std::error::Error; +use std::fs; +use std::path::{Path, PathBuf}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + // `rust/crates/kit-rpc-codegen` -> `rust/crates` -> `rust` -> repo root. + let repo = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.."); + let crate_dir = repo.join("rust/crates/kit-rpc"); + match std::env::args().nth(1).as_deref() { + Some("rpc") => rpc(&repo, &crate_dir), + Some("transport") => transport(&repo, &crate_dir), + _ => Err("usage: kit-rpc-codegen ".into()), + } +} + +/// Removes the `.rs` files directly under `dir` and makes sure it exists. +fn clear(dir: &Path) -> Result<()> { + fs::create_dir_all(dir)?; + for entry in fs::read_dir(dir)? { + let path = entry?.path(); + if path.is_file() && path.extension().is_some_and(|e| e == "rs") { + fs::remove_file(path)?; + } + } + Ok(()) +} + +fn rpc(repo: &Path, crate_dir: &Path) -> Result<()> { + let proto_dir = crate_dir.join("proto"); + // The shared ref types (`kit.common.v1.RefExpectation` and `RefEntry`) + // live in the repository's canonical proto root. + let common_dir = repo.join("proto"); + let files = [ + proto_dir.join("kit/rpc/v1/common.proto"), + proto_dir.join("kit/rpc/v1/signer/signer.proto"), + proto_dir.join("kit/rpc/v1/ssh/ssh.proto"), + proto_dir.join("kit/rpc/v1/verify/verify.proto"), + common_dir.join("kit/common/v1/refs.proto"), + ]; + let out = crate_dir.join("generated"); + clear(&out)?; + buffa_build::Config::new() + .files(&files) + .includes(&[&proto_dir, &common_dir]) + .include_file("_includes.rs") + // `arbitrary::Arbitrary` derives, gated behind kit-rpc's opt-in + // `arbitrary` feature, for the fuzz harness. + .generate_arbitrary(true) + .out_dir(&out) + .compile()?; + Ok(()) +} + +fn transport(repo: &Path, crate_dir: &Path) -> Result<()> { + let root = repo.join("proto"); + let files = [ + root.join("kit/transport/v1/transport.proto"), + root.join("grpc/health/v1/health.proto"), + ]; + let out = crate_dir.join("generated/transport"); + clear(&out)?; + connectrpc_build::Config::new() + .files(&files) + .includes(&[&root]) + .include_file("_connectrpc.rs") + .out_dir(&out) + .emit_rerun_directives(false) + .compile()?; + gate_bindings(&out) +} + +/// Keeps one generated message tree while letting a consumer leave out the +/// Connect runtime: the service bindings compile only with kit-rpc's +/// `transport` feature. +fn gate_bindings(out: &Path) -> Result<()> { + const GATED: [(&str, &str); 2] = [ + ( + "kit.transport.v1.mod.rs", + "include!(\"kit.transport.v1.transport.__connect.rs\");", + ), + ( + "grpc.health.v1.mod.rs", + "include!(\"grpc.health.v1.health.__connect.rs\");", + ), + ]; + for (module, include) in GATED { + let path: PathBuf = out.join(module); + let source = fs::read_to_string(&path)?; + if !source.contains(include) { + return Err(format!("{module} has no `{include}` to gate").into()); + } + let gated = format!("#[cfg(feature = \"transport\")]\n{include}"); + fs::write(&path, source.replace(include, &gated))?; + } + Ok(()) +} diff --git a/rust/crates/kit-rpc/Cargo.toml b/rust/crates/kit-rpc/Cargo.toml index c5fadfc6c..dd80aa147 100644 --- a/rust/crates/kit-rpc/Cargo.toml +++ b/rust/crates/kit-rpc/Cargo.toml @@ -6,7 +6,7 @@ categories = ["encoding", "data-structures"] readme = "README.md" # Ship the sources only. The tests use goldens and helpers that live outside # this package, so they cannot run from a published copy. -include = ["src/**", "/build.rs", "/generated/**", "/proto/**", "/README.md", "/LICENSE-APACHE", "/LICENSE-MIT"] +include = ["src/**", "/generated/**", "/proto/**", "/README.md", "/LICENSE-APACHE", "/LICENSE-MIT"] version.workspace = true edition.workspace = true rust-version.workspace = true @@ -22,7 +22,7 @@ authors.workspace = true # only. [lints.clippy] # Drop pedantic + nursery + cargo for this crate. The buffa-generated -# code (~640 KiB into $OUT_DIR) inherits docstrings from our .proto +# code (~640 KiB under generated/) inherits docstrings from our .proto # files and uses code patterns that pedantic flags hundreds of times. # We keep `clippy::all` (the correctness + style baseline) and our # own hand-written code in src/ stays under it. @@ -40,7 +40,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(kani)"] } [features] # Shared wasm-clean TransportService and health messages and Connect bindings. -transport = ["transport-messages", "dep:connectrpc", "dep:connectrpc-build"] +transport = ["transport-messages", "dep:connectrpc"] # Canonical messages without Connect runtime bindings (no tokio dependency). transport-messages = ["dep:serde", "buffa/json"] # Opt-in `arbitrary::Arbitrary` derives on all generated wire types, @@ -73,19 +73,12 @@ thiserror = "2.0.21" # features on, so their builds still get `pack-zstd` via that edge. kit-core = { version = "=0.10.0", path = "../kit-core", default-features = false } -[build-dependencies] -connectrpc-build = { version = "0.9.0", optional = true } -buffa-build = "0.9.2" - # cargo-machete scans only the Rust sources under src/. It cannot see these -# dependencies, so it lists them as unused. Each one is used. -# - serde, connectrpc and arbitrary: the generated modules under generated/ -# name `::serde`, `::connectrpc` and `::arbitrary::Arbitrary`. build.rs -# stages them into OUT_DIR, and src/lib.rs includes them. -# - buffa-build and connectrpc-build: build.rs calls them when -# KIT_RPC_CODEGEN is set. +# dependencies, so it lists them as unused. Each one is used: the generated +# modules under generated/ name `::serde`, `::connectrpc` and +# `::arbitrary::Arbitrary`, and src/lib.rs includes them. [package.metadata.cargo-machete] -ignored = ["arbitrary", "buffa-build", "connectrpc", "connectrpc-build", "serde"] +ignored = ["arbitrary", "connectrpc", "serde"] [package.metadata.docs.rs] # Build docs with every feature so docs.rs renders the feature-gated API. diff --git a/rust/crates/kit-rpc/README.md b/rust/crates/kit-rpc/README.md index b28c99fea..c3a47d9b1 100644 --- a/rust/crates/kit-rpc/README.md +++ b/rust/crates/kit-rpc/README.md @@ -36,8 +36,9 @@ Both protocols use the same length-prefixed framing: [u32 LE length][N bytes protobuf-encoded Frame] ``` -Generated code is vendored (not built fresh from `.proto` on every build); -regenerate it with `scripts/regen-rpc-proto.sh` after editing a schema. +Generated code is checked in under `generated/` and compiled as ordinary +source: kit-rpc has no build script and no build dependencies. Regenerate it +with `scripts/regen-rpc-proto.sh` after editing a schema. ## Shared Connect transport bindings @@ -52,5 +53,5 @@ do not enable it. Refresh `generated/transport/` with For runtime-independent clients, enable `transport-messages` instead. It exposes the same message types at the same paths, without service bindings, ConnectRPC or tokio. The `transport` feature includes this feature. Both -features stage the same generated files; the build script gates only the -binding includes, including during regeneration. `kit-client` uses this surface. +features compile the same generated files; the generator gates only the +binding includes. `kit-client` uses this surface. diff --git a/rust/crates/kit-rpc/build.rs b/rust/crates/kit-rpc/build.rs deleted file mode 100644 index 57fb2821b..000000000 --- a/rust/crates/kit-rpc/build.rs +++ /dev/null @@ -1,175 +0,0 @@ -// Stages the buffa-generated Rust modules for the .proto schemas in -// proto/ into $OUT_DIR. -// -// Default path: copy the pre-generated sources committed under -// generated/ — no protoc required. This keeps docs.rs (whose build -// image ships protoc 3.21, too old for the `edition = "2023"` protos) -// and stock-distro users building, with zero system dependencies. -// -// Regeneration path: set KIT_RPC_CODEGEN=1 to run buffa-build against -// proto/ and the shared proto root in the repository instead (requires -// protoc >= 27 on PATH or via PROTOC). The published crate has no shared -// proto root, so it builds only from generated/. After -// changing a .proto, run scripts/regen-rpc-proto.sh from the repo root -// to refresh generated/ and commit the result. - -use std::path::PathBuf; - -fn main() { - #[cfg(any(feature = "transport", feature = "transport-messages"))] - transport::stage(); - let proto_dir = PathBuf::from("proto"); - // Shared ref types (kit.common.v1.RefExpectation / RefEntry), one repo - // root up from rust/crates/kit-rpc — see kit/common/v1/refs.proto's - // header comment. - let common_dir = PathBuf::from("../../../proto"); - let common_refs_proto = common_dir.join("kit/common/v1/refs.proto"); - - let package_files = [ - proto_dir.join("kit/rpc/v1/common.proto"), - proto_dir.join("kit/rpc/v1/signer/signer.proto"), - proto_dir.join("kit/rpc/v1/ssh/ssh.proto"), - proto_dir.join("kit/rpc/v1/verify/verify.proto"), - ]; - - // Re-run when a .proto, the vendored output, or the mode changes. - // Without these, cargo re-runs build.rs on every recompile. - // Every path here is inside the package. A path outside the package is - // absent from the published crate and would force a re-run on every build. - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=generated"); - for f in &package_files { - println!("cargo:rerun-if-changed={}", f.display()); - } - println!("cargo:rerun-if-env-changed=PROTOC"); - println!("cargo:rerun-if-env-changed=KIT_RPC_CODEGEN"); - - let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR")); - // Marker distinguishing real codegen output from staged copies of - // generated/ — both fill OUT_DIR with the same .rs file set, so - // scripts/regen-rpc-proto.sh needs this to find the right dir. - let marker = out_dir.join(".kit-rpc-codegen"); - - if std::env::var_os("KIT_RPC_CODEGEN").is_some() { - // Codegen runs only in the repository, where the shared proto root - // exists. The published crate never takes this path. - println!("cargo:rerun-if-changed={}", common_refs_proto.display()); - let mut files = package_files.to_vec(); - files.push(common_refs_proto); - buffa_build::Config::new() - .files(&files) - .includes(&[&proto_dir, &common_dir]) - .include_file("_includes.rs") - // Emits `arbitrary::Arbitrary` derives gated behind the - // crate's opt-in `arbitrary` feature — used by the fuzz - // harness (rust/fuzz) for decode/roundtrip targets. - .generate_arbitrary(true) - .compile() - .expect("buffa codegen"); - std::fs::write(&marker, b"").expect("write codegen marker"); - return; - } - // A previous codegen-mode run may have used this same OUT_DIR; - // drop its marker so the regen script never copies staged files. - let _ = std::fs::remove_file(&marker); - let vendored = PathBuf::from("generated"); - let mut staged = 0usize; - for entry in std::fs::read_dir(&vendored).expect("read generated/") { - let entry = entry.expect("read generated/ entry"); - let path = entry.path(); - if path.extension().is_some_and(|e| e == "rs") { - std::fs::copy(&path, out_dir.join(entry.file_name())).expect("stage generated module"); - staged += 1; - } - } - assert!( - staged > 0, - "generated/ contains no .rs modules — run scripts/regen-rpc-proto.sh" - ); -} - -#[cfg(any(feature = "transport", feature = "transport-messages"))] -mod transport { - use std::path::{Path, PathBuf}; - - /// Written next to real codegen output, so the regen script can tell it - /// from a staged copy of `generated/` (both fill `OUT_DIR` with the same - /// file set). - const MARKER: &str = ".kit-rpc-transport-codegen"; - - pub(crate) fn stage() { - let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR")); - println!("cargo:rerun-if-changed=generated/transport"); - println!("cargo:rerun-if-env-changed=PROTOC"); - println!("cargo:rerun-if-env-changed=KIT_TRANSPORT_CODEGEN"); - let marker = out_dir.join(MARKER); - - if std::env::var_os("KIT_TRANSPORT_CODEGEN").is_some() { - #[cfg(feature = "transport")] - { - // The repo-root canonical proto module, three hops up - // (rust/crates/kit-rpc -> repo root). - let root = Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../../proto") - .canonicalize() - .expect("canonical proto root not found: expected proto/ at repo root"); - let transport = root.join("kit/transport/v1/transport.proto"); - let health = root.join("grpc/health/v1/health.proto"); - println!("cargo:rerun-if-changed={}", transport.display()); - println!("cargo:rerun-if-changed={}", health.display()); - connectrpc_build::Config::new() - .files(&[ - transport.to_str().expect("proto path is valid UTF-8"), - health.to_str().expect("proto path is valid UTF-8"), - ]) - .includes(&[root.to_str().expect("proto root is valid UTF-8")]) - .include_file("_connectrpc.rs") - .compile() - .expect("connectrpc-build codegen failed for the canonical protos"); - gate_bindings(&out_dir); - std::fs::write(&marker, b"").expect("write codegen marker"); - return; - } - #[cfg(not(feature = "transport"))] - panic!("transport codegen requires the transport feature"); - } - - let _ = std::fs::remove_file(&marker); - let mut staged = 0usize; - for entry in - std::fs::read_dir(Path::new("generated/transport")).expect("read generated/transport/") - { - let path = entry.expect("read generated/transport/ entry").path(); - if path.extension().is_some_and(|e| e == "rs") { - let name = path.file_name().expect("file name"); - std::fs::copy(&path, out_dir.join(name)).expect("stage generated module"); - staged += 1; - } - } - gate_bindings(&out_dir); - assert!( - staged > 0, - "generated/transport/ contains no .rs modules: run scripts/regen-transport-proto.sh" - ); - } - - // Keep a single generated message tree, while allowing consumers to omit - // the Connect runtime. Codegen and staging use exactly the same gate. - fn gate_bindings(out_dir: &Path) { - for module in ["kit.transport.v1.mod.rs", "grpc.health.v1.mod.rs"] { - let path = out_dir.join(module); - let source = std::fs::read_to_string(&path).expect("read generated module"); - if source.contains("#[cfg(feature = \"transport\")]\ninclude!") { - continue; - } - let source = source.replace( - "include!(\"kit.transport.v1.transport.__connect.rs\");", - "#[cfg(feature = \"transport\")]\ninclude!(\"kit.transport.v1.transport.__connect.rs\");", - ).replace( - "include!(\"grpc.health.v1.health.__connect.rs\");", - "#[cfg(feature = \"transport\")]\ninclude!(\"grpc.health.v1.health.__connect.rs\");", - ); - std::fs::write(path, source).expect("gate generated bindings"); - } - } -} diff --git a/rust/crates/kit-rpc/generated/README.md b/rust/crates/kit-rpc/generated/README.md index b48b6e799..1c9cf8a14 100644 --- a/rust/crates/kit-rpc/generated/README.md +++ b/rust/crates/kit-rpc/generated/README.md @@ -6,14 +6,16 @@ checked-in generated output. ## Why the files are here -The files come from the `.proto` schemas. The code generator (`buffa-build`) -runs `protoc` or `buf`. Those tools are not part of a normal Rust build. The +The files come from the `.proto` schemas. The code generators (`buffa-build` and +`connectrpc-build`) run `protoc` or `buf`. Those tools are not part of a normal Rust build. The schemas also use protobuf edition 2023, and many hosts ship a `protoc` that is too old to read it. docs.rs is one of them. A build that generated the code would fail on those hosts. -The build script copies these files into the build output. A normal build -needs no `protoc`. +`kit-rpc` compiles these files as ordinary source. It has no build script and +no build dependencies, so a normal build needs no `protoc` and builds no code +generator. The generators live in the `kit-rpc-codegen` tool crate, which +nothing depends on. ## How the files stay correct @@ -24,7 +26,7 @@ change without matching generated files fails CI. ## How to refresh the files Run `scripts/regen-rpc-proto.sh` and `scripts/regen-transport-proto.sh`. -Both scripts need `protoc` 27 or newer. Commit the result in the same commit +Both scripts run `kit-rpc-codegen` and need `protoc` 27 or newer. Commit the result in the same commit as the schema change. Never edit these files by hand. diff --git a/rust/crates/kit-rpc/src/lib.rs b/rust/crates/kit-rpc/src/lib.rs index bac7d27e6..4588a0f4b 100644 --- a/rust/crates/kit-rpc/src/lib.rs +++ b/rust/crates/kit-rpc/src/lib.rs @@ -46,10 +46,10 @@ // `unsafe` — enforce that it stays that way. #![forbid(unsafe_code)] -// Generated protobuf modules. `_includes.rs` is emitted by -// buffa-build; it sets up the module tree for common.proto, -// signer.proto, ssh.proto under `kit::rpc::v1`. -include!(concat!(env!("OUT_DIR"), "/_includes.rs")); +// Generated protobuf modules, checked in under `generated/`. +// `_includes.rs` sets up the module tree for common.proto, signer.proto, +// ssh.proto and verify.proto under `kit::rpc::v1`. +include!("../generated/_includes.rs"); /// Maximum length of a single framed protobuf message in bytes. /// Both directions of both protocols enforce this cap; receivers @@ -151,5 +151,5 @@ mod tests { #[cfg(any(feature = "transport", feature = "transport-messages"))] #[allow(missing_debug_implementations, clippy::all, clippy::pedantic)] pub mod transport { - include!(concat!(env!("OUT_DIR"), "/_connectrpc.rs")); + include!("../generated/transport/_connectrpc.rs"); } diff --git a/rust/crates/kit-transport-connect/README.md b/rust/crates/kit-transport-connect/README.md index ffe12dc99..60cc7bd95 100644 --- a/rust/crates/kit-transport-connect/README.md +++ b/rust/crates/kit-transport-connect/README.md @@ -27,12 +27,12 @@ Connect-code mapping (the client direction is `src/error.rs`), and the `generated`; its native runtime features stay here. Both the client and server use the canonical `/proto/kit/transport/v1/transport.proto`. -- **Default path**: `kit-rpc` stages committed sources from - `generated/transport/` into `$OUT_DIR`; consumers and docs.rs need no `protoc`. +- **Default path**: `kit-rpc` compiles the committed sources in + `generated/transport/`; consumers and docs.rs need no `protoc`. - **Regeneration path**: run `scripts/regen-transport-proto.sh` from the repo - root (requires `protoc >= 27` on `PATH`, or via `PROTOC`). It enables - `KIT_TRANSPORT_CODEGEN=1` for the owning crate and refreshes the single - shared tree. Commit that output after editing the canonical proto. + root (requires `protoc >= 27` on `PATH`, or via `PROTOC`). It runs the + `kit-rpc-codegen` tool and refreshes the single shared tree. Commit that + output after editing the canonical proto. ## Native and wasm diff --git a/rust/release-plz.toml b/rust/release-plz.toml index 983e0757c..ed28ff8b7 100644 --- a/rust/release-plz.toml +++ b/rust/release-plz.toml @@ -108,6 +108,9 @@ release = false name = "kit-fuzz" release = false [[package]] +name = "kit-rpc-codegen" +release = false +[[package]] name = "kit-sign-file" release = false [[package]] diff --git a/scripts/codegen-out-dir.py b/scripts/codegen-out-dir.py deleted file mode 100644 index accd8e32b..000000000 --- a/scripts/codegen-out-dir.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: MIT OR Apache-2.0 -"""Find a marked codegen OUT_DIR in Cargo's JSON build messages.""" - -import json -from pathlib import Path -import sys - -out_dir = None -for line in sys.stdin: - message = json.loads(line) - if message.get("reason") == "build-script-executed": - candidate = message["out_dir"] - if (Path(candidate) / sys.argv[1]).is_file(): - out_dir = candidate -if out_dir is None: - sys.exit("error: Cargo reported no marked codegen output") -print(out_dir) diff --git a/scripts/regen-rpc-proto.sh b/scripts/regen-rpc-proto.sh index 04a91a5d9..a2b5e494a 100755 --- a/scripts/regen-rpc-proto.sh +++ b/scripts/regen-rpc-proto.sh @@ -1,14 +1,16 @@ #!/usr/bin/env bash # SPDX-License-Identifier: MIT OR Apache-2.0 # -# Regenerate the vendored buffa codegen output for kit-rpc. +# Regenerate the checked-in buffa codegen output for kit-rpc. # -# kit-rpc builds from pre-generated sources committed under -# rust/crates/kit-rpc/generated/ so that consumers (and docs.rs) never -# need protoc. After editing any file in rust/crates/kit-rpc/proto/, -# run this script from the repo root and commit the refreshed output. +# kit-rpc compiles the generated sources committed under +# rust/crates/kit-rpc/generated/, so that consumers (and docs.rs) never need +# protoc and never build a code generator. After editing any file in +# rust/crates/kit-rpc/proto/ or proto/kit/common/, run this script from the +# repo root and commit the refreshed output. # -# Requires protoc >= 27 on PATH (protobuf `edition = "2023"` support). +# Requires protoc >= 27 on PATH, or in PROTOC (protobuf `edition = "2023"` +# support). set -euo pipefail @@ -16,22 +18,17 @@ cd "$(dirname "$0")/.." GEN_DIR=rust/crates/kit-rpc/generated -# Cargo reports the actual OUT_DIR, including configured build directories. -out_dir=$(KIT_RPC_CODEGEN=1 cargo build --manifest-path rust/Cargo.toml -p kit-rpc \ - --message-format=json | python3 scripts/codegen-out-dir.py .kit-rpc-codegen) +cargo run --quiet --locked --manifest-path rust/Cargo.toml -p kit-rpc-codegen -- rpc -rm -f "$GEN_DIR"/*.rs -mkdir -p "$GEN_DIR" -cp "$out_dir"/_includes.rs "$out_dir"/kit.rpc.*.rs \ - "$out_dir"/kit.common.*.rs "$GEN_DIR/" - -echo "refreshed $GEN_DIR from $out_dir:" +echo "refreshed $GEN_DIR:" ls "$GEN_DIR" -if ! git diff --quiet -- "$GEN_DIR"; then +# `git status --porcelain`, not only `git diff`: a new module lands untracked. +# The transport tree below generated/ has its own script. +if [ -n "$(git status --porcelain -- "$GEN_DIR" ":!$GEN_DIR/transport")" ]; then echo echo "generated output changed — review and commit:" - git status --short -- "$GEN_DIR" + git status --short -- "$GEN_DIR" ":!$GEN_DIR/transport" else echo "generated output is unchanged." fi diff --git a/scripts/regen-transport-proto.sh b/scripts/regen-transport-proto.sh index ae3726a90..5766ef463 100755 --- a/scripts/regen-transport-proto.sh +++ b/scripts/regen-transport-proto.sh @@ -11,34 +11,18 @@ set -euo pipefail cd "$(dirname "$0")/.." -# $1 = human label, $2 = generated directory, $3 = Cargo's OUT_DIR. -refresh() { - local label="$1" gen_dir="$2" out="$3" - rm -f "$gen_dir"/*.rs - mkdir -p "$gen_dir" - # The owning crate also stages RPC schemas at OUT_DIR's root. Select - # only this Connect generation's transport/health files and wrapper. - cp "$out"/_connectrpc.rs "$out"/kit.transport.*.rs \ - "$out"/grpc.health.*.rs "$gen_dir/" - echo "refreshed $gen_dir from $out:" - ls "$gen_dir" -} +GEN_DIR=rust/crates/kit-rpc/generated/transport -echo ">> kit-rpc transport (wasm32 target)" -out_dir=$(KIT_TRANSPORT_CODEGEN=1 cargo build --manifest-path rust/Cargo.toml -p kit-rpc \ - --features transport --target wasm32-unknown-unknown --message-format=json | \ - python3 scripts/codegen-out-dir.py .kit-rpc-transport-codegen) -refresh "kit-rpc transport" \ - "rust/crates/kit-rpc/generated/transport" \ - "$out_dir" +cargo run --quiet --locked --manifest-path rust/Cargo.toml -p kit-rpc-codegen -- transport + +echo "refreshed $GEN_DIR:" +ls "$GEN_DIR" -generated_dirs=(rust/crates/kit-rpc/generated/transport) # `git status --porcelain`, not only `git diff`: a new module lands untracked. -if ! git diff --quiet -- "${generated_dirs[@]}" \ - || [ -n "$(git status --porcelain -- "${generated_dirs[@]}")" ]; then +if [ -n "$(git status --porcelain -- "$GEN_DIR")" ]; then echo echo "generated output changed — review and commit:" - git status --short -- "${generated_dirs[@]}" + git status --short -- "$GEN_DIR" else echo "generated output is unchanged." fi diff --git a/scripts/test-codegen-out-dir.py b/scripts/test-codegen-out-dir.py deleted file mode 100644 index 23ab150f5..000000000 --- a/scripts/test-codegen-out-dir.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: MIT OR Apache-2.0 -"""Check Cargo output selection with configured paths and cached messages.""" -import json -from pathlib import Path -import subprocess -import sys -import tempfile - -script = Path(__file__).with_name("codegen-out-dir.py") -with tempfile.TemporaryDirectory() as work: - root = Path(work) - out = root / "custom build" / "wasm32-unknown-unknown/debug/build/kit-rpc-hash/out" - out.mkdir(parents=True) - (out / ".codegen").touch() - messages = [ - {"reason": "compiler-artifact", "fresh": True}, - {"reason": "build-script-executed", "out_dir": str(root)}, - {"reason": "build-script-executed", "out_dir": str(out)}, - {"reason": "build-finished", "success": True}, - ] - result = subprocess.run([sys.executable, str(script), ".codegen"], - input="".join(json.dumps(message) + "\n" for message in messages), - text=True, capture_output=True, check=True) - assert result.stdout.strip() == str(out) - result = subprocess.run([sys.executable, str(script), ".absent"], - input=json.dumps(messages[1]) + "\n", text=True, capture_output=True) - assert result.returncode != 0 -print("codegen output selection passed") From ec95a916bc3ef3b716ee69ff41414fd4c54902a6 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 25 Nov 2026 15:33:00 -0300 Subject: [PATCH 2/4] Include the generated modules by relative path --- rust/crates/kit-rpc/generated/_includes.rs | 10 +++++----- rust/crates/kit-rpc/generated/transport/_connectrpc.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rust/crates/kit-rpc/generated/_includes.rs b/rust/crates/kit-rpc/generated/_includes.rs index dca07736c..9563e5871 100644 --- a/rust/crates/kit-rpc/generated/_includes.rs +++ b/rust/crates/kit-rpc/generated/_includes.rs @@ -9,7 +9,7 @@ pub mod kit { #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] pub mod v1 { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.common.v1.mod.rs")); + include!("kit.common.v1.mod.rs"); } } #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] @@ -18,21 +18,21 @@ pub mod kit { #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] pub mod v1 { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.rpc.v1.mod.rs")); + include!("kit.rpc.v1.mod.rs"); #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] pub mod signer { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.rpc.v1.signer.mod.rs")); + include!("kit.rpc.v1.signer.mod.rs"); } #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] pub mod ssh { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.rpc.v1.ssh.mod.rs")); + include!("kit.rpc.v1.ssh.mod.rs"); } #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception)] pub mod verify { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.rpc.v1.verify.mod.rs")); + include!("kit.rpc.v1.verify.mod.rs"); } } } diff --git a/rust/crates/kit-rpc/generated/transport/_connectrpc.rs b/rust/crates/kit-rpc/generated/transport/_connectrpc.rs index 767fdb9aa..43b04a64f 100644 --- a/rust/crates/kit-rpc/generated/transport/_connectrpc.rs +++ b/rust/crates/kit-rpc/generated/transport/_connectrpc.rs @@ -9,7 +9,7 @@ pub mod grpc { #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception, impl_trait_redundant_captures)] pub mod v1 { use super::*; - include!(concat!(env!("OUT_DIR"), "/grpc.health.v1.mod.rs")); + include!("grpc.health.v1.mod.rs"); } } } @@ -22,7 +22,7 @@ pub mod kit { #[allow(non_camel_case_types, dead_code, unused_imports, unused_qualifications, clippy::derivable_impls, clippy::match_single_binding, clippy::uninlined_format_args, clippy::doc_lazy_continuation, clippy::module_inception, impl_trait_redundant_captures)] pub mod v1 { use super::*; - include!(concat!(env!("OUT_DIR"), "/kit.transport.v1.mod.rs")); + include!("kit.transport.v1.mod.rs"); } } } From e14037d164e09693e7dee454d6c9f71443340b39 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 25 Nov 2026 16:05:42 -0300 Subject: [PATCH 3/4] Drop the wasm target from the freshness job and build the generator in the Connect probe --- .github/workflows/proto.yml | 1 - scripts/check-connect-latest.sh | 2 +- scripts/check-generated-fresh.sh | 3 +-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/proto.yml b/.github/workflows/proto.yml index 144dc1749..cd8aa2d2f 100644 --- a/.github/workflows/proto.yml +++ b/.github/workflows/proto.yml @@ -60,7 +60,6 @@ jobs: - uses: ./.github/actions/setup-rust with: cache-prefix: "rust-proto-generated" - targets: "wasm32-unknown-unknown" - name: Regenerate and compare run: bash scripts/check-generated-fresh.sh diff --git a/scripts/check-connect-latest.sh b/scripts/check-connect-latest.sh index 19a393386..ad4b4529f 100755 --- a/scripts/check-connect-latest.sh +++ b/scripts/check-connect-latest.sh @@ -41,7 +41,7 @@ for package in lock['package']: print(f"{package['name']} {package['version']}") VERSIONS -crates=(-p kit-rpc -p kit-transport-connect -p kit-client) +crates=(-p kit-rpc -p kit-rpc-codegen -p kit-transport-connect -p kit-client) features='kit-rpc/transport,kit-rpc/transport-messages' "${cargo_cmd[@]}" build --locked "${crates[@]}" --features "$features" "${cargo_cmd[@]}" nextest run --locked "${crates[@]}" --features "$features" -j 3 diff --git a/scripts/check-generated-fresh.sh b/scripts/check-generated-fresh.sh index c29a9018a..ac8712fca 100755 --- a/scripts/check-generated-fresh.sh +++ b/scripts/check-generated-fresh.sh @@ -8,8 +8,7 @@ # honest: it regenerates the code and compares the result with the checked-in # files. # -# Needs `protoc` 27 or newer on PATH (or PROTOC) and the -# wasm32-unknown-unknown target. Run from any directory: +# Needs `protoc` 27 or newer on PATH (or PROTOC). Run from any directory: # bash scripts/check-generated-fresh.sh set -euo pipefail From 231ef6a235a171352828129fd33743dd702428a8 Mon Sep 17 00:00:00 2001 From: Vitor Date: Wed, 25 Nov 2026 16:06:01 -0300 Subject: [PATCH 4/4] Drop the code generators from the release package list --- scripts/release/kit-packages.golden | 7 ------- 1 file changed, 7 deletions(-) diff --git a/scripts/release/kit-packages.golden b/scripts/release/kit-packages.golden index b53ba9281..a0577d407 100644 --- a/scripts/release/kit-packages.golden +++ b/scripts/release/kit-packages.golden @@ -7,7 +7,6 @@ adler2 aead aes anstyle -anyhow arrayvec async-broadcast async-channel @@ -32,9 +31,6 @@ block-buffer block-padding blocking buffa -buffa-build -buffa-codegen -buffa-descriptor bytes cbc cc @@ -54,8 +50,6 @@ commonware-macros commonware-macros-impl concurrent-queue connectrpc -connectrpc-build -connectrpc-codegen const-oid constant_time_eq core-foundation @@ -182,7 +176,6 @@ pkg-config polling poly1305 potential_utf -prettyplease primefield primeorder proc-macro-crate