Clarification on rustc --extern= resolver

⚓ rust    📅 2025-07-03    👤 surdeus    👁️ 3      

surdeus

Hi, I recently submitted a PR to rules_rust project which allows for compiling rust code via bazel (without cargo)

My PR is a change to the behaviour around coverage instrumentation only adding the flags when the target is requested for coverage. So dependencies don't get coverage, making it so we can reuse the cache for 99% of the build graph and only rebuilt our crates by adding coverage markers there.

In my project this works fine and we get the correct coverage output and everything seems fine compared to cargo llvm-cov however the rules_rust project fails on some unit tests and I am not exactly sure why as it goes against my intuition of how rustc behaves with --extern=crate=rlib flags.

As Noted here,

rustc \
	'test/unit/pipelined_compilation/bin.rs '--crate-name=bin' '--crate-type=bin' '--error-format=human' \
	'--out-dir=bazel-out/k8-fastbuild-ST-1c8a4433bbf5/bin/test/unit/pipelined_compilation' \
	'--codegen=opt-level=0' '--codegen=debuginfo=0' '--codegen=strip=none' '--remap-path-prefix=${pwd}=' \
	'--emit=link=bazel-out/k8-fastbuild-ST-1c8a4433bbf5/bin/test/unit/pipelined_compilation/bin' '--emit=dep-info'  \
	'--color=always' '--target=x86_64-unknown-linux-gnu'  \
	'-L rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib' '--edition=2021' '-Cembed-bitcode=no' \
	'--codegen=linker=/usr/bin/gcc' '--codegen=link-arg=--coverage' '--codegen=link-arg=-fuse-ld=gold' \
	'--codegen=link-arg=-B/usr/bin' '--codegen=link-arg=-Wl,-no-as-needed' '--codegen=link-arg=-Wl,-z,relro,-z,now'  \
	'--codegen=link-arg=-pass-exit-codes' '--codegen=link-arg=-Wl,--push-state,-as-needed' '--codegen=link-arg=-lstdc++' \
	'--codegen=link-arg=-Wl,--pop-state' '--codegen=link-arg=-Wl,--push-state,-as-needed' '--codegen=link-arg=-lm' \
	'--codegen=link-arg=-Wl,--pop-state' \
	'--extern=second=./bin/test/unit/pipelined_compilation/libsecond-416833743.rlib' \
	'-Ldependency=bazel-out/k8-fastbuild-ST-1c8a4433bbf5/bin/test/unit/pipelined_compilation' \
	'-Ldependency=bazel-out/k8-opt-exec-ST-742c84c296cd/bin/test/unit/pipelined_compilation' \
	'--codegen=instrument-coverage' \
	'--sysroot=./rust_toolchain' \
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error[E0463]: can't find crate for `second`
 --> test/unit/pipelined_compilation/bin.rs:1:5
  |
1 | use second::fun;
  |     ^^^^^^ can't find crate

error: aborting due to 1 previous error

The rustc invocation provides an --extern path for the second crate however when it tries to build the bin.rs file it is unable to find the crate, I suspect it finds the crate just fine but I am missing a flag or some dependency to the rlib but its hard to tell what that is exactly (likely some coverage llvm lib).

So my question is: Is there any way to meaningfully debug what rustc is failing to find or do or what it attempted to do and its steps here so I can debug this issue?

1 post - 1 participant

Read full topic

🏷️ rust_feed