bazelbuild / bazelbuild/rules_rust
Can't build rust library with bazel coverage
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
## Background
We have a c++ project, it can use bazel very well to help we manage the dependency and compilation process. We recently tried to introduce rust. First of all, rust will be used in test cases, but we found that it seems to be compatible with `bazel coverage`. So We hope we can found some help here.
## A small case
I have try to add a repo show my problem: https://github.com/rainingmaster/bazel_combine
## Env
```
# bazel version
Build label: 3.7.2
```
```
# rustc -vV
rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-unknown-linux-gnu
release: 1.57.0
LLVM version: 13.0.0
```
And this is the [rules_rust's version](https://github.com/rainingmaster/bazel_combine/blob/master/WORKSPACE#L11)
## Details
I have test in these ways:
1. Run with static library compile by local rustc, and [here should be static](https://github.com/rainingmaster/bazel_combine/blob/master/test/BUILD#L10)
2. Run with library compile with bazel and rules_rust, and [here should be my_lib](https://github.com/rainingmaster/bazel_combine/blob/master/test/BUILD#L10), and without dependencies, like [here](https://github.com/rainingmaster/bazel_combine/blob/master/test/lib/BUILD#L11)
3. Run with library compile with bazel and rules_rust, and with local dependencies, [here should be `: gap_lib` only](https://github.com/rainingmaster/bazel_combine/blob/master/test/lib/BUILD#L11)
4. Run with library compile with bazel and rules_rust, and with remote dependencies, [here should be `//test/lib/cargo:rand_core` only](https://github.com/rainingmaster/bazel_combine/blob/master/test/lib/BUILD#L11)
And only 1, 2 can generate coverage well, 3, 4 will run successfully, but lose the coverage data. And [here](https://github.com/rainingmaster/bazel_combine/blob/master/Makefile#L7) is my command.
I think there should be some difference between build by bazel(and rules_rust) and local rustc, so here is more details:
Following are the verbose for situtation 2
```
# cargo build --release --verbose
Compiling rand_core v0.4.2
Running `rustc --crate-name rand_core /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_core-0.4.2/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=4b621f57b9b87dff -C extra-filename=-4b621f57b9b87dff --out-dir /root/code.byted.org/bazel_combine/test/lib/target/release/deps -L dependency=/root/code.byted.org/bazel_combine/test/lib/target/release/deps --cap-lints allow`
Compiling my_lib v0.1.0 (/root/code.byted.org/bazel_combine/test/lib)
Running `rustc --crate-name my_lib --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type staticlib --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=f1ade3c1cdf081e6 -C extra-filename=-f1ade3c1cdf081e6 --out-dir /root/code.byted.org/bazel_combine/test/lib/target/release/deps -L dependency=/root/code.byted.org/bazel_combine/test/lib/target/release/deps --extern rand_core=/root/code.byted.org/bazel_combine/test/lib/target/release/deps/librand_core-4b621f57b9b87dff.rlib`
Finished release [optimized] target(s) in 0.97s
```
Following are output with `--subcommand` in bazel
```
SUBCOMMAND: # @lib__rand_core__0_4_2//:rand_core [action 'Compiling Rust rlib rand_core v0.4.2 (5 files)', configuration: 0596e8f9bc311ea7666a14e49e00a73440c228e22c1cfc6fb7 5785b9733ecc88, execution platform: @local_config_platform//:host]
(cd /root/.cache/bazel/_bazel_root/df0b37849b08f019d1e16ebc0f568ea0/execroot/__main__ && \
exec env - \
CARGO_CFG_TARGET_ARCH=x86_64 \
CARGO_CFG_TARGET_OS=linux \
CARGO_CRATE_NAME=rand_core \
CARGO_MANIFEST_DIR='${pwd}/external/lib__rand_core__0_4_2' \
CARGO_PKG_AUTHORS='' \
CARGO_PKG_DESCRIPTION='' \
CARGO_PKG_HOMEPAGE='' \
CARGO_PKG_NAME=rand_core \
CARGO_PKG_VERSION=0.4.2 \
CARGO_PKG_VERSION_MAJOR=0 \
CARGO_PKG_VERSION_MINOR=4 \
CARGO_PKG_VERSION_PATCH=2 \
CARGO_PKG_VERSION_PRE='' \
SYSROOT=../rust_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib \
bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_rust/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' -- external/rust_linux_x86_64/bin/c external/lib__ran d_core__0_4_2/src/lib.rs '--crate-name=rand_core' '--crate-type=rlib' '--error-format=human' '--codegen=metadata=-1079659908' ut-dir=bazel-out/k8-fastbuild/bin/external /lib__rand_core__0_4_2' '--codegen=extra-filename=-1079659908' '--codegen=opt-level=0' odegen=debuginfo=0' '--remap-path-prefix=${pwd}=.' '--emit=dep-info,link' '--colo r=always' '--target=x86_64-unknown-linux-gnu' -L external/_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib '--cap-lints=allow' '--codegen=linker=/usr/lib/llvm-11/ bin/clang' --codegen k-args=-fprofile-instr-generate -fuse-ld=/usr/bin/ld.gold -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/lib/llvm-11/bin -lstdc++ -lm -ldl')
SUBCOMMAND: # //test/lib:my_lib [action 'Compiling Rust staticlib my_lib (1 files)', configuration: 0596e8f9bc311ea7666a14e49e00a73440c228e22c1cfc6fb75785b9733ecc88, execut ion platform: @local_config_platform//:host]
(cd /root/.cache/bazel/_bazel_root/df0b37849b08f019d1e16ebc0f568ea0/execroot/__main__ && \
exec env - \
CARGO_CFG_TARGET_ARCH=x86_64 \
CARGO_CFG_TARGET_OS=linux \
CARGO_CRATE_NAME=my_lib \
CARGO_MANIFEST_DIR='${pwd}/test/lib' \
CARGO_PKG_AUTHORS='' \
CARGO_PKG_DESCRIPTION='' \
CARGO_PKG_HOMEPAGE='' \
CARGO_PKG_NAME=my_lib \
CARGO_PKG_VERSION=0.0.0 \
CARGO_PKG_VERSION_MAJOR=0 \
CARGO_PKG_VERSION_MINOR=0 \
CARGO_PKG_VERSION_PATCH=0 \
CARGO_PKG_VERSION_PRE='' \
SYSROOT=../rust_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib \
bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_rust/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' -- external/rust_linux_x86_64/bin/rustc test/lib/src/lib. rs '--crate-name=my_lib' '--crate-type=staticlib' '--error-format=human' '--codegen=metadata=-2043668924' '--out-dir=bazel-out/k8-fastbuild/bin/test/lib' '--codegen=extra-f ilename=-2043668924' '--codegen=opt-level=0' '--codegen=debuginfo=0' '--remap-path-prefix=${pwd}=.' '--emit=dep-info,link' '--color=always' '--target=x86_64-unknown-linux-g nu' -L external/rust_linux_x86_64/lib/rustlib/x86_64-unknown-linux-gnu/lib '--edition=2018' '--codegen=linker=/usr/lib/llvm-11/bin/clang' --codegen 'link-args=-fprofile-ins tr-generate -fuse-ld=/usr/bin/ld.gold -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/lib/llvm-11/bin -lstdc++ -lm -ldl -ldl -lpthread' '--extern=rand_core=bazel-out/k8-fastbu ild/bin/external/lib__rand_core__0_4_2/librand_core-1079659908.rlib' '-Ldependency=bazel-out/k8-fastbuild/bin/external/lib__rand_core__0_4_2')
```
** In fact I have try to use bazel's args and build with local rustc, to found the difference, but it is still work :( **
## Expectation
When I run with remote dependencies(or local dependencies), the coverage should be same with run without rust. I hope some one can help me with this problem. Thanks a lot!
Contributor guide
Assessment
This issue has not been assessed yet.