Repository: taiki-e/cargo-llvm-cov

Issue #303: Non-workspace path dependencies are included in reports and totals
Created: 2023-08-21T06:32:38Z
If I have a workspace package that also has non-workspace path dependencies, the coverage report from cargo llvm-cov --workspace includes the non-workspace sources. I don't know if this is expected or not, but it was surprising to me, and whether it happens is affected by details of the code.
Here's an example project if it helps. The structure  this:
.
├── Dockerfile
├── one
│  ├── Cargo.toml
│  ├── ones-child
│  │  ├── Cargo.toml
│  │  └── src
│  │     └── lib.rs
│  └── src
│     └── lib.rs
└── two
   ├── Cargo.toml
   └── src
      └── lib.rs

The only interesting manifest is one/Cargo.toml:
[package]
name = "one"
version = "0.1.0"
edition = "2021"

[dependencies]
ones-child = { path = "ones-child" }
two = { path = "../two" }

[workspace]
members = [
    "ones-child",
]
If one never calls two, two's sources are not included in the report (example project job #4908791850):
Filename                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ones-child/src/lib.rs               4                 0   100.00%           3                 0   100.00%           8                 0   100.00%           0                 0         -
src/lib.rs                          4                 0   100.00%           3                 0   100.00%           8                 0   100.00%           0                 0         -

If one DOES call two, two's sources ARE included (job #4910063504):
Filename                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
one/ones-child/src/lib.rs           4                 0   100.00%           3                 0   100.00%           8                 0   100.00%           0                 0         -
one/src/lib.rs                      4                 0   100.00%           3                 0   100.00%           8                 0   100.00%           0                 0         -
two/src/lib.rs                      1                 0   100.00%           1                 0   100.00%           3                 0   100.00%           0                 0         -

The totals are a bit variable too - if one's tests cover two's code, even if two has zero coverage itself, it will be reported as 100% covered.
Seems related to #31.

Rust 1.71.1
cargo-llvm-cov 0.5.27
run in Docker image rust:1.71.1-slim (Debian Bullseye)

Duplicate issue #361: Coverage for a single package inside a workspace
Created: 2024-04-04T19:39:29Z
I've faced the same issue as #278, i'll try to provide a reproducible example:
Create a new workspace with the following structure:
├── Cargo.toml
├── crate-a
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
└── crate-b
    ├── Cargo.toml
    └── src
        └── lib.rs

The content of the files:

Cargo.toml:
[workspace]
members = ["crate-a", "crate-b"]

crate-a/Cargo.toml:
[package]
name = "crate-a"
version = "0.1.0"
edition = "2021"
[dependencies]

crate-a/src/lib.rs:
pub fn say_hello() {
    println!("Hello, world!");
}

#[cfg(test)]
mod test {
    #[test]
    fn test_say_hello() {
        super::say_hello();
    }
}

crate-b/Cargo.toml:
[package]
name = "crate-b"
version = "0.1.0"
edition = "2021"
[dependencies]
crate-a = { path = "../crate-a" }

crate-b/src/lib.rs:
pub fn say_hello_goodby() {
    crate_a::say_hello();
    println!("Goodby!");
}

#[cfg(test)]
mod test {
    #[test]
    fn test_say_hello_goodby() {
        super::say_hello_goodby();
    }
}


Run the coverage report just for crate-b:
cargo llvm-cov --package crate-b
This is the actual output:
Filename                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
crate-a/src/lib.rs                  1                 0   100.00%           1                 0   100.00%           3                 0   100.00%           0                 0         -
crate-b/src/lib.rs                  2                 0   100.00%           2                 0   100.00%           7                 0   100.00%           0                 0         -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                               3                 0   100.00%           3                 0   100.00%          10                 0   100.00%           0                 0         -

This is the expected output:
Filename                      Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src/lib.rs                          2                 0   100.00%           2                 0   100.00%           7                 0   100.00%           0                 0         -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                               2                 0   100.00%           2                 0   100.00%           7                 0   100.00%           0                 0         -

It can be achieved ignoring crate-a from the files, but it increases in complexity as the workspace has more members:
cargo llvm-cov --package crate-b --ignore-filename-regex '(^|\/)crate-a'

Closing comment (2024-04-05T01:32:50Z): Thanks. The problem here appears to be similar to #303, and perhaps something in the implementation of #31 is not working well.