Skip to content

Commit f86d6f5

Browse files
committed
refactor(scripts): guard packaged runner includes generically in the packager
The header-mirror requirement was spelled per header in the size reporters, which the Bundle Size job mixes across base and head copies, and it needed a new module plus a test outside unit-core. Move the rule into the packager instead: after packaging, every relative line-start `#import "…"` / `#include "…"` in a packaged native source must resolve to a shipped file, with the mirrored shared header as the one allowed exit from the runner tree. No header name, no size-report coupling: the packagers and install checks return to their pre-split shape plus the new bridge asset, and prose that merely quotes a path is not read as an include.
1 parent c73e0da commit f86d6f5

7 files changed

Lines changed: 130 additions & 168 deletions

File tree

‎scripts/__tests__/size-report-geometry.test.ts‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎scripts/ios-snapshot-benchmark/size-install.test.ts‎

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -67,61 +67,3 @@ test('clean-installed snapshot bridge validates all native assets when present',
6767
await rm(root, { recursive: true, force: true });
6868
}
6969
});
70-
71-
test('clean-installed package requires the shared header mirror only when the runner includes it', async () => {
72-
const root = await mkdtempForTest('agent-device-size-header-');
73-
const bridge = join(root, 'apple', 'snapshot-bridge');
74-
const runner = join(
75-
root,
76-
'dist',
77-
'apple',
78-
'runner',
79-
'AgentDeviceRunner',
80-
'AgentDeviceRunnerUITests',
81-
);
82-
const bridgeNames = [
83-
'SnapshotBridge.m',
84-
'SnapshotBridgeRuntime.m',
85-
'SnapshotBridgeRuntime.h',
86-
'SnapshotBridgeCapture.h',
87-
'SnapshotBridgeCapture.m',
88-
'ForegroundApplicationGeometry.h',
89-
];
90-
const writeBridge = async () => {
91-
await mkdir(bridge, { recursive: true });
92-
for (const name of bridgeNames) {
93-
await writeFile(join(bridge, name), 'native source');
94-
}
95-
};
96-
try {
97-
await writeBridge();
98-
await mkdir(runner, { recursive: true });
99-
// A packaged runner whose sources never include the shared header is a legacy package: the
100-
// mirror is nobody's include target, so nothing is required.
101-
await writeFile(join(runner, 'RunnerXCTestEventBridge.m'), '// predates the shared header\n');
102-
assert.doesNotThrow(() => assertInstalledSnapshotBridge(root));
103-
104-
await writeFile(
105-
join(runner, 'RunnerXCTestEventBridge.m'),
106-
'#import "../../../snapshot-bridge/ForegroundApplicationGeometry.h"\n',
107-
);
108-
await rm(bridge, { recursive: true, force: true });
109-
assert.throws(
110-
() => assertInstalledSnapshotBridge(root),
111-
/dist\/apple\/snapshot-bridge\/ForegroundApplicationGeometry\.h/,
112-
'the runner include requires the mirror even without the apple/snapshot-bridge directory',
113-
);
114-
await writeBridge();
115-
assert.throws(() => assertInstalledSnapshotBridge(root), /ForegroundApplicationGeometry\.h/);
116-
await mkdir(join(root, 'dist', 'apple', 'snapshot-bridge'), { recursive: true });
117-
await writeFile(join(root, 'dist', 'apple', 'snapshot-bridge', 'x.h'), '');
118-
assert.throws(() => assertInstalledSnapshotBridge(root), /ForegroundApplicationGeometry\.h/);
119-
await writeFile(
120-
join(root, 'dist', 'apple', 'snapshot-bridge', 'ForegroundApplicationGeometry.h'),
121-
'#pragma once\n',
122-
);
123-
assert.doesNotThrow(() => assertInstalledSnapshotBridge(root));
124-
} finally {
125-
await rm(root, { recursive: true, force: true });
126-
}
127-
});

