@@ -44,13 +44,21 @@ const SDK_DIRECTORY_NAMES: Record<AppleSdkName, string> = {
4444export 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
4949const 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+
5255type 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. */
5563class 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. */
105116export 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