Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
150 changes: 122 additions & 28 deletions src/analyze/coverage/python-line-coverage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn } from 'node:child_process';
import * as fs from 'node:fs/promises';
import * as fsSync from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

Expand Down Expand Up @@ -69,6 +70,28 @@ export interface CoverageReport {
// toCoverageRunArgs). Losing a measurement costs a verdict; losing equivalence
// costs the truth of every verdict this module supports.
//
// Equivalence may be ESTABLISHED in two ways, and only two:
//
// 1. By RECOGNITION. The command is already equivalent in both spawn shapes
// (`python3 -m pytest`, `python3 tests/runtests.py`). Nothing is rebuilt.
//
// 2. By CONSTRUCTION. We reproduce ONE bounded shell behaviour so the two
// spawns agree. Today that is PATH resolution of a console entry point,
// so sys.path[0] is the bin directory in both. A construction is admitted
// only when all three hold:
// a. TOTAL over its input: every case the shell handles we handle
// identically, or we decline. No "close enough".
// b. CHECKABLE against an observable, not an argument. Here the
// shadow-bypass guard independently tests whether coverage measured
// the copy the gate imported.
// c. Its failure is a DECLINE, never a fall back to a form already
// known to be non-equivalent.
//
// Clause (c) is written in blood: falling back to module form when resolution
// failed reintroduced the exact false SAFE the construction existed to remove,
// on every pyenv, asdf and nix setup, because their console scripts are shell
// shims rather than Python files.
//
// So: new shell features get taught to this path only when equivalence can be
// PROVEN, not when a bug report asks for them.

Expand Down Expand Up @@ -122,20 +145,24 @@ const ASSIGNMENT_RE = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s;
* PATH-like assignment value. */
const TILDE_EXPANDS_RE = /(^|:)~/;

/** Variables that can change WHICH copy of a module Python imports. When one of
* these is set, the interpreter's own sys.path[0] rule decides who wins, so a
* spawn-shape difference between the gate and this runner stops being cosmetic
* and starts selecting a different tree. See the entry-point note below. */
const IMPORT_AFFECTING_RE = /^(PYTHON[A-Z0-9_]*|PATH|VIRTUAL_ENV|CONDA_PREFIX)$/;

/** What to hand `coverage run`, plus any environment the command carried.
* The env is separate because `coverage run` takes argv, not a shell line. */
export interface CoverageRunPlan {
args: string[];
env: Record<string, string>;
}

export function toCoverageRunArgs(testCmd: string): CoverageRunPlan | null {
/** Resolves a console entry point (`pytest`) to the script file a shell would
* execute for it, or null when it cannot be resolved to a Python script we can
* hand to `coverage run` positionally. Injected so the classifier stays pure
* and testable, and REQUIRED so no call site can omit it and silently select a
* spawn shape the gate would not have used. */
export type EntryPointResolver = (name: string, env: Record<string, string>) => string | null;

export function toCoverageRunArgs(
testCmd: string,
resolveEntryPoint: EntryPointResolver,
): CoverageRunPlan | null {
if (SHELL_COMPOSITE_RE.test(testCmd)) return null;
const tokens = tokenizeCommand(testCmd);
if (tokens.length === 0) return null;
Expand Down Expand Up @@ -183,29 +210,96 @@ export function toCoverageRunArgs(testCmd: string): CoverageRunPlan | null {
return { args: [...rest], env };
}

// Console entry point, rewritten to module form. This rewrite is NOT
// equivalent to the gate's spawn when an import-affecting variable is set,
// and the difference silently selects a different copy of the code:
// Console entry point. Prefer to run the SAME file the gate's shell would
// run, positionally, so sys.path[0] is the bin directory in both spawns and
// neither run can import a copy the other did not. This is what lets the
// shadow-bypass guard work: when an editable install resolves imports to the
// ORIGINAL tree, coverage now measures that tree too, the shadow file is
// absent from measuredFiles, and the guard fires as designed.
const resolved = resolveEntryPoint(head, env);
if (resolved !== null) return { args: [resolved, ...rest.slice(1)], env };

// Unresolvable, so decline. There is deliberately no fallback to `-m` here.
//
// gate sh -c "PYTHONPATH=<other> pytest -q" console script:
// sys.path[0] is the bin
// dir, so PYTHONPATH wins
// this path coverage run -m pytest -q `-m` puts CWD FIRST,
// ahead of PYTHONPATH, so
// the shadow tree wins
// Module form is what shipped before, and it is the shape that produced the
// false SAFE: `-m` prepends CWD to sys.path while the console script the gate
// runs does not, so on any project where something else supplies the package
// (an editable install, a plain install, PYTHONPATH) the gate ran the ORIGINAL
// tree while coverage measured the SHADOW tree, and fusing those two true
// statements produced SAFE for a change that broke the suite.
//
// The gate then proves the ORIGINAL tree green while coverage proves the
// SHADOW tree exercised, and fusing those two true statements yields SAFE for
// a change that breaks the suite. That is a false SAFE, reproduced on a flat
// layout with `PYTHONPATH=<repoRoot> pytest -q`, and it is the reason the
// equivalence rule at the top of this file is written as a rule.
// Falling back to it would keep that hole open wherever resolution fails, and
// resolution fails on exactly the setups people use: pyenv and asdf install
// `#!/usr/bin/env bash` shims, nix wraps programs the same way, and Windows
// console scripts are native `.exe` launchers. That is not a corner, so the
// fallback is not a safety net; it is the bug with narrower reach.
//
// Declining costs a measurement for this shape and nothing else: the form the
// docs teach, `PYTHONPATH=. python3 -m pytest -q`, is module form on BOTH
// sides and stays equivalent. Widening the decline can only lose capability;
// narrowing it loses soundness.
if (Object.keys(env).some((name) => IMPORT_AFFECTING_RE.test(name))) return null;
return { args: ['-m', ...rest], env };
// The cost is bounded and honest. `python3 -m pytest -q`, which is what
// auto-detection produces and what every documented example teaches, is
// module form on BOTH sides and never reaches this branch.
return null;
}

/** Find the file a shell would execute for a console entry point, and accept it
* only if it is a Python script we can hand to `coverage run` positionally.
*
* The shebang check is the whole safety argument. A Windows console script is
* an `.exe` launcher and a Unix one is a Python file with `#!.../python`;
* handing the former to `coverage run` would fail, so it is rejected here and
* the caller falls back to module form. Resolution uses the EFFECTIVE PATH,
* including any hoisted `PATH=` prefix, so it agrees with what the gate's
* shell would have resolved. */
export function resolveConsoleScript(name: string, env: Record<string, string>): string | null {
// A name containing a separator is a path, not a PATH lookup. The shell runs
// it relative to ITS cwd, which is the shadow root and not this process's cwd,
// so we cannot reproduce it here.
if (name.includes('/') || name.includes('\\')) return null;

const search = env.PATH ?? process.env.PATH ?? '';
for (const dir of search.split(path.delimiter)) {
// An empty element means cwd to a POSIX shell, and a relative element is
// resolved against the SHELL's cwd. Either way the shell would search a
// directory we cannot identify from here, and anything we found later on
// PATH might be shadowed by something in it. Decline rather than guess.
if (dir === '' || !path.isAbsolute(dir)) return null;

const candidate = path.join(dir, name);
try {
// The shell skips a match it cannot execute and keeps searching, so this
// must too: a non-executable file earlier on PATH is invisible to it.
fsSync.accessSync(candidate, fsSync.constants.X_OK);
if (!fsSync.statSync(candidate).isFile()) continue;
} catch {
continue; // missing, not executable, or a directory: keep searching
}

// This is the file the shell would run. It is the ONLY candidate we may
// consider: scanning past it to find a Python script further down would
// run a different program than the gate did, which is precisely the
// divergence this resolver exists to remove.
try {
const fd = fsSync.openSync(candidate, 'r');
try {
const buf = Buffer.alloc(512);
const read = fsSync.readSync(fd, buf, 0, 512, 0);
const first = (buf.subarray(0, read).toString('utf8').split('\n')[0] ?? '').replace(
/\r$/,
'',
);
// Only a Python script can be handed to `coverage run` positionally.
// A shim (`#!/usr/bin/env bash`, pyenv and asdf install these), a
// native launcher (Windows `.exe`), or a shebang we cannot read is not
// one, and the caller declines rather than substituting module form.
if (first.startsWith('#!') && /python/i.test(first)) return candidate;
} finally {
fsSync.closeSync(fd);
}
} catch {
// unreadable: treat as not resolvable
}
return null;
}
return null;
}

/** Probe whether `coverage.py` can actually RUN in the user's Python.
Expand Down Expand Up @@ -262,7 +356,7 @@ export async function reportCoverage(input: CoverageReportInput): Promise<Covera
};
}

const plan = toCoverageRunArgs(testCmd);
const plan = toCoverageRunArgs(testCmd, resolveConsoleScript);
if (plan === null) {
return {
coverageToolFound: true,
Expand Down
14 changes: 13 additions & 1 deletion src/verify/verify-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,19 @@ async function assessCoverage(
(r) => r.lines.length > 0 && !report.measuredFiles.has(normalizePath(r.path)),
);
if (unmeasured.length > 0) {
return unknownCoverage();
// Say WHY, and say what fixes it. This is the dominant degradation path
// for a project whose tests import an installed copy, and a bare "could
// not be determined" leaves the user with no idea that a one-line change
// to their test command would give them a real verdict. The remedy is
// documented at verdicts.mdx "Make sure the tests run the code being
// verified", but nobody who lands here has a reason to go read it.
const names = unmeasured.map((r) => r.path).join(', ');
return unknownCoverage(
`the test run never imported ${names}, so the suite exercised a different copy ` +
`than the one being verified (usually an installed or editable package). ` +
`Put the verified tree first on sys.path, for example by prefixing the test ` +
`command with PYTHONPATH=. (or PYTHONPATH=src for a src layout).`,
);
}
// Judge coverage on STATEMENTS, not physical lines, using real extents from
// the Python AST. coverage.py only ever marks a statement's FIRST line, so a
Expand Down
Loading
Loading