Skip to content

Commit 18078a3

Browse files
committed
fix(ios-runner): undo only an XCTestDevices redirect agent-device left behind
The legacy cleanup unlinked any XCTestDevices symlink on every simulator startup, so a host that links XCTestDevices to another volume lost the link on a default-set open. A symlink is now removed only when the agent-device backup sits beside it, or when it points at this runner's own scoped simulator set.
1 parent 27441b7 commit 18078a3

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-device-set.test.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test('an older redirect is undone: the symlink goes and the host set comes back'
7676
fs.mkdirSync(path.join(paths.backupPath, 'HOST-UDID'), { recursive: true });
7777
fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir');
7878

79-
restoreLegacyXctestDeviceSetRedirect(paths.xctestDeviceSetPath);
79+
restoreLegacyXctestDeviceSetRedirect(IOS_SIMULATOR, paths.xctestDeviceSetPath);
8080

8181
assert.equal(fs.lstatSync(paths.xctestDeviceSetPath).isSymbolicLink(), false);
8282
assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'HOST-UDID')));
@@ -85,22 +85,48 @@ test('an older redirect is undone: the symlink goes and the host set comes back'
8585
assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID')));
8686
});
8787

88-
test('a symlink an older redirect left without a backup is removed', () => {
88+
test('a symlink an older redirect left into this scoped set without a backup is removed', () => {
8989
const paths = makeLegacyPaths();
9090
fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir');
9191

92-
restoreLegacyXctestDeviceSetRedirect(paths.xctestDeviceSetPath);
92+
restoreLegacyXctestDeviceSetRedirect(
93+
{ ...IOS_SIMULATOR, simulatorSetPath: paths.scopedSetPath },
94+
paths.xctestDeviceSetPath,
95+
);
9396

9497
assert.equal(fs.lstatSync(paths.xctestDeviceSetPath, { throwIfNoEntry: false }), undefined);
9598
assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID')));
9699
});
97100

101+
test('a host-owned XCTestDevices symlink with no backup survives a default-set startup', () => {
102+
const paths = makeLegacyPaths();
103+
fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir');
104+
105+
restoreLegacyXctestDeviceSetRedirect(IOS_SIMULATOR, paths.xctestDeviceSetPath);
106+
107+
assert.equal(fs.readlinkSync(paths.xctestDeviceSetPath), paths.scopedSetPath);
108+
});
109+
110+
test('a host-owned XCTestDevices symlink elsewhere survives a scoped-set startup', () => {
111+
const paths = makeLegacyPaths();
112+
const external = path.join(path.dirname(paths.scopedSetPath), 'external-volume');
113+
fs.mkdirSync(external);
114+
fs.symlinkSync(external, paths.xctestDeviceSetPath, 'dir');
115+
116+
restoreLegacyXctestDeviceSetRedirect(
117+
{ ...IOS_SIMULATOR, simulatorSetPath: paths.scopedSetPath },
118+
paths.xctestDeviceSetPath,
119+
);
120+
121+
assert.equal(fs.readlinkSync(paths.xctestDeviceSetPath), external);
122+
});
123+
98124
test('a backup never replaces a host set that is already in place', () => {
99125
const paths = makeLegacyPaths();
100126
fs.mkdirSync(path.join(paths.xctestDeviceSetPath, 'CURRENT-UDID'), { recursive: true });
101127
fs.mkdirSync(path.join(paths.backupPath, 'OLD-UDID'), { recursive: true });
102128

103-
restoreLegacyXctestDeviceSetRedirect(paths.xctestDeviceSetPath);
129+
restoreLegacyXctestDeviceSetRedirect(IOS_SIMULATOR, paths.xctestDeviceSetPath);
104130

105131
assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'CURRENT-UDID')));
106132
assert.ok(fs.existsSync(path.join(paths.backupPath, 'OLD-UDID')));
@@ -109,7 +135,7 @@ test('a backup never replaces a host set that is already in place', () => {
109135
test('a host with no leftovers is left untouched', () => {
110136
const paths = makeLegacyPaths();
111137

112-
restoreLegacyXctestDeviceSetRedirect(paths.xctestDeviceSetPath);
138+
restoreLegacyXctestDeviceSetRedirect(IOS_SIMULATOR, paths.xctestDeviceSetPath);
113139

114140
assert.equal(fs.existsSync(paths.xctestDeviceSetPath), false);
115141
assert.equal(fs.existsSync(paths.backupPath), false);

‎packages/platform-apple/src/runner/__tests__/runner-session-lifecycle.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ test('a scoped simulator-set session hands off like one in the default set', asy
374374

375375
test('a simulator startup puts back a legacy XCTestDevices redirect before the build', async () => {
376376
const order: string[] = [];
377-
mockRestoreLegacyXctestDeviceSetRedirect.mockImplementationOnce(() => order.push('restore'));
377+
mockRestoreLegacyXctestDeviceSetRedirect.mockImplementationOnce((device: DeviceInfo) =>
378+
order.push(`restore:${device.id}`),
379+
);
378380
const ensure = mockEnsureXctestrunArtifact.getMockImplementation();
379381
mockEnsureXctestrunArtifact.mockImplementationOnce(async (...args: unknown[]) => {
380382
order.push('ensure');
@@ -383,7 +385,7 @@ test('a simulator startup puts back a legacy XCTestDevices redirect before the b
383385

384386
await ensureRunnerSession({ ...IOS_SIMULATOR, id: 'runner-lifecycle-legacy-redirect' }, {});
385387

386-
assert.deepEqual(order, ['restore', 'ensure']);
388+
assert.deepEqual(order, ['restore:runner-lifecycle-legacy-redirect', 'ensure']);
387389
});
388390

389391
// #2681: the handoff lanes and every gate that keeps a runner on the kill path.

‎packages/platform-apple/src/runner/runner-device-set.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ export function xcodebuildDestinationArgs(device: DeviceInfo, destination: strin
2626
* Puts the host's own `~/Library/Developer/XCTestDevices` back where an older agent-device left it
2727
* redirected: a symlink into a scoped set, with the real directory renamed to
2828
* `XCTestDevices.agent-device-backup`. Xcode's first-launch cleanup deletes every device in
29-
* `XCTestDevices`, so a symlink left there deletes the scoped set it points at.
29+
* `XCTestDevices`, so a symlink left there deletes the scoped set it points at. Only that state is
30+
* undone: a symlink is removed when the backup beside it exists, or when it points at this runner's
31+
* own scoped set, so a host that links `XCTestDevices` elsewhere keeps its link.
3032
*/
3133
export function restoreLegacyXctestDeviceSetRedirect(
34+
device: DeviceInfo,
3235
xctestDeviceSetPath: string = path.join(os.homedir(), 'Library', 'Developer', 'XCTestDevices'),
3336
): void {
34-
if (fs.lstatSync(xctestDeviceSetPath, { throwIfNoEntry: false })?.isSymbolicLink()) {
37+
const backupPath = `${xctestDeviceSetPath}.agent-device-backup`;
38+
const backupExists = fs.existsSync(backupPath);
39+
const isSymlink =
40+
fs.lstatSync(xctestDeviceSetPath, { throwIfNoEntry: false })?.isSymbolicLink() ?? false;
41+
if (
42+
isSymlink &&
43+
(backupExists || linkPointsAt(xctestDeviceSetPath, resolveRunnerSimulatorSetPath(device)))
44+
) {
3545
fs.unlinkSync(xctestDeviceSetPath);
3646
}
37-
const backupPath = `${xctestDeviceSetPath}.agent-device-backup`;
38-
if (fs.existsSync(backupPath) && !fs.existsSync(xctestDeviceSetPath)) {
47+
if (backupExists && !fs.existsSync(xctestDeviceSetPath)) {
3948
fs.renameSync(backupPath, xctestDeviceSetPath);
4049
}
4150
}
4251

52+
function linkPointsAt(linkPath: string, targetPath: string | undefined): boolean {
53+
if (targetPath === undefined) return false;
54+
return (
55+
path.resolve(path.dirname(linkPath), fs.readlinkSync(linkPath)) === path.resolve(targetPath)
56+
);
57+
}
58+
4359
/** What a runner xcodebuild failure reports about the scoped set it resolved its destination in. */
4460
export function runnerSimulatorSetFailureDetails(
4561
device: DeviceInfo,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ async function startRunnerSessionWithLease(
254254
// an external xctestrun that never launches are different steps, and a caller told "developer disk
255255
// image" should not have to know which one this run happened to take.
256256
try {
257-
if (device.kind === 'simulator') restoreLegacyXctestDeviceSetRedirect();
257+
if (device.kind === 'simulator') restoreLegacyXctestDeviceSetRedirect(device);
258258
xctestrunArtifact = await measureRunnerStartupStep(
259259
startupTimings,
260260
'ensure_xctestrun',

0 commit comments

Comments
 (0)