Skip to content

Commit 86fd3f4

Browse files
committed
fix(ios): pin runner build roots under derived data
A custom or legacy build location in Xcode settings redirected runner products outside -derivedDataPath, so the xctestrun was never found. Pass SYMROOT, OBJROOT, and SHARED_PRECOMPS_DIR explicitly in the daemon build and build-xcuitest-apple.sh.
1 parent 9a245d0 commit 86fd3f4

6 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { afterEach, beforeEach, test, vi } from 'vitest';
5+
import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo';
6+
import { appleRunnerTestHost } from '../test-host.ts';
7+
import type { ExecResult } from '@agent-device/host-kit/command';
8+
import { ensureXctestrunArtifact } from '../runner-xctestrun.ts';
9+
import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts';
10+
import { IOS_SIMULATOR } from './device-fixtures.ts';
11+
import { seedRunnerProductBundle } from './runner-xctestrun.fixtures.ts';
12+
import { mkdtempForTestSync } from './tmp-dir.ts';
13+
14+
const XCTESTRUN_NAME =
15+
'AgentDeviceRunner_AgentDeviceRunnerUITests_iphonesimulator27.0-arm64.xctestrun';
16+
17+
const runCmdStreaming = vi.fn();
18+
let projectRoot: string;
19+
let derived: string;
20+
let customBuildLocation: string;
21+
22+
beforeEach(() => {
23+
resetAllProcessMemosForTests();
24+
projectRoot = mkdtempForTestSync('agent-device-runner-location-root-');
25+
fs.mkdirSync(
26+
path.join(projectRoot, 'apple', 'runner', 'AgentDeviceRunner', 'AgentDeviceRunner.xcodeproj'),
27+
{ recursive: true },
28+
);
29+
derived = mkdtempForTestSync('agent-device-runner-location-derived-');
30+
customBuildLocation = mkdtempForTestSync('agent-device-runner-location-custom-');
31+
process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH = derived;
32+
runCmdStreaming.mockReset().mockImplementation(xcodebuildHonoringCustomBuildLocation);
33+
appleRunnerTestHost.update({
34+
runCmdSync: vi.fn().mockImplementation(appleToolchainProbeResult),
35+
runCmdStreaming,
36+
findProjectRoot: () => projectRoot,
37+
readVersion: () => '0.0.0-test',
38+
});
39+
});
40+
41+
afterEach(() => {
42+
delete process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH;
43+
});
44+
45+
test('a custom Xcode build location cannot move the built xctestrun out of the runner cache', async () => {
46+
const built = await ensureXctestrunArtifact(IOS_SIMULATOR, {});
47+
48+
assert.equal(built.artifact, 'rebuilt');
49+
assert.equal(built.xctestrunPath, path.join(derived, 'Build', 'Products', XCTESTRUN_NAME));
50+
assert.deepEqual(fs.readdirSync(customBuildLocation), []);
51+
52+
const reused = await ensureXctestrunArtifact(IOS_SIMULATOR, {});
53+
54+
assert.equal(reused.artifact, 'valid');
55+
assert.equal(reused.xctestrunPath, built.xctestrunPath);
56+
assert.equal(runCmdStreaming.mock.calls.length, 1);
57+
});
58+
59+
/**
60+
* Stands in for `xcodebuild build-for-testing` on a host whose Xcode settings set a custom build
61+
* location: products land there unless the command line pins `SYMROOT`.
62+
*/
63+
async function xcodebuildHonoringCustomBuildLocation(
64+
_command: string,
65+
args: string[],
66+
): Promise<ExecResult> {
67+
const symroot = args.find((arg) => arg.startsWith('SYMROOT='))?.slice('SYMROOT='.length);
68+
const productsRoot = symroot ?? customBuildLocation;
69+
await seedRunnerProductBundle(
70+
path.join(productsRoot, 'Debug-iphonesimulator', 'AgentDeviceRunner.app'),
71+
);
72+
fs.writeFileSync(
73+
path.join(productsRoot, XCTESTRUN_NAME),
74+
`<?xml version="1.0" encoding="UTF-8"?>
75+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
76+
<plist version="1.0">
77+
<dict>
78+
<key>ProjectRootHint</key>
79+
<string>${projectRoot}</string>
80+
<key>ProductPaths</key>
81+
<array>
82+
<string>__TESTROOT__/Debug-iphonesimulator/AgentDeviceRunner.app</string>
83+
</array>
84+
</dict>
85+
</plist>`,
86+
);
87+
return { exitCode: 0, stdout: '', stderr: '' };
88+
}

