Skip to content

Fix rust_doc_test linking with native libraries#4080

Merged
UebelAndre merged 2 commits into
bazelbuild:mainfrom
patrickmscott:patrick/doc-test-linking
Jun 11, 2026
Merged

Fix rust_doc_test linking with native libraries#4080
UebelAndre merged 2 commits into
bazelbuild:mainfrom
patrickmscott:patrick/doc-test-linking

Conversation

@patrickmscott

@patrickmscott patrickmscott commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

rust_doc_test does not propagate native libraries into the final test command. This appears to only affect native libraries built by bazel rather than ones linked into crates by build.rs. This change only works with static libraries - dynamic libraries are much more complex.

For example, using the jemalloc bazel module in tikv-jemalloc-sys.

  • Add -Clink-arg for all transitive non-crates
  • Add include_linker_inputs to collect all linker inputs
  • Collect linker roots from all libraries and additional inputs

@patrickmscott patrickmscott force-pushed the patrick/doc-test-linking branch from d4d3435 to f96b19d Compare June 11, 2026 15:14
Make sure linker inputs are included in runfiles by adding a flag to
collect_inputs. Add roots for native libraries.
@patrickmscott patrickmscott force-pushed the patrick/doc-test-linking branch from f96b19d to 2a51335 Compare June 11, 2026 15:15

@UebelAndre UebelAndre left a comment

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.

Thanks! Just one question!

Comment thread rust/private/rustc.bzl
use_direct_link_driver (bool): Whether the linker is a direct driver (e.g. `ld`, `wasm-ld`) vs a wrapper (e.g. `clang`, `gcc`).
include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
"""
if crate_type in ["lib", "rlib"]:

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.

This seems somewhat risky, can you expand on why this is removed?

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.

_add_native_link_flags is called by construct_arguments and is already guarded by

if ("link" in emit and crate_info.type not in ["rlib", "lib"]) or add_flags_for_binary

In the rustdoc test, add_flags_for_binary was already true, but then _add_native_link_flags bails early. I thought it was redundant to check again for ["rlib", "lib"] and seemed like the right thing to do if add_flags_for_binary is true.

@UebelAndre UebelAndre left a comment

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.

Thanks!

@UebelAndre UebelAndre added this pull request to the merge queue Jun 11, 2026
Merged via the queue into bazelbuild:main with commit 0d36c00 Jun 11, 2026
3 checks passed
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.

2 participants