|
| 1 | +# Compiled Source Coverage Checker Design |
| 2 | + |
| 3 | +## Context |
| 4 | + |
| 5 | +`scripts/check-coverage.py` should validate the Rust source files that were actually compiled in the current `cargo llvm-cov` lane, not every `src/**/*.rs` file that exists in the repository and not only the files already present in `coverage.json`. |
| 6 | + |
| 7 | +Issue #3 asks for the semantics already proven in downstream `tenferro-rs`, while preserving template-local threshold configuration and the existing `--report-only` surface. |
| 8 | + |
| 9 | +## Goals |
| 10 | + |
| 11 | +- Prefer dep-info from `target/llvm-cov-target/**/*.d` to derive the expected source universe. |
| 12 | +- Judge only runtime-bearing `src/**/*.rs` files that were compiled in the current lane. |
| 13 | +- Ignore module-local unit test sources under `src/**/tests/*.rs`. |
| 14 | +- Ignore declaration-only compiled Rust files that carry no runtime code. |
| 15 | +- Fall back to a repository scan only when dep-info is unavailable. |
| 16 | +- Preserve `default`, `files`, `exclude`, and `--report-only`. |
| 17 | + |
| 18 | +## Non-Goals |
| 19 | + |
| 20 | +- Raising default thresholds. |
| 21 | +- Adding downstream repo-specific smoke tests. |
| 22 | +- Changing CI policy or coverage thresholds outside the checker contract. |
| 23 | + |
| 24 | +## Approach Options |
| 25 | + |
| 26 | +### Option 1: Coverage JSON only |
| 27 | + |
| 28 | +Use only entries already present in `coverage.json` and keep threshold evaluation as-is. |
| 29 | + |
| 30 | +- Pros: smallest implementation. |
| 31 | +- Cons: cannot detect compiled source files omitted from the coverage report, which is the actual bug. |
| 32 | + |
| 33 | +### Option 2: Dep-info first, repository scan fallback |
| 34 | + |
| 35 | +Parse `target/llvm-cov-target/**/*.d` to discover compiled `.rs` files, filter them to runtime-bearing `src/**/*.rs`, and fall back to scanning the repo only when dep-info is unavailable. |
| 36 | + |
| 37 | +- Pros: matches the desired contract, supports feature-gated lanes, and keeps local coverage aligned with the built lane. |
| 38 | +- Cons: requires lightweight Rust source classification logic. |
| 39 | + |
| 40 | +### Option 3: Always full repository scan |
| 41 | + |
| 42 | +Ignore dep-info and scan the repository for `src/**/*.rs` files, then heuristically drop declarations and module-local tests. |
| 43 | + |
| 44 | +- Pros: simpler than dep-info parsing. |
| 45 | +- Cons: incorrectly includes feature-gated files not compiled in the current lane. |
| 46 | + |
| 47 | +## Decision |
| 48 | + |
| 49 | +Use Option 2. |
| 50 | + |
| 51 | +This is the only option that satisfies the issue requirements without broadening the source universe beyond the current `cargo llvm-cov` lane. |
| 52 | + |
| 53 | +## Data Flow |
| 54 | + |
| 55 | +1. Load thresholds from `coverage-thresholds.json`. |
| 56 | +2. Read coverage JSON from stdin or the first positional path, while preserving `--report-only`. |
| 57 | +3. Build the expected source universe: |
| 58 | + - Parse dep-info files when available. |
| 59 | + - Resolve dep-info paths against both the dep-info parent directory and the repository root. |
| 60 | + - Keep only compiled `src/**/*.rs` files. |
| 61 | + - Exclude `src/**/tests/*.rs`. |
| 62 | + - Exclude declaration-only files with no runtime-bearing code. |
| 63 | + - Fall back to repository scan when dep-info yields no files. |
| 64 | +4. Compare expected files against `coverage.json` entries, still honoring `exclude`. |
| 65 | +5. Report threshold failures plus compiled-but-missing files. |
| 66 | + |
| 67 | +## Runtime-Bearing Heuristic |
| 68 | + |
| 69 | +The checker needs a narrow heuristic instead of a full Rust parser. |
| 70 | + |
| 71 | +- Treat functions with bodies as runtime-bearing. |
| 72 | +- Treat `const` or `static` items initialized with block expressions as runtime-bearing. |
| 73 | +- Treat declaration-only items such as traits, structs, enums, and function signatures ending in `;` as non-runtime-bearing. |
| 74 | +- Allow multi-line function signatures by buffering until either `{` or `;` appears. |
| 75 | + |
| 76 | +This is intentionally conservative: it should avoid false positives for declaration-only modules while still catching compiled executable code. |
| 77 | + |
| 78 | +## Testing Strategy |
| 79 | + |
| 80 | +Add script tests covering: |
| 81 | + |
| 82 | +- failing when a compiled `src/**/*.rs` file is absent from `coverage.json` |
| 83 | +- ignoring `src/<module>/tests/*.rs` |
| 84 | +- ignoring feature-gated files not compiled in the current lane |
| 85 | +- ignoring declaration-only compiled files |
| 86 | +- resolving repo-relative dep-info paths |
| 87 | + |
| 88 | +The tests should build temporary repositories around the script so the semantics are validated without depending on the workspace layout. |
| 89 | + |
| 90 | +## Risks |
| 91 | + |
| 92 | +- Dep-info files may contain relative paths from different working directories. |
| 93 | + Mitigation: resolve against both the dep-info file location and repo root. |
| 94 | +- The runtime heuristic may misclassify unusual Rust constructs. |
| 95 | + Mitigation: keep the heuristic minimal and behavior-focused, matching the issue requirements only. |
0 commit comments