Skip to content

Add test sharding support for rust_test#3774

Open
brianduff wants to merge 3 commits into
bazelbuild:mainfrom
brianduff:push-tusnxzxumrtz
Open

Add test sharding support for rust_test#3774
brianduff wants to merge 3 commits into
bazelbuild:mainfrom
brianduff:push-tusnxzxumrtz

Conversation

@brianduff
Copy link
Copy Markdown
Contributor

Problem

rust_test targets don't support Bazel's native test sharding. When shard_count is set on a rust_test:

  • Without --incompatible_check_sharding_support: all tests run N times (once per shard)
  • With --incompatible_check_sharding_support: the test fails because Rust's libtest harness doesn't read TEST_SHARD_INDEX/TEST_TOTAL_SHARDS environment variables

Solution

This PR adds opt-in sharding support via a new experimental_enable_sharding attribute. When enabled, tests are executed via a wrapper script that:

  1. Detects sharding via TEST_TOTAL_SHARDS environment variable
  2. Touches TEST_SHARD_STATUS_FILE to advertise sharding support to Bazel
  3. Enumerates tests using libtest's --list --format terse flag
  4. Partitions tests by index % TEST_TOTAL_SHARDS == TEST_SHARD_INDEX
  5. Runs only the tests assigned to the current shard using --exact

Usage

rust_test(
    name = "my_test",
    srcs = ["my_test.rs"],
    shard_count = 4,
    experimental_enable_sharding = True,
)

@brianduff brianduff marked this pull request as draft December 12, 2025 21:46
Copy link
Copy Markdown
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

This generally looks good to me (though I don't have enough batch skills to really verify the .bat file). One thought on provider organisation :)

Comment thread rust/private/rust.bzl Outdated

# Get the test binary from CrateInfo - it's the output of the compiled test
crate_info_provider = None
for p in providers:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's do something like edit rustc_compile_action to return a dict of provider types to providers, and then do a key lookup, rather than sniffing an ordered list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

much better - done.

@brianduff brianduff force-pushed the push-tusnxzxumrtz branch 2 times, most recently from f6ec6ee to a7f30a5 Compare December 16, 2025 17:47
@brianduff brianduff force-pushed the push-tusnxzxumrtz branch 8 times, most recently from 47f7781 to c23d0dc Compare December 16, 2025 18:56
@brianduff brianduff marked this pull request as ready for review December 16, 2025 19:09
@brianduff
Copy link
Copy Markdown
Contributor Author

Remaining test breakages appear to be pre-existing issues unrelated to this change.

The windows .bat file was a pain to test, but was able to get it to work eventually. I also have a branch where I use a small rust util to launch the tests - lemme know if we should explore that either here or as a followup.

@dzbarsky
Copy link
Copy Markdown
Contributor

I would love to have the util be a rust wrapper. Seems like that would avoid multiple implementations and a bash dependency

Copy link
Copy Markdown
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Once upon a time we had a test wrapper and it ended up being problematic for debuggers. I'm very nervous about introducing this in this way. Is there not a test crate rules_rust could provide that would enable sharding instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think for cross-platform consistency, could this be a rust binary?

Comment thread rust/private/rust.bzl
E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`.
"""),
),
"experimental_enable_sharding": attr.bool(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this not be a build flag similar to these?

experimental_link_std_dylib()
experimental_per_crate_rustc_flag()
experimental_use_cc_common_link()
experimental_use_coverage_metadata_files()
experimental_use_global_allocator()

@shrey4796
Copy link
Copy Markdown

Any update on this ? we could really use this feature

@bolinfest
Copy link
Copy Markdown

@brianduff @illicitonion good to see you again! We are starting to rely more heavily on Bazel for building https://github.com/openai/codex/ and are very interested in this change.

I would be happy to help shepherd this through if I can!

@dzbarsky
Copy link
Copy Markdown
Contributor

dzbarsky commented Apr 2, 2026

@brianduff @illicitonion good to see you again! We are starting to rely more heavily on Bazel for building https://github.com/openai/codex/ and are very interested in this change.

I would be happy to help shepherd this through if I can!

Our current thinking is the best path forward would be to either patch libtest harness or provide an alternate binary launcher that reuses libtest but adds support for the sharding env vars. (That's basically what Go does as well). Staying in-process should avoid bad interactions with debugger

bolinfest added a commit to bolinfest/rules_rust that referenced this pull request Apr 15, 2026
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. This keeps rustc_compile_action's provider-list API intact and updates rust_test to swap its executable for a generated sharding wrapper when experimental_enable_sharding is set.

Co-authored-by: Brian Duff <bduff@linkedin.com>
Co-authored-by: Codex <noreply@openai.com>
bolinfest added a commit to bolinfest/rules_rust 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 to hermeticbuild/rules_rust 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 to hermeticbuild/rules_rust 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 to hermeticbuild/rules_rust 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 to hermeticbuild/rules_rust 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 to hermeticbuild/rules_rust 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 to hermeticbuild/rules_rust that referenced this pull request Apr 25, 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 to hermeticbuild/rules_rust that referenced this pull request Apr 30, 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 to hermeticbuild/rules_rust that referenced this pull request May 3, 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 to hermeticbuild/rules_rust that referenced this pull request May 3, 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 to hermeticbuild/rules_rust that referenced this pull request May 5, 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 to hermeticbuild/rules_rust that referenced this pull request May 11, 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 to hermeticbuild/rules_rust that referenced this pull request May 15, 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>
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.

6 participants