Skip to content

process_wrapper: Bazel persistent worker mode with rustc incremental state - #7

Closed
ellgorrell wants to merge 1 commit into
mainfrom
rustc-worker
Closed

ellgorrell wants to merge 1 commit into
mainfrom
rustc-worker

Conversation

@ellgorrell

Copy link
Copy Markdown

Summary

Opt-in Bazel persistent worker mode for process_wrapper, with rustc incremental compilation state kept under the output base. Enabled with --@rules_rust//rust/settings:experimental_persistent_worker=true; default off, so nothing changes for builds without it, and no --strategy flag is needed.

Starlark (rust/private/rustc.bzl)

With the setting on, the Rustc and RustcMetadata actions of main-repository crates advertise supports-workers (proto protocol) and lay their command line out for a worker. Crates from external repositories are left alone so their remote cache entries still match every other build.

  • The process wrapper's own flags and the -- rustc part go into --flagfile= param files; rustc's flags stay in the @ param file.
  • The per-crate environment (CARGO_* etc.) travels in the request as --env KEY=VALUE instead of the action env. Bazel keys worker pools by startup arguments and environment, so the startup command is the bare process wrapper and every crate shares one pool per mnemonic.
  • In non-opt builds (and not the single-object cc_common.link layout) actions get --rustc-incremental true, no-remote-cache and no-remote-exec, which routes them to the worker under Bazel's default strategy order (remote, then worker). An incrementally compiled action is never a useful cache entry, and the lookup alone costs seconds for a large input tree.

process_wrapper

  • --persistent_worker enters a loop reading length-delimited WorkRequest protos from stdin. Each request runs as a one-shot child process_wrapper <startup args> --rustc-incremental-dir <output base>/rustc-incremental <request arguments>, so a request behaves exactly like the same action run without a worker. Exit code and output go back as a WorkResponse.
  • A one-shot run expands --flagfile= arguments itself (Bazel only does so for workers) and applies --env pairs before --env-file files.
  • With both --rustc-incremental true and the worker's --rustc-incremental-dir, it compiles with -Cincremental=<dir>/<configuration> (the configuration segment of rustc's --out-dir, so target and exec builds of one crate do not share a session). Without the directory the flag is inert, so remote, sandboxed and local one-shot executions are unchanged. The metadata action never compiles incrementally.
  • The worker finds the output base as the grandparent of its execution root. The existing ${output_base} heuristic in options.rs resolves to the execution root itself under Bazel 9.

tools/rust_analyzer

  • flycheck: a saved file owned by several targets now resolves to a non-test one, both in the file-to-label sidecar that discover writes and in the bazel query fallback. A rust_test(crate = ":bin") owns the same sources as the binary, and building it compiled the crate twice for the same diagnostics.
  • setup: takes --bazel (default bazel), like discover and flycheck, for the bazel info it runs to pre-populate flycheck's cache and for --clean.

The worker protocol code is adapted from bazelbuild#4184 (Angus Lees). The differences are the single worker pool (flag files and --env instead of per-action startup arguments) and the incremental state kept under the output base instead of a per-worker temporary directory, so it survives worker restarts.

