Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,15 @@ The **Debug** profile runs the same command with a bare `--debugger` and attache
agent announces itself. That flag takes no address on `elide test`: the agent always binds 5005, so one debug run at
a time.

The **Run with Coverage** profile adds `--coverage` and, once the run exits, shows the reports it wrote in the
editor's coverage gutter and the Test Coverage view: guest (JavaScript/TypeScript) LCOV from
`.dev/reports/coverage/js/lcov.info`, and JVM coverage from the JaCoCo XML report under
`.dev/reports/coverage/jvm/<task>/` (`.dev/artifacts/coverage/jvm/<task>/` before Elide 1.6), with per-line branch
counts. Elide writes JVM coverage as JaCoCo execution data (`.exec`) by default, which cannot be read without the
compiled classes; ask for the XML report in `elide.pkl` to see it in the editor:
The **Run with Coverage** profile adds `--coverage` and shows line and branch coverage in the editor's gutter and
Test Coverage view. On CLIs supporting `ELIDE_COVERAGE_REPORT`, it reads a private, run-local JSON artifact; JVM
coverage needs no XML-report configuration. Incomplete collection is reported, and sources whose recorded digest
no longer matches the file are omitted. Temporary artifacts are removed after completion or cancellation.
A failed coverage minimum errors the project run without changing individual passing test results.

Older CLIs fall back to guest LCOV (`.dev/reports/coverage/js/lcov.info`) and JaCoCo XML
(`.dev/reports/coverage/jvm/<task>/`, or `.dev/artifacts/coverage/jvm/<task>/` before Elide 1.6).
For that fallback, Elide's default JVM execution data (`.exec`) is not sufficient; configure an XML report:

