Repository navigation
Expand file tree
/
Copy pathcheck-packaged-runner-swift.ts
More file actions
228 lines (215 loc) · 9.6 KB
/
Copy pathcheck-packaged-runner-swift.ts
File metadata and controls
228 lines (215 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// `pnpm check:packaged-runner-swift` — hold the two properties of the Apple runner source the npm
// package ships that nothing else in this repo can observe.
//
// `apple/runner/**` is rewritten on its way into `dist/apple/runner/**`: unit-test `#if` blocks
// come out, and `scripts/strip-swift-comments.mjs` takes the comments out (#2461). Nothing in the
// repo compiles, imports or reads the result — the first consumer is a user's `xcodebuild` — so
// both the Swift staying valid and its line numbers staying meaningful are claims only a check
// makes. This is that check:
//
// 1. Line parity. Every rewritten line is emptied, never deleted, so packaged line N is
// checkout line N. An `xcodebuild`/runner failure names the packaged path (it lands in
// `runner.log`), and the number it prints is only worth reading if it points at the same
// line of `apple/runner/**`. Asserted two ways per file: the line count, and the line every
// declaration the packaged file still carries sits on.
// 2. It parses. `xcrun swiftc -parse` over every packaged Swift file. The scanner throws on a
// construct it cannot lex rather than shipping, but a future mis-lex that fails to throw
// would ship Swift that does not compile, and no other gate would notice.
//
// Packaging runs into a throwaway root over a symlinked `apple/`, so the gate reads the bytes the
// packager really writes without touching `dist/`.
//
// Property 1 needs no toolchain and runs everywhere. Property 2 needs Swift: on a host without it
// (Linux CI) the parse is reported as skipped rather than failing, which is why the gate is
// declared on the macOS lane, where it is the half that matters.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { runCmdSync } from '@agent-device/host-kit/command';
const repoRoot = path.resolve(import.meta.dirname, '..');
const packageAppleRunnerScript = path.join(repoRoot, 'scripts/package-apple-runner-source.mjs');
const SOURCE_DIR = path.join('apple', 'runner');
const PACKAGED_DIR = path.join('dist', 'apple', 'runner');
/**
* A Swift declaration and its name. Matched per line and used as a line-number anchor, so it is
* deliberately independent of the rewrite: it reads the name out of whatever the line still holds
* rather than comparing the two lines, which a removed trailing comment would break.
*/
const DECLARATION = /\b(actor|class|enum|extension|func|protocol|struct)\s+([A-Za-z_]\w*)/;
/** The declaration each line declares, keyed by 1-based line number. */
export function declarationsByLine(text: string): Map<number, string> {
const declarations = new Map<number, string>();
for (const [index, line] of text.split('\n').entries()) {
const match = DECLARATION.exec(line);
if (match) declarations.set(index + 1, `${match[1]} ${match[2]}`);
}
return declarations;
}
/** Every way the packaged file's line numbering can disagree with the checkout's. */
export function parityFailures(
relativePath: string,
sourceText: string,
packagedText: string,
): string[] {
const sourceLines = sourceText.split('\n').length;
const packagedLines = packagedText.split('\n').length;
if (sourceLines !== packagedLines) {
return [
`${relativePath}: packaged source has ${packagedLines} lines, checkout has ${sourceLines}. ` +
'Packaging must empty a removed line, not delete it.',
];
}
const sourceDeclarations = declarationsByLine(sourceText);
return [...declarationsByLine(packagedText)]
.filter(([line, declaration]) => sourceDeclarations.get(line) !== declaration)
.map(
([line, declaration]) =>
`${relativePath}:${line}: packaged \`${declaration}\` is \`` +
`${sourceDeclarations.get(line) ?? '(blank)'}\` at the same line of the checkout.`,
);
}
function swiftFilesUnder(root: string, relativeDir = ''): string[] {
const entries = fs.readdirSync(path.join(root, relativeDir), { withFileTypes: true });
return entries.flatMap((entry) => {
const relativePath = path.join(relativeDir, entry.name);
if (entry.isDirectory()) return swiftFilesUnder(root, relativePath);
return entry.isFile() && entry.name.endsWith('.swift') ? [relativePath] : [];
});
}
/**
* Packages the runner into a disposable root. `apple/` is symlinked rather than copied: the
* packager only reads it, and the output path it derives (`<root>/dist/apple/runner`) is what has
* to land outside the checkout.
*/
function packageIntoScratchRoot(scratchRoot: string): string {
fs.symlinkSync(path.join(repoRoot, 'apple'), path.join(scratchRoot, 'apple'), 'dir');
runCmdSync(process.execPath, [packageAppleRunnerScript, '--root', scratchRoot, '--quiet']);
return path.join(scratchRoot, PACKAGED_DIR);
}
function lineParityFailures(packagedRoot: string, relativePaths: readonly string[]): string[] {
return relativePaths.flatMap((relativePath) => {
const sourcePath = path.join(repoRoot, SOURCE_DIR, relativePath);
if (!fs.existsSync(sourcePath)) {
return [`${path.join(PACKAGED_DIR, relativePath)} has no counterpart in ${SOURCE_DIR}.`];
}
return parityFailures(
path.join(PACKAGED_DIR, relativePath),
fs.readFileSync(sourcePath, 'utf8'),
fs.readFileSync(path.join(packagedRoot, relativePath), 'utf8'),
);
});
}
/** The Swift compiler, or undefined on a host that has none. */
function findSwiftc(): string | undefined {
if (process.platform !== 'darwin') return undefined;
try {
const found = runCmdSync('xcrun', ['--find', 'swiftc'], { allowFailure: true });
return found.exitCode === 0 && found.stdout.trim() !== '' ? found.stdout.trim() : undefined;
} catch {
// No `xcrun` on PATH at all: a macOS host without the command line tools.
return undefined;
}
}
/**
* Parses each packaged file on its own, so a diagnostic names the file that caused it and one
* file's top-level code cannot change how another is read.
*/
function swiftParseFailures(
swiftc: string,
packagedRoot: string,
relativePaths: readonly string[],
tmpDir: string,
): string[] {
return relativePaths.flatMap((relativePath) => {
const result = runCmdSync(swiftc, ['-parse', path.join(packagedRoot, relativePath)], {
allowFailure: true,
env: { ...process.env, TMPDIR: tmpDir },
});
if (result.exitCode === 0) return [];
// Swift names the scratch copy it was handed; say where that file comes from instead.
const detail = (result.stderr || result.stdout)
.replaceAll(`${packagedRoot}${path.sep}`, `${PACKAGED_DIR}${path.sep}`)
.trim();
return [`${path.join(PACKAGED_DIR, relativePath)} does not parse after packaging:\n${detail}`];
});
}
/**
* The packaged `RunnerXCTestEventBridge.m` includes the shared full-screen header by relative
* path (`../../../snapshot-bridge/ForegroundApplicationGeometry.h`). Resolve that include in the
* scratch packaging layout: a missing mirror means a user's `xcodebuild` of the installed
* package cannot compile the runner, and nothing in this repo would notice otherwise.
*/
function sharedHeaderFailures(scratchRoot: string): string[] {
const bridgeSource = path.join(
scratchRoot,
PACKAGED_DIR,
'AgentDeviceRunner',
'AgentDeviceRunnerUITests',
'RunnerXCTestEventBridge.m',
);
const source = fs.readFileSync(bridgeSource, 'utf8');
const include = /^\s*#import\s+"(\.\.[^"]*ForegroundApplicationGeometry\.h)"/m.exec(source);
if (!include) {
return [
`${PACKAGED_DIR}: RunnerXCTestEventBridge.m no longer includes the shared full-screen ` +
'header by relative path; the packaging mirror and this check must match the include.',
];
}
const resolved = path.resolve(path.dirname(bridgeSource), include[1]!);
if (!fs.existsSync(resolved)) {
return [
`Packaged source includes "${include[1]}" from RunnerXCTestEventBridge.m, which resolves ` +
`to ${resolved} — a path nothing packages.`,
];
}
const checkout = path.resolve(
repoRoot,
'apple',
'runner',
'AgentDeviceRunner',
'AgentDeviceRunnerUITests',
include[1]!,
);
if (!fs.existsSync(checkout)) {
return [`Checkout include "${include[1]}" does not resolve either: ${checkout}.`];
}
if (!fs.readFileSync(resolved).equals(fs.readFileSync(checkout))) {
return ['Packaged ForegroundApplicationGeometry.h differs from the checkout source.'];
}
return [];
}
function main(): number {
const scratchRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-packaged-swift-'));
try {
const packagedRoot = packageIntoScratchRoot(scratchRoot);
const relativePaths = swiftFilesUnder(packagedRoot);
const swiftc = findSwiftc();
const failures = [
...sharedHeaderFailures(scratchRoot),
...lineParityFailures(packagedRoot, relativePaths),
...(swiftc === undefined
? []
: swiftParseFailures(swiftc, packagedRoot, relativePaths, scratchRoot)),
];
if (failures.length > 0) {
process.stderr.write(`${failures.join('\n')}\n`);
process.stdout.write(
`packaged runner Swift: ${failures.length} failure(s) over ${relativePaths.length} files.\n`,
);
return 1;
}
const parse =
swiftc === undefined
? 'parse skipped, no Swift toolchain on this host'
: `all ${relativePaths.length} parse under swiftc -parse`;
process.stdout.write(
`packaged runner Swift: ok — ${relativePaths.length} files keep the checkout's line ` +
`numbering; ${parse}.\n`,
);
return 0;
} finally {
fs.rmSync(scratchRoot, { recursive: true, force: true });
}
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) process.exit(main());