@hominis/fireforge 0.44.4 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/dist/build-info.json +4 -4
- package/dist/src/commands/doctor-source-pin.d.ts +29 -0
- package/dist/src/commands/doctor-source-pin.js +81 -0
- package/dist/src/commands/doctor.js +2 -0
- package/dist/src/commands/export-flow.js +3 -18
- package/dist/src/commands/patch/move-files-into.js +3 -6
- package/dist/src/commands/patch/split-plan.js +3 -6
- package/dist/src/commands/re-export-adjacent.d.ts +8 -0
- package/dist/src/commands/re-export-adjacent.js +26 -1
- package/dist/src/commands/re-export-drift.d.ts +8 -0
- package/dist/src/commands/re-export-drift.js +61 -0
- package/dist/src/commands/re-export-files.js +3 -6
- package/dist/src/commands/re-export-options.js +19 -5
- package/dist/src/commands/re-export-register.js +5 -1
- package/dist/src/commands/re-export-scan.js +3 -6
- package/dist/src/commands/re-export.js +16 -16
- package/dist/src/commands/test-build-phase.js +2 -1
- package/dist/src/commands/test-diagnose.js +14 -5
- package/dist/src/commands/test-register.js +1 -0
- package/dist/src/commands/test-run.js +4 -3
- package/dist/src/commands/test-stale-gate.js +5 -2
- package/dist/src/commands/test.js +9 -2
- package/dist/src/core/build-audit.js +44 -2
- package/dist/src/core/build-jar-escalation.d.ts +64 -0
- package/dist/src/core/build-jar-escalation.js +127 -0
- package/dist/src/core/build-prepare.d.ts +12 -1
- package/dist/src/core/build-prepare.js +36 -3
- package/dist/src/core/config-validate-file-size.d.ts +12 -0
- package/dist/src/core/config-validate-file-size.js +65 -0
- package/dist/src/core/config-validate.js +6 -0
- package/dist/src/core/harness-orphans.d.ts +73 -0
- package/dist/src/core/harness-orphans.js +186 -0
- package/dist/src/core/mach-known-noise-filter.d.ts +21 -0
- package/dist/src/core/mach-known-noise-filter.js +24 -1
- package/dist/src/core/mach.d.ts +7 -0
- package/dist/src/core/mach.js +18 -2
- package/dist/src/core/patch-apply.js +49 -20
- package/dist/src/core/patch-lint-checkjs.js +6 -1
- package/dist/src/core/patch-lint-file-size.d.ts +52 -0
- package/dist/src/core/patch-lint-file-size.js +53 -0
- package/dist/src/core/patch-lint-prettier.d.ts +36 -0
- package/dist/src/core/patch-lint-prettier.js +126 -0
- package/dist/src/core/patch-lint-unmanaged-companion.d.ts +106 -0
- package/dist/src/core/patch-lint-unmanaged-companion.js +214 -0
- package/dist/src/core/patch-lint.js +23 -24
- package/dist/src/core/patch-transform.d.ts +21 -0
- package/dist/src/core/patch-transform.js +38 -0
- package/dist/src/core/smoke-patterns.js +12 -0
- package/dist/src/core/test-harness-crash.js +61 -48
- package/dist/src/core/test-harness-verdict-notes.d.ts +55 -0
- package/dist/src/core/test-harness-verdict-notes.js +48 -0
- package/dist/src/core/test-stale-check.d.ts +23 -6
- package/dist/src/core/test-stale-check.js +75 -2
- package/dist/src/core/test-stall-triage.js +37 -19
- package/dist/src/errors/build.d.ts +28 -0
- package/dist/src/errors/build.js +49 -0
- package/dist/src/types/commands/options.d.ts +19 -0
- package/dist/src/types/commands/patches.d.ts +17 -6
- package/dist/src/types/config.d.ts +41 -0
- package/dist/src/utils/fs.d.ts +4 -2
- package/dist/src/utils/fs.js +3 -1
- package/docs/configuration.md +47 -0
- package/docs/exit-codes.md +4 -2
- package/docs/testing.md +62 -10
- package/package.json +1 -1
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `file-too-large` thresholds and their rendered message.
|
|
3
|
+
*
|
|
4
|
+
* Its own module for two reasons: the config validator needs the DEFAULTS
|
|
5
|
+
* to validate an override triple against the merged result, and importing
|
|
6
|
+
* `patch-lint.ts` from `config-validate.ts` for a constant would draw an
|
|
7
|
+
* import edge (and a cycle) between the rule bodies and config parsing.
|
|
8
|
+
*
|
|
9
|
+
* On the vocabulary: the bands were `notice` / `warning` / `error` with the
|
|
10
|
+
* middle one described as a "soft limit". That description is false under
|
|
11
|
+
* the posture FireForge itself recommends for a release gate,
|
|
12
|
+
* `--max-warnings 0`, where a warning is a hard failure — so a file at 751
|
|
13
|
+
* lines failed a gate with a message calling 750 soft. The message below
|
|
14
|
+
* says what the band IS (a warning) and what a zero-warning gate does with
|
|
15
|
+
* it, and the thresholds are now tunable, so a project whose file-size
|
|
16
|
+
* policy differs has a dial instead of an argument.
|
|
17
|
+
*/
|
|
18
|
+
import type { PatchLintFileSizeThresholds } from '../types/config.js';
|
|
19
|
+
/** A fully resolved threshold triple. */
|
|
20
|
+
export interface ResolvedFileSizeTier {
|
|
21
|
+
notice: number;
|
|
22
|
+
warning: number;
|
|
23
|
+
error: number;
|
|
24
|
+
}
|
|
25
|
+
/** Built-in defaults, per file class. */
|
|
26
|
+
export declare const DEFAULT_FILE_SIZE_THRESHOLDS: {
|
|
27
|
+
general: ResolvedFileSizeTier;
|
|
28
|
+
test: ResolvedFileSizeTier;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Merges a configured override over the defaults for one file class.
|
|
32
|
+
*
|
|
33
|
+
* @param overrides - The `patchLint.fileSizeThresholds` block, if any
|
|
34
|
+
* @param isTest - Whether the file counts as a test file
|
|
35
|
+
* @returns The effective triple for this file
|
|
36
|
+
*/
|
|
37
|
+
export declare function resolveFileSizeThresholds(overrides: PatchLintFileSizeThresholds | undefined, isTest: boolean): ResolvedFileSizeTier;
|
|
38
|
+
/**
|
|
39
|
+
* Renders the `file-too-large` message for a given band.
|
|
40
|
+
*
|
|
41
|
+
* The `warning` and `notice` bands name the gate consequence explicitly:
|
|
42
|
+
* the number is advisory to patch-lint and blocking to a `--max-warnings 0`
|
|
43
|
+
* gate, and an operator deciding whether to decompose a file needs to know
|
|
44
|
+
* which of those they are looking at.
|
|
45
|
+
*/
|
|
46
|
+
export declare function formatFileTooLargeMessage(args: {
|
|
47
|
+
label: string;
|
|
48
|
+
lineCount: number;
|
|
49
|
+
thresholds: ResolvedFileSizeTier;
|
|
50
|
+
band: 'notice' | 'warning' | 'error';
|
|
51
|
+
verb: string;
|
|
52
|
+
}): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EUPL-1.2
|
|
2
|
+
/**
|
|
3
|
+
* The `file-too-large` thresholds and their rendered message.
|
|
4
|
+
*
|
|
5
|
+
* Its own module for two reasons: the config validator needs the DEFAULTS
|
|
6
|
+
* to validate an override triple against the merged result, and importing
|
|
7
|
+
* `patch-lint.ts` from `config-validate.ts` for a constant would draw an
|
|
8
|
+
* import edge (and a cycle) between the rule bodies and config parsing.
|
|
9
|
+
*
|
|
10
|
+
* On the vocabulary: the bands were `notice` / `warning` / `error` with the
|
|
11
|
+
* middle one described as a "soft limit". That description is false under
|
|
12
|
+
* the posture FireForge itself recommends for a release gate,
|
|
13
|
+
* `--max-warnings 0`, where a warning is a hard failure — so a file at 751
|
|
14
|
+
* lines failed a gate with a message calling 750 soft. The message below
|
|
15
|
+
* says what the band IS (a warning) and what a zero-warning gate does with
|
|
16
|
+
* it, and the thresholds are now tunable, so a project whose file-size
|
|
17
|
+
* policy differs has a dial instead of an argument.
|
|
18
|
+
*/
|
|
19
|
+
/** Built-in defaults, per file class. */
|
|
20
|
+
export const DEFAULT_FILE_SIZE_THRESHOLDS = {
|
|
21
|
+
general: { notice: 500, warning: 750, error: 900 },
|
|
22
|
+
test: { notice: 1200, warning: 1400, error: 1600 },
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Merges a configured override over the defaults for one file class.
|
|
26
|
+
*
|
|
27
|
+
* @param overrides - The `patchLint.fileSizeThresholds` block, if any
|
|
28
|
+
* @param isTest - Whether the file counts as a test file
|
|
29
|
+
* @returns The effective triple for this file
|
|
30
|
+
*/
|
|
31
|
+
export function resolveFileSizeThresholds(overrides, isTest) {
|
|
32
|
+
const key = isTest ? 'test' : 'general';
|
|
33
|
+
const configured = overrides?.[key] ?? {};
|
|
34
|
+
return { ...DEFAULT_FILE_SIZE_THRESHOLDS[key], ...configured };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Renders the `file-too-large` message for a given band.
|
|
38
|
+
*
|
|
39
|
+
* The `warning` and `notice` bands name the gate consequence explicitly:
|
|
40
|
+
* the number is advisory to patch-lint and blocking to a `--max-warnings 0`
|
|
41
|
+
* gate, and an operator deciding whether to decompose a file needs to know
|
|
42
|
+
* which of those they are looking at.
|
|
43
|
+
*/
|
|
44
|
+
export function formatFileTooLargeMessage(args) {
|
|
45
|
+
const { label, lineCount, thresholds, band, verb } = args;
|
|
46
|
+
if (band === 'error') {
|
|
47
|
+
return `${label} has ${String(lineCount)} lines (error threshold: ${String(thresholds.error)}). Consider ${verb}.`;
|
|
48
|
+
}
|
|
49
|
+
return (`${label} has ${String(lineCount)} lines (warning threshold: ${String(thresholds.warning)}, ` +
|
|
50
|
+
`error threshold: ${String(thresholds.error)}). Consider ${verb}. ` +
|
|
51
|
+
`Reported as a ${band}, which a gate running --max-warnings 0 treats as a failure; ` +
|
|
52
|
+
`tune "patchLint.fileSizeThresholds" if these limits are wrong for this project.`);
|
|
53
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional Prettier pass over patch-owned `.sys.mjs` modules.
|
|
3
|
+
*
|
|
4
|
+
* Until 0.45.0 no tier of the chain ran Prettier on `.sys.mjs` at all, so
|
|
5
|
+
* module formatting drift was caught by nothing and survived to a human
|
|
6
|
+
* reviewer. Worse, the obvious manual check was misleading in the same
|
|
7
|
+
* direction: a fork's ROOT `.prettierignore` typically excludes `engine/`,
|
|
8
|
+
* so `prettier --check --config engine/.prettierrc.js <engine files>` run
|
|
9
|
+
* from the repo root reports "all files use Prettier code style" for files
|
|
10
|
+
* that fail the identical check run from inside `engine/`. A root-level
|
|
11
|
+
* spot-check falsely refuted a correct reviewer finding on exactly this.
|
|
12
|
+
*
|
|
13
|
+
* This pass therefore runs prettier with `cwd` set to the ENGINE directory,
|
|
14
|
+
* so the engine's own `.prettierrc*` and `.prettierignore` resolve the way
|
|
15
|
+
* they do for anyone working in that tree, and the FireForge result agrees
|
|
16
|
+
* with the operator's own command.
|
|
17
|
+
*
|
|
18
|
+
* Opt-in (`patchLint.prettier`, default `'off'`): it spawns a process per
|
|
19
|
+
* lint and is a new failure surface on queues that never had one. When it
|
|
20
|
+
* is off, formatting is explicitly out of scope for the per-patch tier —
|
|
21
|
+
* that is the contract, not an accident.
|
|
22
|
+
*/
|
|
23
|
+
import type { PatchLintIssue } from '../types/commands/index.js';
|
|
24
|
+
import type { PatchLintSeverityGate } from '../types/config.js';
|
|
25
|
+
/**
|
|
26
|
+
* Runs `prettier --check` over `files` from inside the engine directory.
|
|
27
|
+
*
|
|
28
|
+
* @param engineDir - Absolute engine directory; also the `cwd`, which is
|
|
29
|
+
* what makes the engine's own config and ignore file authoritative
|
|
30
|
+
* @param projectRoot - FireForge project root, for the fallback binary
|
|
31
|
+
* @param files - Engine-relative patch-owned `.sys.mjs` paths
|
|
32
|
+
* @param gate - Severity for reported issues; `'off'` skips the pass
|
|
33
|
+
* @returns One issue per unformatted file, or a single run-level issue when
|
|
34
|
+
* prettier could not be run at all
|
|
35
|
+
*/
|
|
36
|
+
export declare function invokePatchLintPrettier(engineDir: string, projectRoot: string, files: readonly string[], gate: PatchLintSeverityGate): Promise<PatchLintIssue[]>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EUPL-1.2
|
|
2
|
+
/**
|
|
3
|
+
* Optional Prettier pass over patch-owned `.sys.mjs` modules.
|
|
4
|
+
*
|
|
5
|
+
* Until 0.45.0 no tier of the chain ran Prettier on `.sys.mjs` at all, so
|
|
6
|
+
* module formatting drift was caught by nothing and survived to a human
|
|
7
|
+
* reviewer. Worse, the obvious manual check was misleading in the same
|
|
8
|
+
* direction: a fork's ROOT `.prettierignore` typically excludes `engine/`,
|
|
9
|
+
* so `prettier --check --config engine/.prettierrc.js <engine files>` run
|
|
10
|
+
* from the repo root reports "all files use Prettier code style" for files
|
|
11
|
+
* that fail the identical check run from inside `engine/`. A root-level
|
|
12
|
+
* spot-check falsely refuted a correct reviewer finding on exactly this.
|
|
13
|
+
*
|
|
14
|
+
* This pass therefore runs prettier with `cwd` set to the ENGINE directory,
|
|
15
|
+
* so the engine's own `.prettierrc*` and `.prettierignore` resolve the way
|
|
16
|
+
* they do for anyone working in that tree, and the FireForge result agrees
|
|
17
|
+
* with the operator's own command.
|
|
18
|
+
*
|
|
19
|
+
* Opt-in (`patchLint.prettier`, default `'off'`): it spawns a process per
|
|
20
|
+
* lint and is a new failure surface on queues that never had one. When it
|
|
21
|
+
* is off, formatting is explicitly out of scope for the per-patch tier —
|
|
22
|
+
* that is the contract, not an accident.
|
|
23
|
+
*/
|
|
24
|
+
import { join } from 'node:path';
|
|
25
|
+
import { toError } from '../utils/errors.js';
|
|
26
|
+
import { pathExists } from '../utils/fs.js';
|
|
27
|
+
import { verbose } from '../utils/logger.js';
|
|
28
|
+
import { exec } from '../utils/process.js';
|
|
29
|
+
/** How long prettier gets before the pass gives up (ms). */
|
|
30
|
+
const PRETTIER_TIMEOUT_MS = 120_000;
|
|
31
|
+
/** Prettier's per-file `--check` failure line. */
|
|
32
|
+
const PRETTIER_WARN_LINE = /^\[warn]\s+(.+)$/;
|
|
33
|
+
/** Summary lines prettier prints alongside the per-file ones. */
|
|
34
|
+
const PRETTIER_SUMMARY_LINE = /^\[warn]\s+(?:Code style issues|Forgot to run)/;
|
|
35
|
+
/**
|
|
36
|
+
* Finds prettier, preferring a binary installed in the tree being checked.
|
|
37
|
+
*
|
|
38
|
+
* Order matters: the engine's own `node_modules` is the install whose
|
|
39
|
+
* version matches the engine's config, the project's is the next best
|
|
40
|
+
* guess, and `npx` is the last resort (it can download, so it is never
|
|
41
|
+
* preferred over something already present).
|
|
42
|
+
*/
|
|
43
|
+
async function resolvePrettier(engineDir, projectRoot) {
|
|
44
|
+
for (const root of [engineDir, projectRoot]) {
|
|
45
|
+
const candidate = join(root, 'node_modules', '.bin', 'prettier');
|
|
46
|
+
if (await pathExists(candidate)) {
|
|
47
|
+
return { command: candidate, prefixArgs: [] };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { command: 'npx', prefixArgs: ['--no-install', 'prettier'] };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Runs `prettier --check` over `files` from inside the engine directory.
|
|
54
|
+
*
|
|
55
|
+
* @param engineDir - Absolute engine directory; also the `cwd`, which is
|
|
56
|
+
* what makes the engine's own config and ignore file authoritative
|
|
57
|
+
* @param projectRoot - FireForge project root, for the fallback binary
|
|
58
|
+
* @param files - Engine-relative patch-owned `.sys.mjs` paths
|
|
59
|
+
* @param gate - Severity for reported issues; `'off'` skips the pass
|
|
60
|
+
* @returns One issue per unformatted file, or a single run-level issue when
|
|
61
|
+
* prettier could not be run at all
|
|
62
|
+
*/
|
|
63
|
+
export async function invokePatchLintPrettier(engineDir, projectRoot, files, gate) {
|
|
64
|
+
if (gate === 'off' || files.length === 0)
|
|
65
|
+
return [];
|
|
66
|
+
const severity = gate === 'error' ? 'error' : 'warning';
|
|
67
|
+
const invocation = await resolvePrettier(engineDir, projectRoot);
|
|
68
|
+
let result;
|
|
69
|
+
try {
|
|
70
|
+
result = await exec(invocation.command, [...invocation.prefixArgs, '--check', ...files], {
|
|
71
|
+
cwd: engineDir,
|
|
72
|
+
timeout: PRETTIER_TIMEOUT_MS,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return [
|
|
77
|
+
{
|
|
78
|
+
file: '(prettier)',
|
|
79
|
+
check: 'prettier-format',
|
|
80
|
+
message: `patchLint.prettier is enabled but prettier could not be run (${toError(error).message}). ` +
|
|
81
|
+
'Install prettier in engine/ or the project root, or set "patchLint.prettier": "off".',
|
|
82
|
+
severity,
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
// Exit 0: everything formatted. Exit 1: at least one file differs. Any
|
|
87
|
+
// other code is prettier failing (bad config, unparseable file), which is
|
|
88
|
+
// a different finding and must not be reported as formatting drift.
|
|
89
|
+
if (result.exitCode === 0)
|
|
90
|
+
return [];
|
|
91
|
+
const output = `${result.stderr}\n${result.stdout}`;
|
|
92
|
+
if (result.exitCode !== 1) {
|
|
93
|
+
return [
|
|
94
|
+
{
|
|
95
|
+
file: '(prettier)',
|
|
96
|
+
check: 'prettier-format',
|
|
97
|
+
message: `prettier exited ${String(result.exitCode)} without completing the check: ` +
|
|
98
|
+
output.trim().split('\n').slice(-5).join(' ').slice(0, 400),
|
|
99
|
+
severity,
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
const unformatted = new Set();
|
|
104
|
+
for (const line of output.split(/\r?\n/)) {
|
|
105
|
+
const trimmed = line.trim();
|
|
106
|
+
if (PRETTIER_SUMMARY_LINE.test(trimmed))
|
|
107
|
+
continue;
|
|
108
|
+
const match = PRETTIER_WARN_LINE.exec(trimmed);
|
|
109
|
+
const file = match?.[1]?.trim();
|
|
110
|
+
if (file !== undefined && file.length > 0)
|
|
111
|
+
unformatted.add(file);
|
|
112
|
+
}
|
|
113
|
+
if (unformatted.size === 0) {
|
|
114
|
+
verbose('patchLint.prettier: prettier exited 1 but named no file; reporting nothing.');
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
return [...unformatted].sort().map((file) => ({
|
|
118
|
+
file,
|
|
119
|
+
check: 'prettier-format',
|
|
120
|
+
message: `Not formatted per the project's Prettier configuration. Checked from inside engine/, ` +
|
|
121
|
+
`so engine/.prettierrc* and engine/.prettierignore apply — the same command run from the ` +
|
|
122
|
+
`repo root can report a false pass when the root .prettierignore excludes engine/. ` +
|
|
123
|
+
`Fix with "prettier --write ${file}" run from engine/.`,
|
|
124
|
+
severity,
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recognizes the one shape where the undefined-identifier hint sends the
|
|
3
|
+
* operator at the wrong remedy — and where taking that remedy would MASK a
|
|
4
|
+
* real bug.
|
|
5
|
+
*
|
|
6
|
+
* The shape: helpers are hoisted out of duplicated test files into a new
|
|
7
|
+
* `head_settings.js` that a managed `head.js` pulls in with
|
|
8
|
+
* `loadSubScript`. Until that companion is exported it belongs to no patch,
|
|
9
|
+
* so the per-patch checkJs program — which only ever loads patch-owned
|
|
10
|
+
* roots — cannot see any of its declarations. Every use of every hoisted
|
|
11
|
+
* helper is then reported as `Cannot find name`, and the generic hint
|
|
12
|
+
* offers three remedies of which the shim is the easiest: adding the
|
|
13
|
+
* globals to `extraShim` makes all of the warnings disappear AND makes a
|
|
14
|
+
* genuinely missing helper undetectable forever after.
|
|
15
|
+
*
|
|
16
|
+
* The warnings themselves are correct — the per-patch program's honest view
|
|
17
|
+
* of a file that is in no patch. Only the advice is wrong. FireForge
|
|
18
|
+
* already knows ownership, so when every undefined name in a file is
|
|
19
|
+
* declared at the top level of an UNMANAGED file that a managed head loads,
|
|
20
|
+
* it can name that file and the adoption command instead.
|
|
21
|
+
*
|
|
22
|
+
* Detection is deliberately conservative on both halves: a companion must
|
|
23
|
+
* be reachable from an actual `loadSubScript` call, and EVERY undefined
|
|
24
|
+
* name must resolve inside it. One unresolved name means the file may
|
|
25
|
+
* genuinely be missing a helper, and the generic hint stays.
|
|
26
|
+
*/
|
|
27
|
+
import type { PatchLintIssue } from '../types/commands/index.js';
|
|
28
|
+
/**
|
|
29
|
+
* String literals passed anywhere inside a `loadSubScript(...)` call.
|
|
30
|
+
*
|
|
31
|
+
* The mochitest idiom is
|
|
32
|
+
* `Services.scriptloader.loadSubScript(getRootDirectory(gTestPath) + "head_settings.js", this)`,
|
|
33
|
+
* so the useful signal is the `.js` literal inside the call, not a
|
|
34
|
+
* resolvable URL — the path half is computed at runtime.
|
|
35
|
+
*/
|
|
36
|
+
export declare function findLoadSubScriptTargets(source: string): string[];
|
|
37
|
+
/**
|
|
38
|
+
* Top-level declaration names in a script-scope file.
|
|
39
|
+
*
|
|
40
|
+
* Regex rather than a TS program on purpose: this runs only to decide the
|
|
41
|
+
* WORDING of a hint, the file is by definition outside every program the
|
|
42
|
+
* pass built, and a missed declaration degrades to the generic hint rather
|
|
43
|
+
* than to a wrong answer. Matching is anchored at column zero so a nested
|
|
44
|
+
* declaration inside a function body is not mistaken for a global.
|
|
45
|
+
*/
|
|
46
|
+
export declare function collectTopLevelDeclarations(source: string): Set<string>;
|
|
47
|
+
/** An unmanaged companion a managed head loads, plus what it declares. */
|
|
48
|
+
export interface UnmanagedCompanion {
|
|
49
|
+
/** Repo-relative path of the companion. */
|
|
50
|
+
file: string;
|
|
51
|
+
/** Repo-relative path of the managed head that loads it. */
|
|
52
|
+
loadedBy: string;
|
|
53
|
+
/** Top-level names the companion declares. */
|
|
54
|
+
declarations: Set<string>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolves the unmanaged companions reachable from a set of managed head
|
|
58
|
+
* files, given a reader for repo-relative file content.
|
|
59
|
+
*
|
|
60
|
+
* A candidate qualifies only when it is NOT in `ownedFiles` and its content
|
|
61
|
+
* can be read — an unreadable or absent target is simply not a companion,
|
|
62
|
+
* and the generic hint applies.
|
|
63
|
+
*
|
|
64
|
+
* @param heads - Repo-relative managed head files, with their sources
|
|
65
|
+
* @param ownedFiles - Every patch-owned repo-relative path
|
|
66
|
+
* @param readFile - Reads a repo-relative file, or returns undefined
|
|
67
|
+
*/
|
|
68
|
+
export declare function resolveUnmanagedCompanions(heads: ReadonlyArray<{
|
|
69
|
+
file: string;
|
|
70
|
+
source: string;
|
|
71
|
+
}>, ownedFiles: ReadonlySet<string>, readFile: (repoRelative: string) => Promise<string | undefined>): Promise<UnmanagedCompanion[]>;
|
|
72
|
+
/**
|
|
73
|
+
* The replacement hint, when every undefined name resolves in one
|
|
74
|
+
* unmanaged companion.
|
|
75
|
+
*
|
|
76
|
+
* States the ownership fact first (that is the diagnosis), then the
|
|
77
|
+
* adoption command, and explicitly rules the shim OUT — the shim is what an
|
|
78
|
+
* operator reaches for by default here, and it is the one remedy that
|
|
79
|
+
* permanently hides a genuinely missing helper.
|
|
80
|
+
*/
|
|
81
|
+
export declare function formatUnmanagedCompanionHint(companion: UnmanagedCompanion): string;
|
|
82
|
+
/**
|
|
83
|
+
* Picks the companion that explains EVERY undefined identifier in
|
|
84
|
+
* `messages`, or undefined when none does.
|
|
85
|
+
*
|
|
86
|
+
* @param messages - Diagnostic messages reported for one file
|
|
87
|
+
* @param companions - Candidates from {@link resolveUnmanagedCompanions}
|
|
88
|
+
*/
|
|
89
|
+
export declare function explainUndefinedIdentifiers(messages: readonly string[], companions: readonly UnmanagedCompanion[]): UnmanagedCompanion | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Reads the head files in the owned test set and resolves the UNMANAGED
|
|
92
|
+
* companions they pull in with `loadSubScript`. Hoisting shared helpers
|
|
93
|
+
* into such a companion leaves it unowned until export, and every use of
|
|
94
|
+
* every helper then reports `Cannot find name` from an honest per-patch
|
|
95
|
+
* program — see `patch-lint-unmanaged-companion.ts` for why the generic
|
|
96
|
+
* hint is actively harmful there.
|
|
97
|
+
*
|
|
98
|
+
* Best-effort: an unreadable head simply contributes no companions.
|
|
99
|
+
*/
|
|
100
|
+
export declare function collectUnmanagedCompanions(repoDir: string, files: readonly string[], ownedFiles: ReadonlySet<string>): Promise<UnmanagedCompanion[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Swaps the generic undefined-identifier hint for the ownership hint when
|
|
103
|
+
* one unmanaged companion explains EVERY undefined name in the file. Any
|
|
104
|
+
* other issue in the list is returned untouched.
|
|
105
|
+
*/
|
|
106
|
+
export declare function retargetUnmanagedCompanionHints(issues: readonly PatchLintIssue[], companions: readonly UnmanagedCompanion[]): PatchLintIssue[];
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EUPL-1.2
|
|
2
|
+
/**
|
|
3
|
+
* Recognizes the one shape where the undefined-identifier hint sends the
|
|
4
|
+
* operator at the wrong remedy — and where taking that remedy would MASK a
|
|
5
|
+
* real bug.
|
|
6
|
+
*
|
|
7
|
+
* The shape: helpers are hoisted out of duplicated test files into a new
|
|
8
|
+
* `head_settings.js` that a managed `head.js` pulls in with
|
|
9
|
+
* `loadSubScript`. Until that companion is exported it belongs to no patch,
|
|
10
|
+
* so the per-patch checkJs program — which only ever loads patch-owned
|
|
11
|
+
* roots — cannot see any of its declarations. Every use of every hoisted
|
|
12
|
+
* helper is then reported as `Cannot find name`, and the generic hint
|
|
13
|
+
* offers three remedies of which the shim is the easiest: adding the
|
|
14
|
+
* globals to `extraShim` makes all of the warnings disappear AND makes a
|
|
15
|
+
* genuinely missing helper undetectable forever after.
|
|
16
|
+
*
|
|
17
|
+
* The warnings themselves are correct — the per-patch program's honest view
|
|
18
|
+
* of a file that is in no patch. Only the advice is wrong. FireForge
|
|
19
|
+
* already knows ownership, so when every undefined name in a file is
|
|
20
|
+
* declared at the top level of an UNMANAGED file that a managed head loads,
|
|
21
|
+
* it can name that file and the adoption command instead.
|
|
22
|
+
*
|
|
23
|
+
* Detection is deliberately conservative on both halves: a companion must
|
|
24
|
+
* be reachable from an actual `loadSubScript` call, and EVERY undefined
|
|
25
|
+
* name must resolve inside it. One unresolved name means the file may
|
|
26
|
+
* genuinely be missing a helper, and the generic hint stays.
|
|
27
|
+
*/
|
|
28
|
+
import { readFile } from 'node:fs/promises';
|
|
29
|
+
import { join } from 'node:path';
|
|
30
|
+
import { UNDEFINED_IDENTIFIER_HINT } from './typecheck-shim.js';
|
|
31
|
+
/**
|
|
32
|
+
* String literals passed anywhere inside a `loadSubScript(...)` call.
|
|
33
|
+
*
|
|
34
|
+
* The mochitest idiom is
|
|
35
|
+
* `Services.scriptloader.loadSubScript(getRootDirectory(gTestPath) + "head_settings.js", this)`,
|
|
36
|
+
* so the useful signal is the `.js` literal inside the call, not a
|
|
37
|
+
* resolvable URL — the path half is computed at runtime.
|
|
38
|
+
*/
|
|
39
|
+
export function findLoadSubScriptTargets(source) {
|
|
40
|
+
const targets = [];
|
|
41
|
+
// Scanned to the statement terminator rather than to a closing paren:
|
|
42
|
+
// the idiomatic argument is itself a call (`getRootDirectory(gTestPath) +
|
|
43
|
+
// "head_settings.js"`), so a non-greedy `\)` stops on the INNER paren and
|
|
44
|
+
// never reaches the literal.
|
|
45
|
+
for (const call of source.matchAll(/loadSubScript\s*\(/g)) {
|
|
46
|
+
const from = call.index + call[0].length;
|
|
47
|
+
const semicolon = source.indexOf(';', from);
|
|
48
|
+
const to = semicolon === -1 ? from + CALL_SCAN_LIMIT : Math.min(semicolon, from + CALL_SCAN_LIMIT);
|
|
49
|
+
for (const literal of source.slice(from, to).matchAll(/["'`]([^"'`]*\.js)["'`]/g)) {
|
|
50
|
+
const value = literal[1];
|
|
51
|
+
if (value !== undefined && value.length > 0)
|
|
52
|
+
targets.push(value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return targets;
|
|
56
|
+
}
|
|
57
|
+
/** How far past a `loadSubScript(` the argument scan looks. */
|
|
58
|
+
const CALL_SCAN_LIMIT = 400;
|
|
59
|
+
/**
|
|
60
|
+
* Top-level declaration names in a script-scope file.
|
|
61
|
+
*
|
|
62
|
+
* Regex rather than a TS program on purpose: this runs only to decide the
|
|
63
|
+
* WORDING of a hint, the file is by definition outside every program the
|
|
64
|
+
* pass built, and a missed declaration degrades to the generic hint rather
|
|
65
|
+
* than to a wrong answer. Matching is anchored at column zero so a nested
|
|
66
|
+
* declaration inside a function body is not mistaken for a global.
|
|
67
|
+
*/
|
|
68
|
+
export function collectTopLevelDeclarations(source) {
|
|
69
|
+
const names = new Set();
|
|
70
|
+
const patterns = [
|
|
71
|
+
/^(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/gm,
|
|
72
|
+
/^(?:const|let|var)\s+([A-Za-z_$][\w$]*)/gm,
|
|
73
|
+
/^class\s+([A-Za-z_$][\w$]*)/gm,
|
|
74
|
+
];
|
|
75
|
+
for (const pattern of patterns) {
|
|
76
|
+
for (const match of source.matchAll(pattern)) {
|
|
77
|
+
const name = match[1];
|
|
78
|
+
if (name !== undefined)
|
|
79
|
+
names.add(name);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return names;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Extracts the identifier from a `Cannot find name 'X'` diagnostic message,
|
|
86
|
+
* or undefined when the message is not one.
|
|
87
|
+
*/
|
|
88
|
+
function extractUndefinedIdentifier(message) {
|
|
89
|
+
const match = /Cannot find name '([^']+)'/.exec(message);
|
|
90
|
+
return match?.[1];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Resolves the unmanaged companions reachable from a set of managed head
|
|
94
|
+
* files, given a reader for repo-relative file content.
|
|
95
|
+
*
|
|
96
|
+
* A candidate qualifies only when it is NOT in `ownedFiles` and its content
|
|
97
|
+
* can be read — an unreadable or absent target is simply not a companion,
|
|
98
|
+
* and the generic hint applies.
|
|
99
|
+
*
|
|
100
|
+
* @param heads - Repo-relative managed head files, with their sources
|
|
101
|
+
* @param ownedFiles - Every patch-owned repo-relative path
|
|
102
|
+
* @param readFile - Reads a repo-relative file, or returns undefined
|
|
103
|
+
*/
|
|
104
|
+
export async function resolveUnmanagedCompanions(heads, ownedFiles, readFile) {
|
|
105
|
+
const companions = [];
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
for (const head of heads) {
|
|
108
|
+
const dir = head.file.slice(0, head.file.lastIndexOf('/') + 1);
|
|
109
|
+
for (const target of findLoadSubScriptTargets(head.source)) {
|
|
110
|
+
// Only same-directory targets: the runtime path is computed from the
|
|
111
|
+
// test's own directory, and a bare basename is what that idiom yields.
|
|
112
|
+
const candidate = `${dir}${target.slice(target.lastIndexOf('/') + 1)}`;
|
|
113
|
+
if (candidate === head.file || ownedFiles.has(candidate) || seen.has(candidate))
|
|
114
|
+
continue;
|
|
115
|
+
seen.add(candidate);
|
|
116
|
+
const source = await readFile(candidate);
|
|
117
|
+
if (source === undefined)
|
|
118
|
+
continue;
|
|
119
|
+
companions.push({
|
|
120
|
+
file: candidate,
|
|
121
|
+
loadedBy: head.file,
|
|
122
|
+
declarations: collectTopLevelDeclarations(source),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return companions;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The replacement hint, when every undefined name resolves in one
|
|
130
|
+
* unmanaged companion.
|
|
131
|
+
*
|
|
132
|
+
* States the ownership fact first (that is the diagnosis), then the
|
|
133
|
+
* adoption command, and explicitly rules the shim OUT — the shim is what an
|
|
134
|
+
* operator reaches for by default here, and it is the one remedy that
|
|
135
|
+
* permanently hides a genuinely missing helper.
|
|
136
|
+
*/
|
|
137
|
+
export function formatUnmanagedCompanionHint(companion) {
|
|
138
|
+
return (`(undefined identifier — every one of them is declared in ${companion.file}, which ` +
|
|
139
|
+
`${companion.loadedBy} loads via loadSubScript but which NO patch owns yet, so the ` +
|
|
140
|
+
`per-patch program cannot see it. Adopt it with ` +
|
|
141
|
+
`"fireforge re-export <patch> --scan --scan-file ${companion.file}". Do NOT add these ` +
|
|
142
|
+
`globals to the extra shim: that silences these warnings and every future genuinely ` +
|
|
143
|
+
`missing helper with them.)`);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Picks the companion that explains EVERY undefined identifier in
|
|
147
|
+
* `messages`, or undefined when none does.
|
|
148
|
+
*
|
|
149
|
+
* @param messages - Diagnostic messages reported for one file
|
|
150
|
+
* @param companions - Candidates from {@link resolveUnmanagedCompanions}
|
|
151
|
+
*/
|
|
152
|
+
export function explainUndefinedIdentifiers(messages, companions) {
|
|
153
|
+
const names = messages
|
|
154
|
+
.map((message) => extractUndefinedIdentifier(message))
|
|
155
|
+
.filter((name) => name !== undefined);
|
|
156
|
+
if (names.length === 0)
|
|
157
|
+
return undefined;
|
|
158
|
+
// Only the undefined-identifier diagnostics are explained here; a file
|
|
159
|
+
// carrying OTHER type errors still gets those reported untouched.
|
|
160
|
+
return companions.find((companion) => names.every((name) => companion.declarations.has(name)));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Reads the head files in the owned test set and resolves the UNMANAGED
|
|
164
|
+
* companions they pull in with `loadSubScript`. Hoisting shared helpers
|
|
165
|
+
* into such a companion leaves it unowned until export, and every use of
|
|
166
|
+
* every helper then reports `Cannot find name` from an honest per-patch
|
|
167
|
+
* program — see `patch-lint-unmanaged-companion.ts` for why the generic
|
|
168
|
+
* hint is actively harmful there.
|
|
169
|
+
*
|
|
170
|
+
* Best-effort: an unreadable head simply contributes no companions.
|
|
171
|
+
*/
|
|
172
|
+
export async function collectUnmanagedCompanions(repoDir, files, ownedFiles) {
|
|
173
|
+
const heads = [];
|
|
174
|
+
for (const file of files) {
|
|
175
|
+
const base = file.split('/').pop() ?? '';
|
|
176
|
+
if (!/^head(?:_.*)?\.js$/.test(base))
|
|
177
|
+
continue;
|
|
178
|
+
try {
|
|
179
|
+
heads.push({ file, source: await readFile(join(repoDir, file), 'utf8') });
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (heads.length === 0)
|
|
186
|
+
return [];
|
|
187
|
+
return resolveUnmanagedCompanions(heads, ownedFiles, async (rel) => {
|
|
188
|
+
try {
|
|
189
|
+
return await readFile(join(repoDir, rel), 'utf8');
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Swaps the generic undefined-identifier hint for the ownership hint when
|
|
198
|
+
* one unmanaged companion explains EVERY undefined name in the file. Any
|
|
199
|
+
* other issue in the list is returned untouched.
|
|
200
|
+
*/
|
|
201
|
+
export function retargetUnmanagedCompanionHints(issues, companions) {
|
|
202
|
+
if (companions.length === 0)
|
|
203
|
+
return [...issues];
|
|
204
|
+
const undefinedIssues = issues.filter((issue) => issue.message.includes(UNDEFINED_IDENTIFIER_HINT));
|
|
205
|
+
if (undefinedIssues.length === 0)
|
|
206
|
+
return [...issues];
|
|
207
|
+
const companion = explainUndefinedIdentifiers(undefinedIssues.map((issue) => issue.message), companions);
|
|
208
|
+
if (!companion)
|
|
209
|
+
return [...issues];
|
|
210
|
+
const hint = formatUnmanagedCompanionHint(companion);
|
|
211
|
+
return issues.map((issue) => issue.message.includes(UNDEFINED_IDENTIFIER_HINT)
|
|
212
|
+
? { ...issue, message: issue.message.replace(UNDEFINED_IDENTIFIER_HINT, hint) }
|
|
213
|
+
: issue);
|
|
214
|
+
}
|