Test plan

  • bazel test //util/process_wrapper/... //test/process_wrapper/... //tools/rust_analyzer/... passes (14/14). Covers worker protocol framing, flag file expansion, --env, incremental directory selection, option precedence, an end-to-end driver speaking the proto protocol to process_wrapper --persistent_worker, and flycheck's target choice.
  • bazel build //util/process_wrapper:process_wrapper //test/unit/... passes with the setting off.
  • Manual test build in the monorepo with the setting on (building the unit test suite itself with the setting on fails analysis in //test/unit/apple_deployment_target, which asserts on action.env; the worker layout moves that env into request arguments by design).

🤖 Generated with Claude Code

…state

Opt-in with --@rules_rust//rust/settings:experimental_persistent_worker=true
(default off: nothing changes for builds without it); no --strategy is needed.

Starlark (rust/private/rustc.bzl): with the setting on, the Rustc and RustcMetadata
actions of main-repository crates advertise supports-workers (proto protocol) and
lay their command line out for a worker (crates of external repositories are left
alone: the worker layout is a different command line, and theirs must stay the one
every other build gives them, so that their remote cache entries still match): the process wrapper's own flags and the `-- rustc` part go into
`--flagfile=` param files, rustc's flags stay in the `@` param file, and the
per-crate environment (CARGO_* etc.) travels in the request as `--env KEY=VALUE`
instead of in the action's env. Bazel keys worker pools by startup arguments and
environment, so this makes the startup command the bare process wrapper and every
crate shares one pool per mnemonic. In non-opt builds (and not the single-object
cc_common.link layout) they get `--rustc-incremental true`, `no-remote-cache` (an
incrementally compiled action is never a useful cache entry, and the lookup alone
costs seconds for a large input tree) and `no-remote-exec`, which routes them to
the worker under Bazel's default strategy order (remote, then worker).

process_wrapper: `--persistent_worker` enters a loop reading length-delimited
WorkRequest protos from stdin; each request is run as a one-shot child
`process_wrapper <startup args> --rustc-incremental-dir <output base>/rustc-incremental
<request arguments>` (the wrapper re-executes itself, so a request behaves exactly
like the same action run without a worker), and its exit code and output go back as
a WorkResponse. A one-shot run expands `--flagfile=` arguments itself (Bazel only
does so for workers), applies `--env` pairs before `--env-file` files, and, when it
has both `--rustc-incremental true` and the worker's `--rustc-incremental-dir`,
compiles with `-Cincremental=<dir>/<configuration>` (the configuration segment of
rustc's --out-dir, so target and exec builds of one crate do not share a session).
Without the directory the flag is inert, so remote, sandboxed and local one-shot
executions of the same action are unchanged; the metadata action (rustc stopped at
the rmeta, no session to reuse) never compiles incrementally. The worker finds the
output base as the grandparent of its execution root (`<output base>/execroot/<ws>`);
the existing `${output_base}` heuristic in options.rs resolves to the execution root
itself under Bazel 9, where `external` is a directory inside it.

flycheck (tools/rust_analyzer): a saved file owned by several targets now resolves
to a non-test one, in the file-to-label sidecar discover writes (lib.rs) and in the
`bazel query` fallback (`--output=label_kind`, first non-`*_test` kind). A
`rust_test(crate = ":bin")` owns the same sources as the binary, and building it
compiled the crate twice, once more for the test harness, for the same diagnostics.

setup (tools/rust_analyzer/bin/setup.rs) takes `--bazel` (default `bazel`), like
discover and flycheck, for the `bazel info` it runs to pre-populate flycheck's
cache and the `bazel clean --expunge` of `--clean`. Run with the `bazel` on PATH
against an output base that a wrapper serves with its own startup options, that
`info` restarted the wrapper's server, and the wrapper's next command restarted
it again.

The worker protocol code is adapted from bazelbuild#4184 (Angus Lees);
the differences are the single worker pool (flag files and --env instead of
per-action startup arguments) and the incremental state kept under the output base
instead of a per-worker temporary directory, so it survives worker restarts.

Tests: process_wrapper unit tests (worker protocol framing, flag file expansion,
--env, incremental directory selection, option precedence), a driver that speaks
the proto protocol to `process_wrapper --persistent_worker` end to end, and a unit
test for flycheck's target choice; `bazel test //util/process_wrapper/...
//test/process_wrapper/... //tools/rust_analyzer/...` pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit bc39933. Configure here.

.arg("--rustc-incremental-dir")
.arg(&incremental_dir)
.args(&request.arguments)
.output();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worker re-exec hits argument length limits

High Severity

The worker expands every rustc flag from @ param files into the WorkRequest, then re-execs process_wrapper (and rustc) with those flags as argv. One-shot runs keep @ files so rustc reads them from disk. On Windows CreateProcess caps the command line at 32K characters, and large crates can also exceed Linux ARG_MAX, so enabling the worker fails those compiles.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bc39933. Configure here.

out
out.into_iter()
.map(|(root, (label, _))| (root, label))
.collect()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Library sidecar still maps to tests

Medium Severity

build_file_label_map prefers a non-test label using spec.is_test, but it runs after consolidate_crate_specs, which merges a library and its rust_test into one spec and copies the test's build label for codelens. The sidecar therefore still maps lib.rs to the test target, so flycheck compiles the crate twice for the library case the comments call out.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bc39933. Configure here.

bool_flag(
name = "experimental_persistent_worker",
build_setting_default = False,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worker flag missing universal scope

Medium Severity

experimental_persistent_worker is a bool_flag without scope = "universal". Nearby rustc flags such as pipelined_compilation set that so they apply in exec configuration. Without it, main-repo proc macros and build scripts analyzed in exec config keep the default false and never join the worker pool or reuse incremental state.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bc39933. Configure here.

@ellgorrell ellgorrell closed this Nov 26, 2026
@ellgorrell
ellgorrell deleted the rustc-worker branch November 26, 2026 23:59
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