‎packages/platform-apple/src/runner/runner-artifact.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
requireRunnerPhaseRemainingMs,
3232
resolveExpectedRunnerCacheMetadata,
3333
resolveRunnerArchBuildSettings,
34+
resolveRunnerBuildLocationSettings,
3435
resolveRunnerBundleBuildSettings,
3536
resolveRunnerDerivedPath,
3637
resolveRunnerMaxConcurrentDestinationsFlag,
@@ -470,6 +471,7 @@ async function buildRunnerXctestrun(
470471
...xcodebuildDestinationArgs(device, resolveRunnerBuildDestination(device)),
471472
'-derivedDataPath',
472473
derived,
474+
...resolveRunnerBuildLocationSettings(derived),
473475
...performanceBuildSettings,
474476
...archBuildSettings,
475477
...sandboxBuildArgs,

‎packages/platform-apple/src/runner/runner-cache-metadata.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,20 @@ export function resolveRunnerArchBuildSettings(env: NodeJS.ProcessEnv = process.
640640
return archs ? [`ARCHS=${archs}`] : [];
641641
}
642642

643+
/**
644+
* Pins the build roots to the default layout under `derived`. `-derivedDataPath` alone does not:
645+
* a custom or legacy build location in the user's Xcode settings still redirects products and
646+
* intermediates, so the `.xctestrun` would land outside the cache directory.
647+
*/
648+
export function resolveRunnerBuildLocationSettings(derived: string): string[] {
649+
const intermediates = path.join(derived, 'Build', 'Intermediates.noindex');
650+
return [
651+
`SYMROOT=${path.join(derived, 'Build', 'Products')}`,
652+
`OBJROOT=${intermediates}`,
653+
`SHARED_PRECOMPS_DIR=${path.join(intermediates, 'PrecompiledHeaders')}`,
654+
];
655+
}
656+
643657
export function resolveRunnerSandboxBuildArgs(): string[] {
644658
return [
645659
...RUNNER_SANDBOX_BUILD_ARGS,

‎packages/platform-apple/src/runner/runner-cache.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
requireRunnerPhaseRemainingMs,
2626
resolveExpectedRunnerCacheMetadata,
2727
resolveRunnerArchBuildSettings,
28+
resolveRunnerBuildLocationSettings,
2829
resolveRunnerBundleBuildSettings,
2930
resolveRunnerDerivedPath,
3031
resolveRunnerMaxConcurrentDestinationsFlag,

‎scripts/__tests__/runner-isolation-diagnostics.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,18 @@ describe('scripts/build-xcuitest-apple.sh isolation scan', () => {
185185
expect(result.stderr).not.toMatch(/runner isolation scan/);
186186
});
187187
});
188+
189+
describe('scripts/build-xcuitest-apple.sh build location', () => {
190+
test('pins every build root under the derived path so a custom Xcode build location cannot move products', () => {
191+
const result = runBuildScript(log(...CANARY_DIAGNOSTICS), 65);
192+
const args = fs.readFileSync(result.xcodebuildArgs, 'utf8').split('\n');
193+
const intermediates = path.join(result.derived, 'Build', 'Intermediates.noindex');
194+
expect(args).toEqual(
195+
expect.arrayContaining([
196+
`SYMROOT=${path.join(result.derived, 'Build', 'Products')}`,
197+
`OBJROOT=${intermediates}`,
198+
`SHARED_PRECOMPS_DIR=${path.join(intermediates, 'PrecompiledHeaders')}`,
199+
]),
200+
);
201+
});
202+
});

‎scripts/build-xcuitest-apple.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ build_for_testing() {
144144
-scheme "$SCHEME" \
145145
-destination "$DESTINATION" \
146146
-derivedDataPath "$DERIVED_PATH" \
147+
SYMROOT="$DERIVED_PATH/Build/Products" \
148+
OBJROOT="$DERIVED_PATH/Build/Intermediates.noindex" \
149+
SHARED_PRECOMPS_DIR="$DERIVED_PATH/Build/Intermediates.noindex/PrecompiledHeaders" \
147150
"$@"
148151
}
149152

0 commit comments

Comments
 (0)