Skip to content

Commit 3cd64f5

Browse files
authored
fix(apple): fall back to the default Xcode past a dangling selection link (callstack#4146)
* fix(apple): fall back to the default Xcode past a dangling selection link xcode-select -p and xcrun skip a /var/db/xcode_select_link whose target was deleted and use /Applications/Xcode.app. callstack#3995 replaced the xcrun probe with a file reader that failed with apple_toolchain_unreadable instead, which blocked iOS input on hosts with a stale link from an uninstalled Xcode. Any other unreadable link still fails. Fixes callstack#4145 * fix(apple): name a dangling selection link when no default Xcode stands behind it Report danglingSelectionLink and an xcode-select hint when the fallback default Xcode is missing too. Share one ENOENT/unreadable mapping for both selection-link reads. Keep the fixture hermetic against the host's default Xcode, and raise EACCES through the fixture so the case holds as root. * refactor(apple): drop the dangling-link diagnostic for one hint sentence The dangling-link hint asserted a cause the resolver never checked. Return the developer dir as a string again and add the xcode-select -s step to the hint that every toolchain-read failure carries. * refactor(apple): name the selection link in the toolchain hint by its constant
1 parent 343b7fc commit 3cd64f5

3 files changed

Lines changed: 68 additions & 25 deletions

File tree

‎packages/apple-runner/src/__tests__/apple-toolchain-fixtures.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { beforeEach, vi } from 'vitest';
22
import fs from 'node:fs';
3-
import { XCODE_SELECT_LINK_PATH } from '../runner-toolchain-identity.ts';
3+
import path from 'node:path';
4+
import { DEFAULT_DEVELOPER_DIR, XCODE_SELECT_LINK_PATH } from '../runner-toolchain-identity.ts';
5+
import { mkdtempForTestSync } from './tmp-dir.ts';
46

57
export {
68
STUBBED_APPLE_TOOLCHAIN,
@@ -9,30 +11,48 @@ export {
911
} from '@agent-device/test-kit';
1012

1113
/**
12-
* The host's default selection link as the toolchain identity sees it. Undefined keeps the link
13-
* unreadable; a case that owns a link points it at a path it created.
14+
* The host's selection link and default Xcode as the toolchain identity sees them. Undefined keeps
15+
* a path absent; a case that owns one points it at a path it created. `targetErrorCode` makes the
16+
* link's target fail to resolve with that code.
1417
*/
15-
export const appleSelectionLink: { target: string | undefined } = { target: undefined };
18+
export const appleSelectionLink: {
19+
target: string | undefined;
20+
targetErrorCode: string | undefined;
21+
defaultDeveloperDir: string | undefined;
22+
} = { target: undefined, targetErrorCode: undefined, defaultDeveloperDir: undefined };
1623

1724
/**
18-
* Hides the selection link of the machine running the suite, so a case decides the host's selection
19-
* rather than inheriting it.
25+
* Hides the selection link and default Xcode of the machine running the suite, so a case decides the
26+
* host's selection rather than inheriting it.
2027
*/
2128
export function stubAppleSelectionLink(): void {
2229
const realRealpathSync = fs.realpathSync;
2330
const realReadlinkSync = fs.readlinkSync;
24-
const absent = () =>
25-
Object.assign(new Error('ENOENT: no such file or directory'), { code: 'ENOENT' });
31+
const failure = (code: string) => Object.assign(new Error(code), { code });
32+
const absent = () => failure('ENOENT');
2633
beforeEach(() => {
34+
// The host's own default Xcode stays out of reach: an absent default resolves to a path that
35+
// does not exist, so every read under it fails.
36+
const missingDefaultDeveloperDir = path.join(
37+
mkdtempForTestSync('no-default-xcode-'),
38+
'Xcode.app/Contents/Developer',
39+
);
2740
appleSelectionLink.target = undefined;
41+
appleSelectionLink.targetErrorCode = undefined;
42+
appleSelectionLink.defaultDeveloperDir = undefined;
2843
vi.spyOn(fs, 'readlinkSync').mockImplementation(((target: fs.PathLike) => {
2944
if (String(target) !== XCODE_SELECT_LINK_PATH) return realReadlinkSync(target);
3045
if (appleSelectionLink.target === undefined) throw absent();
3146
return appleSelectionLink.target;
3247
}) as typeof fs.readlinkSync);
3348
vi.spyOn(fs, 'realpathSync').mockImplementation((target: fs.PathLike) => {
49+
if (String(target) === DEFAULT_DEVELOPER_DIR) {
50+
if (appleSelectionLink.defaultDeveloperDir === undefined) return missingDefaultDeveloperDir;
51+
return realRealpathSync(appleSelectionLink.defaultDeveloperDir);
52+
}
3453
if (String(target) !== XCODE_SELECT_LINK_PATH) return realRealpathSync(target);
3554
if (appleSelectionLink.target === undefined) throw absent();
55+
if (appleSelectionLink.targetErrorCode) throw failure(appleSelectionLink.targetErrorCode);
3656
return realRealpathSync(appleSelectionLink.target);
3757
});
3858
});

‎packages/apple-runner/src/__tests__/runner-toolchain-identity.test.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,33 @@ test('without DEVELOPER_DIR the host selection link names the Xcode, and a moved
6565
assert.deepEqual(readFingerprint(), FINGERPRINT_B);
6666
});
6767

68-
test('a selection link whose Xcode is gone fails instead of reading another Xcode', () => {
68+
test('a selection link whose Xcode is gone falls back to the default Xcode, as xcode-select -p does', () => {
69+
// #4145: an uninstalled Xcode leaves the link dangling; Apple's tools then use the default Xcode.
6970
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
7071
appleSelectionLink.target = path.join(mkdtempForTestSync('deleted-xcode-'), 'Xcode.app');
72+
appleSelectionLink.defaultDeveloperDir = xcodeB;
73+
assert.deepEqual(readFingerprint(), FINGERPRINT_B);
74+
assert.equal(readRunnerXcodeVersion(), '27.0');
75+
});
76+
77+
test('without DEVELOPER_DIR or a selection link the default Xcode names the fingerprint', () => {
78+
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
79+
appleSelectionLink.defaultDeveloperDir = xcodeB;
80+
assert.deepEqual(readFingerprint(), FINGERPRINT_B);
81+
});
82+
83+
test('a selection link whose Xcode this process cannot read fails instead of reading the default Xcode', () => {
84+
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
85+
appleSelectionLink.target = xcodeA;
86+
appleSelectionLink.targetErrorCode = 'EACCES';
87+
appleSelectionLink.defaultDeveloperDir = xcodeB;
7188
assert.throws(
7289
readFingerprint,
7390
(error: unknown) =>
7491
error instanceof AppError &&
7592
error.details?.reason === 'apple_toolchain_unreadable' &&
7693
error.details?.file === XCODE_SELECT_LINK_PATH &&
77-
error.details?.detail === 'dangling selection link',
94+
error.details?.detail === 'EACCES',
7895
);
7996
});
8097

‎packages/apple-runner/src/runner-toolchain-identity.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ const SDK_DIRECTORY_NAMES: Record<AppleSdkName, string> = {
4444
export const XCODE_SELECT_LINK_PATH = '/var/db/xcode_select_link';
4545

4646
/** Where `xcrun` looks when neither `DEVELOPER_DIR` nor the selection link names a developer dir. */
47-
const DEFAULT_DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer';
47+
export const DEFAULT_DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer';
4848

49-
const TOOLCHAIN_FILE_HINT =
50-
'The Apple runner cache is keyed on the toolchain version, so a cache decision cannot be made without it. Check that `xcode-select -p` or DEVELOPER_DIR names a full Xcode install.';
49+
const TOOLCHAIN_FILE_HINT = `The Apple runner cache is keyed on the toolchain version, so a cache decision cannot be made without it. Check that \`xcode-select -p\` or DEVELOPER_DIR names a full Xcode install. If ${XCODE_SELECT_LINK_PATH} names a deleted Xcode, run \`sudo xcode-select -s <Xcode.app>\`.`;
5150

5251
type BundleVersion = Readonly<{ version: string; buildVersion: string }>;
5352

@@ -113,10 +112,10 @@ export function readRunnerXcodeVersion(): string | undefined {
113112

114113
/**
115114
* The developer dir `xcrun` uses, resolved to its real path: the client's `DEVELOPER_DIR`, else the
116-
* host's selection link, else the default Xcode when no link exists. A tool like xcodes swaps the
117-
* Xcode.app bundle behind a stable path, so the real path is the one that names the toolchain. Like
118-
* `xcrun`, it accepts an `Xcode.app` bundle for its `Contents/Developer`, and a link whose target is
119-
* gone is a broken selection rather than a reason to read another Xcode.
115+
* host's selection link, else the default Xcode. This order must match `xcode-select -p` on the
116+
* host, because the cache key must name the Xcode that builds the runner. A tool like xcodes swaps
117+
* the Xcode.app bundle behind a stable path, so the real path is the one that names the toolchain.
118+
* Like `xcrun`, it accepts an `Xcode.app` bundle for its `Contents/Developer`.
120119
*/
121120
function resolveSelectedDeveloperDir(chosenDir: string | undefined): string {
122121
const candidate = chosenDir?.trim() || selectionLinkTarget() || DEFAULT_DEVELOPER_DIR;
@@ -126,20 +125,27 @@ function resolveSelectedDeveloperDir(chosenDir: string | undefined): string {
126125
}
127126

128127
function selectionLinkTarget(): string | undefined {
128+
// No selection link: `xcrun` falls back to the default Xcode as well.
129+
if (readSelectionLinkStep(() => fs.readlinkSync(XCODE_SELECT_LINK_PATH)) === undefined) {
130+
return undefined;
131+
}
132+
// A link whose target was deleted (an uninstalled Xcode) is not a selection: `xcode-select -p`
133+
// and `xcrun` skip it and use the default Xcode (#4145).
134+
return readSelectionLinkStep(() => fs.realpathSync(XCODE_SELECT_LINK_PATH));
135+
}
136+
137+
/**
138+
* One read of the selection link. A missing path is undefined; any other failure is a selection
139+
* this process cannot read, which must not stand in for the default Xcode.
140+
*/
141+
function readSelectionLinkStep(read: () => string): string | undefined {
129142
try {
130-
fs.readlinkSync(XCODE_SELECT_LINK_PATH);
143+
return read();
131144
} catch (error) {
132-
// No selection link: `xcrun` falls back to the default Xcode as well. Any other failure is a
133-
// selection this process cannot read, which must not stand in for the default Xcode.
134145
const code = (error as NodeJS.ErrnoException).code;
135146
if (code === 'ENOENT') return undefined;
136147
throw new ToolchainFileError(XCODE_SELECT_LINK_PATH, code ?? String(error));
137148
}
138-
const target = realPath(XCODE_SELECT_LINK_PATH);
139-
if (target === undefined) {
140-
throw new ToolchainFileError(XCODE_SELECT_LINK_PATH, 'dangling selection link');
141-
}
142-
return target;
143149
}
144150

145151
/** What `xcodebuild -version` prints for the Xcode that owns `developerDir`. */

0 commit comments

Comments
 (0)