@@ -23,8 +23,12 @@ import { memoizedRunnerXcodeVersion } from '../runner-toolchain-probe.ts';
2323import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from '../apple-runner-platform.ts' ;
2424import {
2525 appleToolchainProbeResult ,
26+ blockForWholeTimeout ,
27+ installToolchainProbeClock ,
28+ selectedDeveloperDirResult ,
2629 STUBBED_APPLE_TOOLCHAIN ,
2730 stubAppleToolchainProbes ,
31+ valueProbeCommands ,
2832} from './apple-toolchain-fixtures.ts' ;
2933import { mkdtempForTestSync } from './tmp-dir.ts' ;
3034import { xcodebuildLogWithBuildArguments } from './runner-build-log.fixtures.ts' ;
@@ -272,7 +276,7 @@ describe('toolchain probe budget', () => {
272276 beforeEach ( resetAllProcessMemosForTests ) ;
273277
274278 test ( 'a cold-start probe recovers on retry, and the stall it survived is charged to the budget' , ( ) => {
275- const clock = installFakeToolchainClock ( ) ;
279+ const clock = installToolchainProbeClock ( ) ;
276280 const xcodebuildTimeouts : number [ ] = [ ] ;
277281 runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
278282 if ( command !== 'xcodebuild' ) return appleToolchainProbeResult ( command , args ) ;
@@ -292,9 +296,10 @@ describe('toolchain probe budget', () => {
292296 } ) ;
293297
294298 test ( 'an owning phase with 4 s left gets one 4 s attempt and no retry' , ( ) => {
295- const clock = installFakeToolchainClock ( ) ;
299+ const clock = installToolchainProbeClock ( ) ;
296300 const phaseBudget = createRunnerPhaseBudget ( 4_000 , undefined ) ;
297301 runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
302+ if ( command === 'xcode-select' ) return appleToolchainProbeResult ( command , args ) ;
298303 throw blockForWholeTimeout ( clock , command , args , options ) ;
299304 } ) ;
300305 runCmdSync . mockClear ( ) ;
@@ -303,14 +308,14 @@ describe('toolchain probe budget', () => {
303308 ( ) => resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR , undefined , phaseBudget ) ,
304309 ( error : unknown ) => expectRunnerPhaseBudgetExhausted ( error ) ,
305310 ) ;
306- assert . equal ( runCmdSync . mock . calls . length , 1 ) ;
311+ assert . equal ( valueProbeCommands ( runCmdSync . mock . calls ) . length , 1 ) ;
307312 // The one attempt was capped by the phase, not by the 30 s per-call ceiling.
308- assert . equal ( runCmdSync . mock . calls [ 0 ] ?. [ 2 ] ?. timeoutMs , 4_000 ) ;
313+ assert . equal ( runCmdSync . mock . calls . at ( - 1 ) ?. [ 2 ] ?. timeoutMs , 4_000 ) ;
309314 assert . equal ( clock . nowMs , 4_000 ) ;
310315 } ) ;
311316
312317 test ( 'a probe that failed on its own and merely says "timed out" in its message is not retried' , ( ) => {
313- installFakeToolchainClock ( ) ;
318+ installToolchainProbeClock ( ) ;
314319 runCmdSync . mockImplementation ( ( command : string , args : string [ ] ) => {
315320 if ( command !== 'xcodebuild' ) return appleToolchainProbeResult ( command , args ) ;
316321 // No `timeoutMs` detail: this is the tool reporting its own failure, not
@@ -439,7 +444,7 @@ describe('toolchain probe budget', () => {
439444 ] ;
440445
441446 test . each ( CANCELLATION_CASES ) ( 'cancellation matrix: $label' , ( testCase ) => {
442- const clock = installFakeToolchainClock ( ) ;
447+ const clock = installToolchainProbeClock ( ) ;
443448 const request = new AbortController ( ) ;
444449 if ( testCase . fingerprintCache === 'warm' ) {
445450 runCmdSync . mockImplementation ( appleToolchainProbeResult ) ;
@@ -449,6 +454,9 @@ describe('toolchain probe budget', () => {
449454
450455 let execs = 0 ;
451456 runCmdSync . mockImplementation ( ( command : string , args : string [ ] , options : ExecOptions ) => {
457+ // The cache-key resolution picks which entry the read belongs to; it is never one of the
458+ // three probes this matrix interrupts.
459+ if ( command === 'xcode-select' ) return appleToolchainProbeResult ( command , args ) ;
452460 execs += 1 ;
453461 if ( ! failsOnExec ( testCase , execs ) ) return appleToolchainProbeResult ( command , args ) ;
454462 if ( testCase . aborts === 'while-it-blocks' ) request . abort ( ) ;
@@ -513,44 +521,6 @@ function expectRunnerPhaseBudgetExhausted(error: unknown): boolean {
513521 return true ;
514522}
515523
516- /**
517- * A clock the probes' own budget reads, advanced only by
518- * {@link blockForWholeTimeout}. Without it a mock that throws immediately
519- * proves nothing about a deadline: no time passes, so every budget looks
520- * untouched however many attempts run.
521- */
522- function installFakeToolchainClock ( ) : { nowMs : number } {
523- const clock = { nowMs : 0 } ;
524- appleRunnerTestHost . update ( {
525- deadlineFromTimeoutMs : ( timeoutMs : number ) => {
526- const startedAtMs = clock . nowMs ;
527- const expiresAtMs = startedAtMs + Math . max ( 0 , timeoutMs ) ;
528- return {
529- remainingMs : ( ) => Math . max ( 0 , expiresAtMs - clock . nowMs ) ,
530- elapsedMs : ( ) => Math . max ( 0 , clock . nowMs - startedAtMs ) ,
531- isExpired : ( ) => expiresAtMs - clock . nowMs <= 0 ,
532- } ;
533- } ,
534- } ) ;
535- return clock ;
536- }
537-
538- /** A probe that blocked for its whole timeout and was then killed, as the exec layer reports it. */
539- function blockForWholeTimeout (
540- clock : { nowMs : number } ,
541- command : string ,
542- args : string [ ] ,
543- options : ExecOptions ,
544- ) : AppError {
545- const timeoutMs = options . timeoutMs ?? 0 ;
546- clock . nowMs += timeoutMs ;
547- return new AppError ( 'COMMAND_FAILED' , `${ command } timed out after ${ timeoutMs } ms` , {
548- cmd : command ,
549- args,
550- timeoutMs,
551- } ) ;
552- }
553-
554524test ( 'a failing probe reports its exit status rather than a fabricated SDK version' , ( ) => {
555525 runCmdSync . mockImplementation ( ( command : string , args : readonly string [ ] ) =>
556526 command === 'xcrun'
@@ -652,16 +622,14 @@ test('a malformed xcodebuild answer is not memoized: the next request re-probes
652622 assert . deepEqual ( unavailableProbes ( ) , [
653623 { probe : 'xcodebuild -version' , reason : 'unparsable_output' } ,
654624 ] ) ;
655- const probeCallsWhileMalformed = runCmdSync . mock . calls . length ;
656625
657626 runCmdSync . mockImplementation ( appleToolchainProbeResult ) ;
627+ runCmdSync . mockClear ( ) ;
658628 const metadata = resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
659629
660630 assert . equal ( metadata . xcodeVersion , '26.2' ) ;
661631 assert . equal ( metadata . xcodeBuildVersion , '17C52' ) ;
662- expect ( runCmdSync . mock . calls . slice ( probeCallsWhileMalformed ) . map ( ( [ command ] ) => command ) ) . toEqual (
663- [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ,
664- ) ;
632+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ) ;
665633} ) ;
666634
667635test ( 'only a complete, parsed toolchain fingerprint is memoized' , ( ) => {
@@ -681,15 +649,11 @@ test('only a complete, parsed toolchain fingerprint is memoized', () => {
681649 runCmdSync . mockClear ( ) ;
682650 const first = resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
683651 // The healthy xcodebuild answer from the failed round was not kept either: all three re-run.
684- expect ( runCmdSync . mock . calls . map ( ( [ command ] ) => command ) ) . toEqual ( [
685- 'xcodebuild' ,
686- 'xcrun' ,
687- 'xcrun' ,
688- ] ) ;
652+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ) ;
689653
690654 runCmdSync . mockClear ( ) ;
691655 const second = resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
692- expect ( runCmdSync ) . not . toHaveBeenCalled ( ) ;
656+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ ] ) ;
693657 assert . deepEqual (
694658 [ second . xcodeVersion , second . xcodeBuildVersion , second . sdkVersion , second . sdkBuildVersion ] ,
695659 [ first . xcodeVersion , first . xcodeBuildVersion , first . sdkVersion , first . sdkBuildVersion ] ,
@@ -793,26 +757,57 @@ test('a toolchain read under one DEVELOPER_DIR does not answer for another, so i
793757 resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) . xcodeBuildVersion ,
794758 STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ,
795759 ) ;
796- expect ( runCmdSync ) . not . toHaveBeenCalled ( ) ;
760+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ ] ) ;
797761} ) ;
798762
799- test ( 'a toolchain fingerprint expires once no request uses it; each use renews it' , ( ) => {
763+ test ( 'a default selection the host moved does not answer from the fingerprint it left behind' , ( ) => {
764+ // Same consequence as the DEVELOPER_DIR case above, reached through `xcode-select` instead:
765+ // `sudo xcode-select -s` changes what a read without a client dir is answering for (#3300).
766+ withoutRunnerDerivedPathEnv ( ) ;
767+ let selectedDir = '/Applications/Xcode-A.app/Contents/Developer' ;
768+ runCmdSync . mockImplementation ( ( command : string , args : readonly string [ ] ) =>
769+ command === 'xcode-select'
770+ ? selectedDeveloperDirResult ( selectedDir )
771+ : command === 'xcodebuild' && selectedDir === '/Applications/Xcode-B.app/Contents/Developer'
772+ ? { exitCode : 0 , stdout : 'Xcode 27.0\nBuild version 18A100\n' , stderr : '' }
773+ : appleToolchainProbeResult ( command , args ) ,
774+ ) ;
775+
776+ const onA = resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
777+ selectedDir = '/Applications/Xcode-B.app/Contents/Developer' ;
778+ runCmdSync . mockClear ( ) ;
779+ const onB = resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
780+ expect ( runCmdSync . mock . calls . map ( ( [ command ] ) => command ) ) . toEqual ( [
781+ 'xcode-select' ,
782+ 'xcodebuild' ,
783+ 'xcrun' ,
784+ 'xcrun' ,
785+ ] ) ;
786+ assert . equal ( onA . xcodeBuildVersion , STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
787+ assert . equal ( onB . xcodeBuildVersion , '18A100' ) ;
788+ // The derived path is what a retained runner is reused by, so a differing one makes it stale.
789+ assert . notEqual (
790+ resolveRunnerDerivedPath ( IOS_SIMULATOR , onA ) ,
791+ resolveRunnerDerivedPath ( IOS_SIMULATOR , onB ) ,
792+ ) ;
793+ } ) ;
794+
795+ test ( 'a toolchain fingerprint expires at the end of the TTL its read started, hits included' , ( ) => {
800796 vi . useFakeTimers ( ) ;
801797 try {
802798 appleRunnerTestHost . update ( {
803799 commandDeveloperDir : ( ) => '/Applications/Xcode-A.app/Contents/Developer' ,
804800 } ) ;
805801 resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
806802 runCmdSync . mockClear ( ) ;
807- // Used every 9 minutes, it outlives a 10-minute window without another probe.
808- vi . advanceTimersByTime ( 9 * 60_000 ) ;
809- resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
803+ // A hit answers the read and leaves the expiry where the read put it. Hits used to push it
804+ // ahead of themselves, so a busy daemon never re-resolved what it was answering for (#3300).
810805 vi . advanceTimersByTime ( 9 * 60_000 ) ;
811806 resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
812807 expect ( runCmdSync ) . not . toHaveBeenCalled ( ) ;
813808 expect ( memoizedRunnerXcodeVersion ( IOS_SIMULATOR ) ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeVersion ) ;
814809
815- vi . advanceTimersByTime ( 10 * 60_000 ) ;
810+ vi . advanceTimersByTime ( 60_001 ) ;
816811 expect ( memoizedRunnerXcodeVersion ( IOS_SIMULATOR ) ) . toBeUndefined ( ) ;
817812 resolveExpectedRunnerCacheMetadata ( IOS_SIMULATOR ) ;
818813 expect ( runCmdSync . mock . calls . map ( ( [ command ] ) => command ) ) . toEqual ( [
0 commit comments