Skip to content

[codex] Add rust_test sharding support#13

Merged
dzbarsky merged 1 commit intohermeticbuild:mainfrom
bolinfest:codex/add-rust-test-sharding
Apr 15, 2026
Merged

[codex] Add rust_test sharding support#13
dzbarsky merged 1 commit intohermeticbuild:mainfrom
bolinfest:codex/add-rust-test-sharding

Conversation

@bolinfest
Copy link
Copy Markdown

@bolinfest bolinfest commented Apr 15, 2026

Summary

Ports the rust_test sharding feature from bazelbuild#3774 into the hermeticbuild fork.

This adds an opt-in experimental_enable_sharding attribute for rust_test. When enabled with the libtest harness, the rule swaps the test executable for a generated wrapper script that:

  • touches TEST_SHARD_STATUS_FILE so Bazel recognizes sharding support
  • enumerates libtest tests with --list --format terse
  • partitions tests with TEST_SHARD_INDEX and TEST_TOTAL_SHARDS
  • runs the selected tests with --exact

The wrapper partitions tests deterministically from libtest's listing order. It keeps a zero-based index for every listed test and runs a test in a shard when:

index % TEST_TOTAL_SHARDS == TEST_SHARD_INDEX

For example, with 4 shards, tests at indices 0, 4, 8, ... run in shard 0; indices 1, 5, 9, ... run in shard 1; and so on.

The implementation is adapted for this fork by making rustc_compile_action() return a dict of named providers. Rule implementations convert that dict back to a provider list at their return boundary, while rust_test can look up and replace DefaultInfo directly when sharding is enabled.

Expected Codex Usage

Once Codex consumes this fork update, the plan is to expose a small per-target sharding config in the Codex codex_rust_crate macro rather than sharding every Rust test target. The initial use case is the large codex-core test targets:

codex_rust_crate(
    name = "core",
    crate_name = "codex_core",
    # ...
    test_shard_counts = {
        "core-all-test": 8,
        "core-unit-tests": 8,
    },
)

For generated integration-test targets like core-all-test, the macro can pass both experimental_enable_sharding = True and shard_count = 8 directly to the generated rust_test.

For core-unit-tests, Codex wraps the Rust unit-test binary in a workspace_root_test launcher so tests execute from the repo root. That means the outer workspace_root_test target needs shard_count = 8 so Bazel schedules separate shards, while the inner core-unit-tests-bin rust_test needs experimental_enable_sharding = True so the executable that the launcher runs honors TEST_SHARD_INDEX and TEST_TOTAL_SHARDS.