```pkl
import "elide:Coverage.pkl" as Coverage
Expand Down
168 changes: 167 additions & 1 deletion packages/core/src/coverage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from "node:crypto";
import { existsSync, realpathSync } from "node:fs";
import { readFile, readdir, stat } from "node:fs/promises";
import { open, readFile, readdir, realpath, stat } from "node:fs/promises";
import path from "node:path";
import { OUTPUT_DIR } from "./elide.js";

Expand Down Expand Up @@ -196,6 +197,171 @@ export interface LoadedCoverage {
reports: CoverageReports;
}

const MAX_ARTIFACT_BYTES = 8 * 1024 * 1024;
const MAX_ARTIFACT_FILES = 2_000;
const MAX_ARTIFACT_LINES = 200_000;
// Compact branch counters must not expand into an unbounded number of editor objects.
const MAX_ARTIFACT_BRANCHES = 1_000_000;

interface CoverageArtifactFile {
engine: string;
path: string;
sourceDigest?: { algorithm: "sha256-utf16be"; value: string };
lines: { line: number; covered: boolean; branches?: { covered: number; total: number } }[];
}

function isCoverageArtifact(value: unknown): value is { version: 1; complete: boolean; files: CoverageArtifactFile[] } {
if (
typeof value !== "object" || value === null ||
!("version" in value) || value.version !== 1 ||
!("complete" in value) || typeof value.complete !== "boolean" ||
!("files" in value) || !Array.isArray(value.files) || value.files.length > MAX_ARTIFACT_FILES
) return false;
let lines = 0;
let branches = 0;
const identities = new Set<string>();
for (const file of value.files as unknown[]) {
if (
typeof file !== "object" || file === null ||
!("engine" in file) || typeof file.engine !== "string" || !file.engine ||
!("path" in file) || typeof file.path !== "string" || !file.path ||
!("lines" in file) || !Array.isArray(file.lines)
) return false;
// The producer writes normalized, relative slash paths, including on Windows.
if (
path.posix.isAbsolute(file.path) || path.win32.parse(file.path).root ||
file.path.includes("\\") || file.path.includes("\0") ||
file.path.split("/").some((part) => !part || part === "." || part === "..")
) return false;
const identity = JSON.stringify([file.engine, file.path]);
if (identities.has(identity)) return false;
identities.add(identity);
if ("sourceDigest" in file && file.sourceDigest !== undefined) {
const digest = file.sourceDigest;
if (
typeof digest !== "object" || digest === null ||
!("algorithm" in digest) || digest.algorithm !== "sha256-utf16be" ||
!("value" in digest) || typeof digest.value !== "string" || !/^[a-fA-F0-9]{64}$/.test(digest.value)
) return false;
}
lines += file.lines.length;
if (lines > MAX_ARTIFACT_LINES) return false;
const seenLines = new Set<number>();
for (const line of file.lines as unknown[]) {
if (
typeof line !== "object" || line === null ||
!("line" in line) || typeof line.line !== "number" || !Number.isSafeInteger(line.line) || line.line <= 0 ||
!("covered" in line) || typeof line.covered !== "boolean"
) return false;
if (seenLines.has(line.line)) return false;
seenLines.add(line.line);
if ("branches" in line && line.branches !== undefined) {
const counter = line.branches;
if (
typeof counter !== "object" || counter === null ||
!("covered" in counter) || typeof counter.covered !== "number" || !Number.isSafeInteger(counter.covered) || counter.covered < 0 ||
!("total" in counter) || typeof counter.total !== "number" || !Number.isSafeInteger(counter.total) || counter.total < counter.covered
) return false;
branches += counter.total;
if (branches > MAX_ARTIFACT_BRANCHES) return false;
}
}
}
return true;
}

/**
* Read a run-local coverage artifact, retaining incomplete subsets and excluding sources whose digest changed.
* Unknown or malformed documents return `undefined` to allow the legacy-report fallback.
*/
export async function loadCoverageArtifact(file: string, projectRoot: string): Promise<{ files: FileCoverageData[]; complete: boolean } | undefined> {
const text = await readArtifactText(file);
if (text === undefined) return undefined;
let artifact: unknown;
try {
artifact = JSON.parse(text);
} catch {
return undefined;
}
if (!isCoverageArtifact(artifact)) return undefined;
const root = path.resolve(projectRoot);
let realRoot: string;
try {
realRoot = await realpath(root);
} catch {
return undefined;
}
const files: FileCoverageData[] = [];
let complete = artifact.complete;
for (const entry of artifact.files) {
const source = path.resolve(root, entry.path);
if (!isWithin(root, source) || !(await artifactSourceWithinRoot(root, realRoot, source))) return undefined;
if (entry.sourceDigest) {
try {
const sourceText = await readFile(source, "utf8");
const digest = createHash("sha256").update(Buffer.from(sourceText, "utf16le").swap16()).digest("hex");
if (digest !== entry.sourceDigest.value.toLowerCase()) {
complete = false;
continue;
}
} catch {
complete = false;
continue;
}
}
files.push({
path: source,
lines: entry.lines.map((line) => ({
line: line.line,
hits: line.covered ? 1 : 0,
branches: Array.from({ length: line.branches?.total ?? 0 }, (_, i) => ({ hits: i < (line.branches?.covered ?? 0) ? 1 : 0 })),
})),
});
}
return { files: mergeCoverage(files), complete };
}

function isWithin(root: string, file: string): boolean {
const relative = path.relative(root, file);
return relative !== "" && relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
}

async function artifactSourceWithinRoot(root: string, realRoot: string, source: string): Promise<boolean> {
// Check the nearest existing ancestor too, so a missing file below an escaping symlink is rejected.
for (let candidate = source; candidate !== root; candidate = path.dirname(candidate)) {
try {
const canonical = await realpath(candidate);
return isWithin(realRoot, canonical) || (candidate !== source && canonical === realRoot);
} catch {
// A source without a digest can still be reported after it has been removed.
}
}
return true;
}

async function readArtifactText(file: string): Promise<string | undefined> {
let handle;
try {
handle = await open(file, "r");
const info = await handle.stat();
if (!info.isFile() || info.size > MAX_ARTIFACT_BYTES) return undefined;
// Read at most the observed size plus one byte, detecting growth without an unbounded readFile.
const buffer = Buffer.alloc(info.size + 1);
let length = 0;
while (length < buffer.length) {
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, null);
if (bytesRead === 0) break;
length += bytesRead;
}
if (length > info.size) return undefined;
return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, length));
} catch {
return undefined;
} finally {
await handle?.close();
}
}

/**
* Read the coverage a run left under `projectRoot` since `modifiedSince`: guest LCOV as it is, JaCoCo XML resolved
* against `sourceRoots`. JaCoCo execution data is reported in `reports` but not decoded.
Expand Down
Loading
Loading