Skip to content

Commit cd53ac0

Browse files
authored
fix(ci): harden hydrated dead-export scans (#120365)
* fix(ci): harden hydrated dead-export scans * fix(ci): satisfy lint and test-type gates for hydrated scan hardening
1 parent f513e87 commit cd53ac0

5 files changed

Lines changed: 160 additions & 34 deletions

File tree

‎scripts/check-deadcode-exports.d.mts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ export function checkUnusedExports(output: string): {
1111
entries: string[];
1212
message: string;
1313
};
14+
/** Classifies Knip export output, rejecting findings after resolution failures. */
15+
export function checkExportScan(
16+
scanName: string,
17+
output: string,
18+
): {
19+
ok: boolean;
20+
entries: string[];
21+
message: string;
22+
};

‎scripts/check-deadcode-exports.mjs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ export function checkUnusedExports(output) {
9595
};
9696
}
9797

98+
/** Classifies Knip export output, rejecting findings after resolution failures. */
99+
export function checkExportScan(scanName, output) {
100+
const resolutionErrors = output
101+
.split(/\r?\n/u)
102+
.filter((line) => line.startsWith("ERROR: Error loading "));
103+
if (resolutionErrors.length > 0) {
104+
return {
105+
ok: false,
106+
entries: [],
107+
message: [
108+
`deadcode ${scanName} could not resolve workspace modules; export findings would be unreliable and are discarded.`,
109+
...resolutionErrors,
110+
"Install workspace dependencies in-tree (pnpm install) — on delegated boxes the crabbox wrapper links the hydrated modules dir into the workdir — and rerun.",
111+
].join("\n"),
112+
};
113+
}
114+
115+
const check = checkUnusedExports(output);
116+
return {
117+
...check,
118+
message: check.ok ? "" : `${scanName}:\n${check.message}`,
119+
};
120+
}
121+
98122
async function main() {
99123
// The scans are independent Knip child processes over separate configs;
100124
// running them concurrently cuts the lane's serial wall clock roughly 2x.
@@ -128,6 +152,12 @@ function reportUnusedExportScan(scan, result) {
128152
return false;
129153
}
130154

155+
const check = checkExportScan(scan.name, result.output);
156+
if (!check.ok) {
157+
console.error(check.message);
158+
return false;
159+
}
160+
131161
const parsed = parseKnipCompactUnusedExportsResult(result.output);
132162
// Knip's compact reporter omits empty sections, so a clean scan (exit 0)
133163
// legitimately prints no export sections; sectionless output is only a
@@ -140,11 +170,6 @@ function reportUnusedExportScan(scan, result) {
140170
return false;
141171
}
142172

143-
const check = checkUnusedExports(result.output);
144-
if (!check.ok) {
145-
console.error(`${scan.name}:\n${check.message}`);
146-
return false;
147-
}
148173
if (result.status !== 0) {
149174
console.error(`deadcode ${scan.name} exited with status ${result.status}.`);
150175
if (result.output) {

‎scripts/crabbox-wrapper.mjs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,12 @@ function remoteWindowsHydratedNodeModulesBootstrap() {
25502550
].join("; ");
25512551
}
25522552

2553+
function remotePosixHydratedNodeModulesBootstrap() {
2554+
// Knip and other non-pnpm tools walk node_modules, while hydrated boxes keep it external.
2555+
// Without this link, dead-code scans silently lose consumer edges and report false positives.
2556+
return 'if [ -n "${PNPM_CONFIG_MODULES_DIR:-}" ] && [ -d "$PNPM_CONFIG_MODULES_DIR" ] && [ ! -e node_modules ]; then ln -s "$PNPM_CONFIG_MODULES_DIR" node_modules; fi;';
2557+
}
2558+
25532559
function injectRemoteWindowsHydratedNodeModulesBootstrap(invocation, facts, providerName) {
25542560
if (
25552561
invocation.args[0] !== "run" ||
@@ -2571,6 +2577,23 @@ function injectRemoteWindowsHydratedNodeModulesBootstrap(invocation, facts, prov
25712577
);
25722578
}
25732579

2580+
function injectRemotePosixHydratedNodeModulesBootstrap(invocation) {
2581+
if (
2582+
invocation.args[0] !== "run" ||
2583+
isWindowsRemoteTarget(invocation.args) ||
2584+
invocation.options.has("script") ||
2585+
invocation.options.has("script-stdin") ||
2586+
invocation.start < 0
2587+
) {
2588+
return invocation.args;
2589+
}
2590+
2591+
return replaceRunCommandWithShell(
2592+
invocation,
2593+
`${remotePosixHydratedNodeModulesBootstrap()} ${renderRunShellCommand(invocation)}`,
2594+
);
2595+
}
2596+
25742597
function injectRemoteChangedGateGitBootstrap(commandArgs, changedGateBase, changedGateAlias) {
25752598
if (!changedGateBase || commandArgs[0] !== "run" || isWindowsRemoteTarget(commandArgs)) {
25762599
return commandArgs;
@@ -3636,6 +3659,8 @@ function applyRunTransforms(initialInvocation, initialFacts, options) {
36363659
options.changedGateAlias,
36373660
);
36383661
}
3662+
invocation = parseRunInvocation(help.text, transformedArgs);
3663+
transformedArgs = injectRemotePosixHydratedNodeModulesBootstrap(invocation);
36393664
return {
36403665
args: injectRemoteTestboxCi(transformedArgs, options.provider),
36413666
wsl2ScriptBootstrap,

‎test/scripts/check-deadcode-exports.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import allExportsKnipConfig from "../../config/knip.all-exports.config.ts";
77
import knipConfig from "../../config/knip.config.ts";
88
import scriptExportsKnipConfig from "../../config/knip.scripts-exports.config.ts";
99
import {
10+
checkExportScan,
1011
checkUnusedExports,
1112
parseKnipCompactUnusedExports,
1213
parseKnipCompactUnusedExportsResult,
@@ -388,4 +389,41 @@ src/a.ts: alpha
388389
Delete the exports or model their real production consumers in Knip.`,
389390
});
390391
});
392+
393+
it("discards export findings after workspace module resolution failures", () => {
394+
const output = `ERROR: Error loading vitest.config.ts (Cannot find module 'vitest/config')
395+
ERROR: Error loading ui/vite.config.ts (Cannot find module 'vite')
396+
Unused exports (1)
397+
src/config/types.ts: TelegramConfig
398+
`;
399+
400+
const result = checkExportScan("production unused-export scan", output);
401+
expect(result.ok).toBe(false);
402+
expect(result.entries).toEqual([]);
403+
expect(result.message).toContain(
404+
"deadcode production unused-export scan could not resolve workspace modules; export findings would be unreliable and are discarded.",
405+
);
406+
expect(result.message).toContain("ERROR: Error loading vitest.config.ts");
407+
expect(result.message).toContain("ERROR: Error loading ui/vite.config.ts");
408+
expect(result.message).toContain("Install workspace dependencies in-tree (pnpm install)");
409+
expect(result.message).not.toContain("Unused exports are not allowed");
410+
expect(result.message).not.toContain("src/config/types.ts: TelegramConfig");
411+
});
412+
413+
it("accepts clean export scan output unchanged", () => {
414+
expect(checkExportScan("clean scan", "")).toEqual({ entries: [], message: "", ok: true });
415+
});
416+
417+
it("reports genuine export findings unchanged", () => {
418+
expect(
419+
checkExportScan("finding scan", "Unused exports (1)\nsrc/config/types.ts: TelegramConfig\n"),
420+
).toEqual({
421+
entries: ["src/config/types.ts: TelegramConfig"],
422+
message: `finding scan:
423+
Unused exports are not allowed:
424+
src/config/types.ts: TelegramConfig
425+
Delete the exports or model their real production consumers in Knip.`,
426+
ok: false,
427+
});
428+
});
391429
});

0 commit comments

Comments
 (0)