Skip to content

Commit b08ccb2

Browse files
committed
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.
1 parent 031c78f commit b08ccb2

3 files changed

Lines changed: 95 additions & 44 deletions

File tree

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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';
4-
5-
const DEFAULT_DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer';
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';
66

77
export {
88
STUBBED_APPLE_TOOLCHAIN,
@@ -12,12 +12,14 @@ export {
1212

1313
/**
1414
* 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.
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.
1617
*/
1718
export const appleSelectionLink: {
1819
target: string | undefined;
20+
targetErrorCode: string | undefined;
1921
defaultDeveloperDir: string | undefined;
20-
} = { target: undefined, defaultDeveloperDir: undefined };
22+
} = { target: undefined, targetErrorCode: undefined, defaultDeveloperDir: undefined };
2123

2224
/**
2325
* Hides the selection link and default Xcode of the machine running the suite, so a case decides the
@@ -26,10 +28,17 @@ export const appleSelectionLink: {
2628
export function stubAppleSelectionLink(): void {
2729
const realRealpathSync = fs.realpathSync;
2830
const realReadlinkSync = fs.readlinkSync;
29-
const absent = () =>
30-
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');
3133
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+
);
3240
appleSelectionLink.target = undefined;
41+
appleSelectionLink.targetErrorCode = undefined;
3342
appleSelectionLink.defaultDeveloperDir = undefined;
3443
vi.spyOn(fs, 'readlinkSync').mockImplementation(((target: fs.PathLike) => {
3544
if (String(target) !== XCODE_SELECT_LINK_PATH) return realReadlinkSync(target);
@@ -38,11 +47,12 @@ export function stubAppleSelectionLink(): void {
3847
}) as typeof fs.readlinkSync);
3948
vi.spyOn(fs, 'realpathSync').mockImplementation((target: fs.PathLike) => {
4049
if (String(target) === DEFAULT_DEVELOPER_DIR) {
41-
if (appleSelectionLink.defaultDeveloperDir === undefined) throw absent();
50+
if (appleSelectionLink.defaultDeveloperDir === undefined) return missingDefaultDeveloperDir;
4251
return realRealpathSync(appleSelectionLink.defaultDeveloperDir);
4352
}
4453
if (String(target) !== XCODE_SELECT_LINK_PATH) return realRealpathSync(target);
4554
if (appleSelectionLink.target === undefined) throw absent();
55+
if (appleSelectionLink.targetErrorCode) throw failure(appleSelectionLink.targetErrorCode);
4656
return realRealpathSync(appleSelectionLink.target);
4757
});
4858
});

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,44 @@ test('without DEVELOPER_DIR or a selection link the default Xcode names the fing
8080
assert.deepEqual(readFingerprint(), FINGERPRINT_B);
8181
});
8282

83+
test('a dangling selection link without a default Xcode fails naming the dangling link', () => {
84+
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
85+
appleSelectionLink.target = path.join(mkdtempForTestSync('deleted-xcode-'), 'Xcode.app');
86+
assert.throws(
87+
readFingerprint,
88+
(error: unknown) =>
89+
error instanceof AppError &&
90+
error.details?.reason === 'apple_toolchain_unreadable' &&
91+
String(error.details?.file).endsWith(path.join('Xcode.app', 'Contents', 'version.plist')) &&
92+
error.details?.danglingSelectionLink === XCODE_SELECT_LINK_PATH,
93+
);
94+
assert.equal(readRunnerXcodeVersion(), undefined);
95+
});
96+
97+
test('no selection link and no default Xcode fails without blaming a selection link', () => {
98+
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
99+
assert.throws(
100+
readFingerprint,
101+
(error: unknown) =>
102+
error instanceof AppError &&
103+
error.details?.reason === 'apple_toolchain_unreadable' &&
104+
error.details?.danglingSelectionLink === undefined,
105+
);
106+
});
107+
83108
test('a selection link whose Xcode this process cannot read fails instead of reading the default Xcode', () => {
84109
appleRunnerTestHost.update({ commandDeveloperDir: () => undefined });
85-
const locked = mkdtempForTestSync('locked-xcode-');
86-
appleSelectionLink.target = path.join(locked, 'Xcode.app');
110+
appleSelectionLink.target = xcodeA;
111+
appleSelectionLink.targetErrorCode = 'EACCES';
87112
appleSelectionLink.defaultDeveloperDir = xcodeB;
88-
fs.chmodSync(locked, 0o000);
89-
try {
90-
assert.throws(
91-
readFingerprint,
92-
(error: unknown) =>
93-
error instanceof AppError &&
94-
error.details?.reason === 'apple_toolchain_unreadable' &&
95-
error.details?.file === XCODE_SELECT_LINK_PATH &&
96-
error.details?.detail === 'EACCES',
97-
);
98-
} finally {
99-
fs.chmodSync(locked, 0o700);
100-
}
113+
assert.throws(
114+
readFingerprint,
115+
(error: unknown) =>
116+
error instanceof AppError &&
117+
error.details?.reason === 'apple_toolchain_unreadable' &&
118+
error.details?.file === XCODE_SELECT_LINK_PATH &&
119+
error.details?.detail === 'EACCES',
120+
);
101121
});
102122

103123
test('a selection link this process cannot read fails instead of reading the default Xcode', () => {

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

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ 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

4949
const TOOLCHAIN_FILE_HINT =
5050
'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.';
5151

52+
const DANGLING_SELECTION_LINK_HINT =
53+
'The `xcode-select` selection names an Xcode that no longer exists, and no Xcode is installed at the default path. Run `sudo xcode-select -s <Xcode.app>` or set DEVELOPER_DIR.';
54+
5255
type BundleVersion = Readonly<{ version: string; buildVersion: string }>;
5356

57+
/** The host's selection link when it exists: the Xcode it resolves to, or dangling. */
58+
type SelectionLink = { target: string; dangling?: never } | { target?: never; dangling: true };
59+
60+
type SelectedDeveloperDir = Readonly<{ developerDir: string; danglingSelectionLink: boolean }>;
61+
5462
/** A toolchain file that is missing or does not carry the value the identity needs. */
5563
class ToolchainFileError extends Error {
5664
readonly file: string;
@@ -74,10 +82,12 @@ export function requireRunnerToolchainFingerprint(
7482
): RunnerToolchainFingerprint {
7583
if (signal?.aborted) throw createRequestCanceledError({ phase: 'apple_toolchain_identity' });
7684
const chosenDir = commandDeveloperDir();
85+
let danglingSelectionLink = false;
7786
try {
78-
const developerDir = resolveSelectedDeveloperDir(chosenDir);
79-
const xcode = readXcodeVersion(developerDir);
80-
const sdk = readSdkVersion(developerDir, sdkName);
87+
const selected = resolveSelectedDeveloperDir(chosenDir);
88+
danglingSelectionLink = selected.danglingSelectionLink;
89+
const xcode = readXcodeVersion(selected.developerDir);
90+
const sdk = readSdkVersion(selected.developerDir, sdkName);
8191
return {
8292
xcodeVersion: xcode.version,
8393
xcodeBuildVersion: xcode.buildVersion,
@@ -95,7 +105,8 @@ export function requireRunnerToolchainFingerprint(
95105
...(chosenDir === undefined ? {} : { developerDir: chosenDir }),
96106
file: error.file,
97107
detail: error.detail,
98-
hint: TOOLCHAIN_FILE_HINT,
108+
...(danglingSelectionLink ? { danglingSelectionLink: XCODE_SELECT_LINK_PATH } : {}),
109+
hint: danglingSelectionLink ? DANGLING_SELECTION_LINK_HINT : TOOLCHAIN_FILE_HINT,
99110
},
100111
);
101112
}
@@ -104,7 +115,8 @@ export function requireRunnerToolchainFingerprint(
104115
/** The selected Xcode's version, for a failure report that names it; undefined when unreadable. */
105116
export function readRunnerXcodeVersion(): string | undefined {
106117
try {
107-
return readXcodeVersion(resolveSelectedDeveloperDir(commandDeveloperDir())).version;
118+
return readXcodeVersion(resolveSelectedDeveloperDir(commandDeveloperDir()).developerDir)
119+
.version;
108120
} catch (error) {
109121
if (error instanceof ToolchainFileError) return undefined;
110122
throw error;
@@ -118,28 +130,37 @@ export function readRunnerXcodeVersion(): string | undefined {
118130
* the Xcode.app bundle behind a stable path, so the real path is the one that names the toolchain.
119131
* Like `xcrun`, it accepts an `Xcode.app` bundle for its `Contents/Developer`.
120132
*/
121-
function resolveSelectedDeveloperDir(chosenDir: string | undefined): string {
122-
const candidate = chosenDir?.trim() || selectionLinkTarget() || DEFAULT_DEVELOPER_DIR;
133+
function resolveSelectedDeveloperDir(chosenDir: string | undefined): SelectedDeveloperDir {
134+
const chosen = chosenDir?.trim();
135+
const link = chosen ? undefined : readSelectionLink();
136+
const candidate = chosen || link?.target || DEFAULT_DEVELOPER_DIR;
123137
const resolved = realPath(candidate) ?? candidate;
124138
const bundleDeveloperDir = path.join(resolved, 'Contents', 'Developer');
125-
return fs.existsSync(bundleDeveloperDir) ? bundleDeveloperDir : resolved;
139+
return {
140+
developerDir: fs.existsSync(bundleDeveloperDir) ? bundleDeveloperDir : resolved,
141+
danglingSelectionLink: link?.dangling === true,
142+
};
126143
}
127144

128-
function selectionLinkTarget(): string | undefined {
129-
try {
130-
fs.readlinkSync(XCODE_SELECT_LINK_PATH);
131-
} 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.
134-
const code = (error as NodeJS.ErrnoException).code;
135-
if (code === 'ENOENT') return undefined;
136-
throw new ToolchainFileError(XCODE_SELECT_LINK_PATH, code ?? String(error));
145+
function readSelectionLink(): SelectionLink | undefined {
146+
// No selection link: `xcrun` falls back to the default Xcode as well.
147+
if (readSelectionLinkStep(() => fs.readlinkSync(XCODE_SELECT_LINK_PATH)) === undefined) {
148+
return undefined;
137149
}
150+
// A link whose target was deleted (an uninstalled Xcode) is not a selection: `xcode-select -p`
151+
// and `xcrun` skip it and use the default Xcode (#4145).
152+
const target = readSelectionLinkStep(() => fs.realpathSync(XCODE_SELECT_LINK_PATH));
153+
return target === undefined ? { dangling: true } : { target };
154+
}
155+
156+
/**
157+
* One read of the selection link. A missing path is undefined; any other failure is a selection
158+
* this process cannot read, which must not stand in for the default Xcode.
159+
*/
160+
function readSelectionLinkStep(read: () => string): string | undefined {
138161
try {
139-
return fs.realpathSync(XCODE_SELECT_LINK_PATH);
162+
return read();
140163
} catch (error) {
141-
// A link whose target was deleted (an uninstalled Xcode) is not a selection: `xcode-select -p`
142-
// and `xcrun` skip it and use the default Xcode (#4145).
143164
const code = (error as NodeJS.ErrnoException).code;
144165
if (code === 'ENOENT') return undefined;
145166
throw new ToolchainFileError(XCODE_SELECT_LINK_PATH, code ?? String(error));

0 commit comments

Comments
 (0)