crane icon indicating copy to clipboard operation
crane copied to clipboard

cargoLlvmCov produces different result in Nix derivation than a regular invocation

Open phip1611 opened this issue 2 years ago • 5 comments

When I execute cargo llvm-cov directly, results seem to be fine. When I execute it in a Nix derivation using crane.cargoLlvmCov, it seems like it takes all dependencies into account as well. However, this is not something that you want. The difference is significant:

Without Nix: lines......: 65.5% (16880 of 25762 lines) Within Nix: lines......: 22.4% (38873 of 173539 lines)

I suppose that in the regular workflow, llvm-cov filters out all dependencies (by some regex into the target folder), while in the Nix derivation the target folder is not within the same location as the src folder, hence, some regex probably doesn't match anymore.

I tried to workaround this with the --ignore-filenames-regex parameter of cargo-llvm-cov, but without success. I can't find the location where the build artifacts are stored. It is neither in $CARGO_TARGET_DIR nor in $CARGO_BUILD_TARGET_DIR

phip1611 avatar Mar 29 '23 08:03 phip1611

I'm not entirely sure why that happens, maybe it's the default --output-path $out argument that we set?

cc @figsoda in case you have more context around this?

ipetkov avatar Apr 02 '23 18:04 ipetkov

I'm not sure what could be causing this, could it have something to do with cargoArtifacts?

figsoda avatar Apr 15 '23 20:04 figsoda

FWIW. In our CI we're doing:

  # Build only deps, but with llvm-cov so `workspaceCov` can reuse them cached
  workspaceDepsCov = craneLib.buildDepsOnly (commonArgsDepsOnly // {
    pnameSuffix = "-lcov-deps";
    version = "0.0.1";
    buildPhaseCargoCommand = "cargo llvm-cov --locked --workspace --profile $CARGO_PROFILE --no-report";
    cargoBuildCommand = "dontuse";
    cargoCheckCommand = "dontuse";
    nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ cargo-llvm-cov ];
    doCheck = false;
  });

  workspaceCov = craneLib.buildPackage (commonArgs // {
    pnameSuffix = "-lcov";
    version = "0.0.1";
    cargoArtifacts = workspaceDepsCov;
    buildPhaseCargoCommand = "mkdir -p $out ; env RUST_LOG=info,timing=debug cargo llvm-cov --locked --workspace --profile $CARGO_PROFILE --lcov --all-targets --tests --output-path $out/lcov.info --  --test-threads=$(($(nproc) * 2))";
    installPhaseCommand = "true";
    nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ cargo-llvm-cov ];
    doCheck = false;
  });

and it works fine, AFAICT, a 60% coverage.

dpc avatar Apr 15 '23 21:04 dpc

and it works fine, AFAICT, a 60% coverage.

With and also without Nix?

phip1611 avatar Apr 18 '23 13:04 phip1611

We never run it locally, AFAICT. So with, in our CI. But by calling cargo llvm-cov directly, no with crane.cargoLlvmCov

dpc avatar Apr 18 '23 15:04 dpc