bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
644 stars 402 forks source link

Can't build rust library with bazel coverage #1077

Open rainingmaster opened 2 years ago

rainingmaster commented 2 years ago

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

Details

I have test in these ways:

  1. Run with static library compile by local rustc, and here should be static
  2. Run with library compile with bazel and rules_rust, and here should be my_lib, and without dependencies, like here
  3. Run with library compile with bazel and rules_rust, and with local dependencies, here should be : gap_lib only
  4. Run with library compile with bazel and rules_rust, and with remote dependencies, here should be //test/lib/cargo:rand_core only

And only 1, 2 can generate coverage well, 3, 4 will run successfully, but lose the coverage data. And here 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!

rainingmaster commented 2 years ago

@illicitonion I found you are professional in this project, hope you can give me some help when you free. Thanks!

illicitonion commented 2 years ago

Hi @rainingmaster - thanks for the detailed bug report!

I've tried reproducing, and have a few follow-up questions:

  1. Am I understanding right that the thing you're changing to see different results is replacing the file test/lib/libmy_lib.a between runs? So either building it with cargo build and copying it from target/release, or by uncommenting assorted of the deps in the rust_static_library target, building it with bazel, and copying it from bazel-out?

Each of those re-builds I did with different deps lists produced a byte-for-byte identical library file, so I suspect just adding something to deps isn't sufficient for what you're trying to reproduce...

  1. How are you detecting that there's a problem sometimes? Can you show an example of the failed output?

One thing I will note is that I've found that if you just re-build the same stuff with coverage enabled, it will sometimes say no coverage information was found, but if you bazel clean --expunge it will start working again... This feels like a wider bazel issue, rather than one specific to rules_rust.

rainingmaster commented 2 years ago

Hi, @illicitonion Thanks for your response!

  1. Yes, your understanding is correct, I am try to compare the difference between depend on difference rules. And the file test/lib/libmy_lib.a is come from cargo build. You have a nice catch, the md5 of file test/lib/libmy_lib.a and the result of bazel coverage is same, I didn't notice this before.

  2. As my try, after run bazel clean --expunge, the coverage still 0, you can check the file under test/coverage/index.html, it should be always 0? I agree with you, there should be have some problem with coverage in bazel.

UebelAndre commented 2 years ago

Is this not a duplicate of https://github.com/bazelbuild/rules_rust/issues/690 (which might have been addressed by https://github.com/bazelbuild/rules_rust/pull/1324)?