Validation

  • bazel test //test/unit/test_sharding:test_sharding_test_suite
  • bazel test --cache_test_results=no --incompatible_check_sharding_support //test/unit/test_sharding:sharded_integration_test
  • bazel test //test/unit/test_sharding:test_sharding_test_suite //test/unit/consistent_crate_name:consistent_crate_name_test_suite //test/unit/force_all_deps_direct:force_all_deps_direct_test_suite //test/unit/pipelined_compilation:pipelined_compilation_test_suite
  • bazel build //rust/private:bzl_lib
  • (cd extensions/prost && bazel build //private:bzl_lib)
  • (cd extensions/wasm_bindgen && bazel build //private:bzl_lib)

Comment thread rust/private/rust.bzl

return providers

def _maybe_wrap_sharded_test(ctx, providers, toolchain):
Copy link
Copy Markdown

@tyler-french tyler-french Apr 15, 2026

Choose a reason for hiding this comment

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

would it be cleaner to have rustc_compile_action() return a dict of named providers instead of a positional list? Then rust_test can look up DefaultInfo / crate_info / test_crate_info directly instead of sniffing provider shape/order.

providers = rustc_compile_action(...)

default_info = providers["DefaultInfo"]
crate_info = providers.get("crate_info") or providers.get("test_crate_info")
if hasattr(crate_info, "crate"):
    crate_info = crate_info.crate

test_binary = crate_info.output
providers["DefaultInfo"] = DefaultInfo(
    files = default_info.files,
    runfiles = default_info.default_runfiles.merge(ctx.runfiles(files = [test_binary])),
    executable = wrapper,
)

return list(providers.values())

Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. This updates rustc_compile_action to return a dict of named providers so rust_test can replace DefaultInfo without provider-shape sniffing when experimental_enable_sharding is set.

Co-authored-by: Brian Duff <bduff@linkedin.com>
Co-authored-by: Codex <noreply@openai.com>
@bolinfest bolinfest force-pushed the codex/add-rust-test-sharding branch from b0a7f0d to 1f3dd3b Compare April 15, 2026 20:05
@bolinfest bolinfest marked this pull request as ready for review April 15, 2026 20:14
@bolinfest bolinfest requested a review from tyler-french April 15, 2026 20:14
@dzbarsky dzbarsky merged commit 1478052 into hermeticbuild:main Apr 15, 2026
dzbarsky pushed a commit that referenced this pull request Apr 15, 2026
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. This updates rustc_compile_action to return a dict of named providers so rust_test can replace DefaultInfo without provider-shape sniffing when experimental_enable_sharding is set.

Co-authored-by: Brian Duff <bduff@linkedin.com>
Co-authored-by: Codex <noreply@openai.com>
dzbarsky pushed a commit that referenced this pull request Apr 16, 2026
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. This updates rustc_compile_action to return a dict of named providers so rust_test can replace DefaultInfo without provider-shape sniffing when experimental_enable_sharding is set.

Co-authored-by: Brian Duff <bduff@linkedin.com>
Co-authored-by: Codex <noreply@openai.com>
dzbarsky pushed a commit that referenced this pull request Apr 16, 2026
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. The implementation wraps rust_test executables when experimental_enable_sharding is set while keeping rustc_compile_action's existing provider-list API for internal and extension callers.

rust_test now scans the returned providers to replace DefaultInfo for the wrapper, so extensions such as prost and wasm-bindgen continue to consume rustc_compile_action without API churn.

Co-authored-by: Brian Duff <bduff@linkedin.com>

Co-authored-by: Codex <noreply@openai.com>
dzbarsky pushed a commit that referenced this pull request Apr 16, 2026
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. The implementation wraps rust_test executables when experimental_enable_sharding is set while keeping rustc_compile_action's existing provider-list API for internal and extension callers.

rust_test now scans the returned providers to replace DefaultInfo for the wrapper, so extensions such as prost and wasm-bindgen continue to consume rustc_compile_action without API churn.

Co-authored-by: Brian Duff <bduff@linkedin.com>

Co-authored-by: Codex <noreply@openai.com>
zbarsky-openai added a commit to openai/codex that referenced this pull request Apr 17, 2026
## Why
This branch brings the Bazel module pins for `rules_rs` and `llvm` up to
the latest BCR releases and aligns the root direct dependencies with the
versions the module graph already resolves to.

That gives us a few concrete wins:
- picks up newer upstream fixes in the `rules_rs` / `rules_rust` stack,
including work around repo-rule nondeterminism and default Cargo binary
target generation
- picks up test sharding support from the newer `rules_rust` stack
([hermeticbuild/rules_rust#13](hermeticbuild/rules_rust#13))
- picks up newer built-in knowledge for common system crates like
`gio-sys`, `glib-sys`, `gobject-sys`, `libgit2-sys`, and `libssh2-sys`,
which gives us a future path to reduce custom build-script handling
- reduces local patch maintenance by dropping fixes that are now
upstream and rebasing the remaining Windows patch stack onto a newer
upstream base
- removes the direct-dependency warnings from `bazel-lock-check` by
making the root pins match the resolved graph

## What Changed
- bump `rules_rs` from `0.0.43` to `0.0.58`
- bump `llvm` from `0.6.8` to `0.7.1`
- bump `bazel_skylib` from `1.8.2` to `1.9.0` so the root direct dep
matches the resolved graph
- regenerate `MODULE.bazel.lock` for the updated module graph
- refresh the remaining Windows-specific patch stack against the newer
upstream sources:
  - `patches/rules_rs_windows_gnullvm_exec.patch`
  - `patches/rules_rs_windows_exec_linker.patch`
  - `patches/rules_rust_windows_exec_std.patch`
  - `patches/rules_rust_windows_msvc_direct_link_args.patch`
- remove patches that are no longer needed because the underlying fixes
are upstream now:
  - `patches/rules_rs_delete_git_worktree_pointer.patch`
  - `patches/rules_rust_repository_set_exec_constraints.patch`

## Validation
- `just bazel-lock-update`
- `just bazel-lock-check`

---------

Co-authored-by: Codex <noreply@openai.com>
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.

3 participants