`deno run` coverage reports untaken top-level branches as covered when the V8 code cache is warm
This issue was written by an AI model (Claude Opus 5.5).
Version: Deno 2.9.6 (also seen with 2.9.4), macOS arm64
When deno run collects coverage, it still loads modules from V8's code cache in DENO_DIR (v8_code_cache_v2). V8 compiles a module loaded from the code cache without block coverage counters. The coverage profile then describes the module's top-level function as one range with isBlockCoverage: false, and deno coverage reports every top-level line as covered, including branches that never ran.
The first run in a fresh DENO_DIR gives correct coverage, because nothing is cached yet. Every later run gives inflated coverage. This happens with deno run --coverage=DIR and with deno run when DENO_COVERAGE_DIR is set. deno test --coverage does not seem to be affected, because deno test does not use the code cache. However, deno run processes started by a test inherit DENO_COVERAGE_DIR, and those processes are affected. In CI, the result also depends on whether a cached DENO_DIR restored from an earlier job contains the code cache.
Steps to reproduce
main.ts:
if (Deno.args.length > 0) {
console.log("has args");
}
console.log("done");export DENO_DIR=$(mktemp -d)
DENO_COVERAGE_DIR=cov1 deno run main.ts
DENO_COVERAGE_DIR=cov2 deno run main.ts
deno run --coverage=cov3 main.ts
deno run --no-code-cache --coverage=cov4 main.ts
deno coverage --detailed cov1
deno coverage --detailed cov2
deno coverage --detailed cov3
deno coverage --detailed cov4Expected
All four reports are the same, with lines 2 and 3 uncovered:
cover file:///.../main.ts ... 50.000% (2/4)
2 | console.log("has args");
3 | }Actual
Only cov1 (the first run, with an empty code cache) and cov4 (run with --no-code-cache) look like that. cov2 and cov3 both report:
cover file:///.../main.ts ... 100.000% (4/4)The raw profiles show the difference. This is the top-level function of main.ts in each:
cov1: isBlockCoverage: true, ranges: [0-78 count 1], [26-56 count 0]
cov2: isBlockCoverage: false, ranges: [0-78 count 2]Workaround
Pass --no-code-cache to every deno run that collects coverage, or delete v8_code_cache_v2 from DENO_DIR before each such run. Neither is practical for processes that a test starts, since they share the same DENO_DIR and each one reads the cache that the previous one wrote.
Possible fix
CliOptions::code_cache_enabled() in cli/args/mod.rs could return false whenever coverage_dir() returns a directory. coverage_dir() already covers both --coverage and DENO_COVERAGE_DIR. Every user of the code cache (the module loader, the worker factory, and the cache warm-up in cli/factory.rs) checks code_cache_enabled(), so this one change would stop coverage runs from reading or writing the cache. In local testing this makes all four reports above match.
Source: denoland/deno