Skip to content

Exclude Rust test harness stdlib crates from non-test CcInfo - #4466

Open
vaibhav-1207 wants to merge 1 commit into
bazelbuild:mainfrom
vaibhav-1207:filter-test-stdlib-ccinfo
Open

vaibhav-1207 wants to merge 1 commit into
bazelbuild:mainfrom
vaibhav-1207:filter-test-stdlib-ccinfo

Conversation

@vaibhav-1207

Copy link
Copy Markdown
Contributor

Previously, make_libstd_and_allocator_ccinfo unconditionally bundled StdLibInfo.test_files into the standard libstd CcInfo exported by rust_library targets and linked into non-test binaries. In addition, _rust_stdlib_filegroup_impl matched test_files via substring matching ("test" in f.basename), which left test's exclusive transitive dependencies (getopts and unicode_width) in between_core_and_std_files and could misclassify unrelated crates containing "test".

This change separates the test harness sysroot crates from non-test CcInfo while preserving linkage for all test targets and --test binaries:

  • Partition test, getopts, and unicode_width into StdLibInfo.test_files in topological link order using exact lib<crate>- / lib<crate>. prefix matching (retaining proc_macro and rustc_literal_escaper in between_core_and_std_files since non-test crates can depend on them).
  • Remove test_files from make_libstd_and_allocator_ccinfo and add an allocator-independent make_libtest_ccinfo exposed on ToolchainInfo as libtest_ccinfo.
  • Collect crate_info.deps before toolchain.libtest_ccinfo, _get_std_and_alloc_info(...), and toolchain.stdlib_linkflags in _collect_dep_cc_infos, and include libtest_ccinfo under experimental_use_cc_common_link whenever crate_info.is_test is true or --test is passed to rustc (via rustc_flags, caller rust_flags, or collect_extra_rustc_flags) and is_no_std is false. This preserves the archive link order deps -> test -> getopts [-> unicode_width] -> std -> ... -> core -> compiler_builtins -> alloc -> allocator.
  • Add unit tests in test/unit/stdlib and an end-to-end rust_binary(rustc_flags = ["--test"]) integration test in test/integration/cc_common_link.

Assisted-by: Gemini

Previously, `make_libstd_and_allocator_ccinfo` unconditionally bundled
`StdLibInfo.test_files` into the standard `libstd` `CcInfo` exported by
`rust_library` targets and linked into non-test binaries. In addition,
`_rust_stdlib_filegroup_impl` matched `test_files` via substring matching
(`"test" in f.basename`), which left `test`'s exclusive transitive
dependencies (`getopts` and `unicode_width`) in
`between_core_and_std_files` and could misclassify unrelated crates
containing `"test"`.

This change separates the test harness sysroot crates from non-test
`CcInfo` while preserving linkage for all test targets and `--test`
binaries:
- Partition `test`, `getopts`, and `unicode_width` into
  `StdLibInfo.test_files` in topological link order using exact
  `lib<crate>-` / `lib<crate>.` prefix matching (retaining `proc_macro`
  and `rustc_literal_escaper` in `between_core_and_std_files` since
  non-test crates can depend on them).
- Remove `test_files` from `make_libstd_and_allocator_ccinfo` and add an
  allocator-independent `make_libtest_ccinfo` exposed on `ToolchainInfo`
  as `libtest_ccinfo`.
- Collect `crate_info.deps` before `toolchain.libtest_ccinfo`,
  `_get_std_and_alloc_info(...)`, and `toolchain.stdlib_linkflags` in
  `_collect_dep_cc_infos`, and include `libtest_ccinfo` under
  `experimental_use_cc_common_link` whenever `crate_info.is_test` is
  true or `--test` is passed to `rustc` (via `rustc_flags`, caller
  `rust_flags`, or `collect_extra_rustc_flags`) and `is_no_std` is
  false. This preserves the archive link order
  `deps -> test -> getopts [-> unicode_width] -> std -> ... -> core ->
  compiler_builtins -> alloc -> allocator`.
- Add unit tests in `test/unit/stdlib` and an end-to-end
  `rust_binary(rustc_flags = ["--test"])` integration test in
  `test/integration/cc_common_link`.

Assisted-by: Gemini
@vaibhav-1207
vaibhav-1207 force-pushed the filter-test-stdlib-ccinfo branch from 6ce9f25 to 5bb7ff1 Compare November 26, 2026 06:45
@krasimirgg
krasimirgg self-requested a review November 26, 2026 09:35

This branch has not been deployed

No deployments
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