‎scripts/package-apple-runner-source.mjs‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const SHIPPED_TEST_METHOD_ALLOWLIST = new Map([
5757
],
5858
]);
5959
const TEST_METHOD_PATTERN = /^\s*(?:@\w+(?:\([^)]*\))?\s+)*(?:\w+\s+)*func\s+(test\w*)\s*\(/;
60+
// A line-start `#import "…"` / `#include "…"`. Anchored to the directive so prose and comments
61+
// that merely quote a path are not reads as includes.
62+
const QUOTED_NATIVE_INCLUDE = /^[ \t]*#[ \t]*(?:import|include)[ \t]+"([^"]+)"[ \t]*$/;
6063

6164
function packageAppleRunnerSource(options = {}) {
6265
const root = path.resolve(options.root ?? process.cwd());
@@ -74,13 +77,17 @@ function packageAppleRunnerSource(options = {}) {
7477
strippedBlocks: 0,
7578
strippedComments: 0,
7679
strippedCommentBytes: 0,
80+
packagedNativeSourcePaths: [],
81+
checkOnly: Boolean(options.checkOnly),
7782
};
7883

7984
processDirectory(sourceRoot, options.checkOnly ? undefined : outputRoot, '', summary, {
8085
skipFilePaths: SKIPPED_RUNNER_FILE_PATHS,
86+
trackNativeSources: true,
8187
});
8288
packageSnapshotPresentationSource(root, options, summary);
8389
packageSharedHeader(root, options, summary);
90+
assertPackagedNativeIncludesResolve(root, summary);
8491
return summary;
8592
}
8693

@@ -99,6 +106,42 @@ function packageSharedHeader(root, options, summary) {
99106
summary.copiedFiles += 1;
100107
}
101108

109+
// A relative quoted include resolves through the packaged directory tree, so it can point at a
110+
// file the package does not ship — the checkout builds while a user's `xcodebuild` dies. Every
111+
// such include in a packaged native source must resolve inside the package after packaging.
112+
function assertPackagedNativeIncludesResolve(root, summary) {
113+
const packagedRoot = summary.checkOnly
114+
? path.join(root, SOURCE_DIR)
115+
: path.join(root, OUTPUT_DIR);
116+
const sharedTarget = path.resolve(
117+
root,
118+
summary.checkOnly ? SHARED_HEADER_SOURCE : SHARED_HEADER_OUTPUT,
119+
);
120+
for (const nativePath of summary.packagedNativeSourcePaths) {
121+
for (const line of fs.readFileSync(nativePath, 'utf8').split('\n')) {
122+
const target = QUOTED_NATIVE_INCLUDE.exec(line.trimEnd())?.[1];
123+
if (target?.includes('/') && !path.isAbsolute(target)) {
124+
assertPackagedIncludeResolves(root, nativePath, target, packagedRoot, sharedTarget);
125+
}
126+
}
127+
}
128+
}
129+
130+
function assertPackagedIncludeResolves(root, nativePath, target, packagedRoot, sharedTarget) {
131+
const resolved = path.resolve(path.dirname(nativePath), target);
132+
const label = path.relative(root, nativePath);
133+
if (!resolved.startsWith(packagedRoot + path.sep) && resolved !== sharedTarget) {
134+
throw new Error(
135+
`Runner source ${label} includes "${target}", which resolves outside the packaged tree.`,
136+
);
137+
}
138+
if (!fs.existsSync(resolved)) {
139+
throw new Error(
140+
`Runner source ${label} includes "${target}", which resolves to a file the package does not ship.`,
141+
);
142+
}
143+
}
144+
102145
function packageSnapshotPresentationSource(root, options, summary) {
103146
const sourceRoot = path.join(root, SNAPSHOT_PRESENTATION_SOURCE_DIR);
104147
if (!fs.existsSync(sourceRoot)) {
@@ -226,6 +269,12 @@ function processIncludedEntry(entry, sourceDir, outputDir, relativePath, summary
226269
}
227270

228271
function processFile(sourcePath, outputPath, relativePath, summary, options) {
272+
if (
273+
options.trackNativeSources &&
274+
(path.extname(sourcePath) === '.m' || path.extname(sourcePath) === '.h')
275+
) {
276+
summary.packagedNativeSourcePaths.push(outputPath ?? sourcePath);
277+
}
229278
if (outputPath) {
230279
copyFile(sourcePath, outputPath, relativePath, summary, options);
231280
return;
@@ -234,7 +283,8 @@ function processFile(sourcePath, outputPath, relativePath, summary, options) {
234283
}
235284

236285
function copyFile(sourcePath, outputPath, relativePath, summary, options) {
237-
if (path.extname(sourcePath) !== '.swift') {
286+
const extension = path.extname(sourcePath);
287+
if (extension !== '.swift') {
238288
fs.copyFileSync(sourcePath, outputPath);
239289
summary.copiedFiles += 1;
240290
return;

‎scripts/size-report-geometry.mjs‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎scripts/size-report-install.mjs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { execFileSync } from 'node:child_process';
22
import fs from 'node:fs';
33
import os from 'node:os';
44
import path from 'node:path';
5-
import { assertPackagedRunnerSharedHeaderMirror } from './size-report-geometry.mjs';
65
import { assertSnapshotBridgeAssets, SNAPSHOT_BRIDGE_ASSET_PATHS } from './size-report-package.mjs';
76

87
export function measureCleanInstalledPackage(tarballPath, packageName) {
@@ -46,9 +45,6 @@ export function measureCleanInstalledPackage(tarballPath, packageName) {
4645
}
4746

4847
export function assertInstalledSnapshotBridge(packageDir) {
49-
// The runner include check reads the packaged runner itself, so it runs before the bridge
50-
// directory guard: a package that ships the include without the bridge directory must still fail.
51-
assertPackagedRunnerSharedHeaderMirror(packageDir);
5248
const bridgeRoot = path.join(packageDir, 'apple', 'snapshot-bridge');
5349
if (!fs.existsSync(bridgeRoot)) return;
5450
const present = SNAPSHOT_BRIDGE_ASSET_PATHS.filter((assetPath) =>

‎scripts/size-report-package.mjs‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { execFileSync } from 'node:child_process';
22
import fs from 'node:fs';
33
import path from 'node:path';
4-
import { assertPackagedRunnerSharedHeaderMirror } from './size-report-geometry.mjs';
54
export const SNAPSHOT_BRIDGE_ASSET_PATHS = Object.freeze([
65
'apple/snapshot-bridge/SnapshotBridge.m',
76
'apple/snapshot-bridge/SnapshotBridgeRuntime.m',
@@ -40,10 +39,6 @@ export function collectNpmPack(root) {
4039
);
4140
const pack = parseNpmPackOutput(stdout);
4241
const entries = normalizeNpmPackEntries(pack);
43-
const paths = entries.map((entry) => entry.path);
44-
// The mirror is required exactly when the packaged runner includes the shared header; deriving
45-
// it from the runner's sources lets a pack of an older tree validate too.
46-
assertPackagedRunnerSharedHeaderMirror(root, paths);
4742
assertPublishPackageContents(entries, {
4843
repoRoot: root,
4944
requireSnapshotBridge: fs.existsSync(path.join(root, 'apple', 'snapshot-bridge')),

‎test/repo-guards/apple-runner-package-source.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ test('package apple runner source mirrors the shared header next to the packaged
405405
);
406406
writeFixtureFile(root, 'apple/snapshot-presentation/Package.runner.swift', 'runner package\n');
407407
writeFixtureFile(root, 'apple/snapshot-bridge/ForegroundApplicationGeometry.h', '#pragma once\n');
408+
writeFixtureFile(
409+
root,
410+
'apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerXCTestEventBridge.m',
411+
'#import "../../../snapshot-bridge/ForegroundApplicationGeometry.h"\n',
412+
);
408413

409414
await runCmd(process.execPath, [packageScript, '--root', root, '--quiet']);
410415

@@ -417,6 +422,80 @@ test('package apple runner source mirrors the shared header next to the packaged
417422
);
418423
});
419424

425+
test('package apple runner source rejects a packaged include that ships no file', async () => {
426+
const root = mkdtempForTestSync('agent-device-runner-package-phantom-');
427+
onTestFinished(() => fs.rmSync(root, { recursive: true, force: true }));
428+
writeFixtureFile(
429+
root,
430+
'apple/runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj',
431+
'',
432+
);
433+
writeFixtureFile(root, 'apple/snapshot-presentation/Package.runner.swift', 'runner package\n');
434+
writeFixtureFile(root, 'apple/snapshot-bridge/ForegroundApplicationGeometry.h', '#pragma once\n');
435+
writeFixtureFile(
436+
root,
437+
'apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerPhantom.m',
438+
'#import "../../gone/Phantom.h"\n',
439+
);
440+
441+
const result = await runCmd(process.execPath, [packageScript, '--root', root, '--quiet'], {
442+
allowFailure: true,
443+
});
444+
445+
assert.notEqual(result.exitCode, 0);
446+
assert.match(result.stderr, /RunnerPhantom\.m includes "\.\.\/\.\.\/gone\/Phantom\.h"/);
447+
});
448+
449+
test('package apple runner source rejects an include that resolves outside the packaged runner', async () => {
450+
const root = mkdtempForTestSync('agent-device-runner-package-checkout-only-');
451+
onTestFinished(() => fs.rmSync(root, { recursive: true, force: true }));
452+
writeFixtureFile(
453+
root,
454+
'apple/runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj',
455+
'',
456+
);
457+
writeFixtureFile(root, 'apple/snapshot-presentation/Package.runner.swift', 'runner package\n');
458+
writeFixtureFile(root, 'apple/snapshot-bridge/ForegroundApplicationGeometry.h', '#pragma once\n');
459+
// The target exists in the checkout and even somewhere under dist, so the checkout build works
460+
// and a dist-only grep would pass; the include still escapes the packaged runner tree, where no
461+
// user's xcodebuild would find it. Only the mirrored shared header may leave the tree.
462+
writeFixtureFile(root, 'Outside/Outside.h', 'checkout sibling only\n');
463+
writeFixtureFile(
464+
root,
465+
'apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerCheckoutOnly.m',
466+
'#import "../../../../../Outside/Outside.h"\n',
467+
);
468+
469+
const result = await runCmd(process.execPath, [packageScript, '--root', root, '--quiet'], {
470+
allowFailure: true,
471+
});
472+
473+
assert.notEqual(result.exitCode, 0);
474+
assert.match(result.stderr, /resolves outside the packaged tree/);
475+
});
476+
477+
test('package apple runner source ignores quoted paths that are not line-start includes', async () => {
478+
const root = mkdtempForTestSync('agent-device-runner-package-prose-');
479+
onTestFinished(() => fs.rmSync(root, { recursive: true, force: true }));
480+
writeFixtureFile(
481+
root,
482+
'apple/runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj',
483+
'',
484+
);
485+
writeFixtureFile(root, 'apple/snapshot-presentation/Package.runner.swift', 'runner package\n');
486+
writeFixtureFile(root, 'apple/snapshot-bridge/ForegroundApplicationGeometry.h', '#pragma once\n');
487+
writeFixtureFile(
488+
root,
489+
'apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerProse.m',
490+
[
491+
'// The flag reads "../../gone/Phantom.h" in documentation.',
492+
'int RunnerProse(void) { return 0; }',
493+
].join('\n'),
494+
);
495+
496+
await runCmd(process.execPath, [packageScript, '--root', root, '--quiet']);
497+
});
498+
420499
test('snapshot presentation manifests keep their supported platform declarations in parity', () => {
421500
const manifest = fs.readFileSync(
422501
path.join(repoRoot, 'apple/snapshot-presentation/Package.swift'),

0 commit comments

Comments
 (0)