Skip to content

Commit d33b804

Browse files
fix(apple): keep each client's toolchain answer for its own failure report
One global record meant the last client-named DEVELOPER_DIR read erased what an earlier client needed to report on, even while that client's fingerprint was still cached. A daemon serves request-scoped selections concurrently, so retain the answer per name instead. It is a TTL memo, so a name whose reads stopped drops out on the same clock as the entry it names. Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent 6add8b0 commit d33b804

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,30 @@ describe('the developer dir a toolchain fingerprint answers for', () => {
348348
expect(realpathSync.mock.calls).toEqual([]);
349349
});
350350

351+
test('each client-named selection keeps its own report across another client read', () => {
352+
// A daemon serves clients with different request-scoped DEVELOPER_DIRs at once. The second
353+
// client's read must not erase what the first one needs to report on, even though both of their
354+
// fingerprints are still cached.
355+
const dirA = realSelectionDir(XCODE_A);
356+
const dirB = realSelectionDir(XCODE_B);
357+
let chosen: string | undefined = dirA;
358+
appleRunnerTestHost.update({ commandDeveloperDir: () => chosen });
359+
runCmdSync.mockImplementation((command: string, args: readonly string[]) =>
360+
command === 'xcodebuild' && chosen === dirA
361+
? { exitCode: 0, stdout: 'Xcode 15.1\nBuild version 1A111\n', stderr: '' }
362+
: appleToolchainProbeResult(command, args),
363+
);
364+
365+
expect(readFingerprint().xcodeBuildVersion).toBe('1A111');
366+
chosen = dirB;
367+
expect(readFingerprint().xcodeBuildVersion).toBe(STUBBED_APPLE_TOOLCHAIN.xcodeBuildVersion);
368+
369+
chosen = dirA;
370+
expect(memoizedRunnerXcodeVersion(IOS_SIMULATOR)).toBe('15.1');
371+
chosen = dirB;
372+
expect(memoizedRunnerXcodeVersion(IOS_SIMULATOR)).toBe(STUBBED_APPLE_TOOLCHAIN.xcodeVersion);
373+
});
374+
351375
test('a report for a client dir this process never read answers with nothing', () => {
352376
appleSelectionLink.target = realSelectionDir(XCODE_A);
353377
readFingerprint();

‎packages/platform-apple/src/runner/runner-toolchain-probe.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ type RecordedSelection = {
140140
bundleIdentity: string | undefined;
141141
};
142142

143+
// One clock with the fingerprints it points at, and expiry scheduled for the same reason: nothing
144+
// renews a client dir whose reads stopped, so its record cannot outlive the entry it names.
145+
let lazyRecordedClients: TtlMemo<string, RecordedSelection> | undefined;
146+
function lazyRecordedClientSelections(): TtlMemo<string, RecordedSelection> | undefined {
147+
lazyRecordedClients ??= createTtlMemo<string, RecordedSelection>({
148+
ttlMs: TOOLCHAIN_FINGERPRINT_TTL_MS,
149+
scheduleExpiry: true,
150+
});
151+
return lazyRecordedClients;
152+
}
153+
143154
/**
144155
* What the last default-selection read answered for, which is what a report names when the client
145156
* chose no `DEVELOPER_DIR`. A read that threw or could not resolve its selection leaves it alone,
@@ -148,11 +159,19 @@ type RecordedSelection = {
148159
let recordedDefaultSelection: RecordedSelection | undefined;
149160

150161
/**
151-
* What the last client-named read answered for, under the exact name that client used. A report for
152-
* a client naming some other dir gets nothing: the key for that dir needs the identity of a bundle
153-
* this path must not go and look up.
162+
* What each client-named selection answered for, under the exact name that client used. Kept per
163+
* name rather than in one slot because a daemon serves clients with different request-scoped
164+
* `DEVELOPER_DIR`s at once: one global record would let the last read erase what an earlier client
165+
* needs to report on, even though its fingerprint is still cached. It is a memo and not a map, so a
166+
* name drops out on the same clock as the entry it points at.
154167
*/
155-
let recordedChosenSelection: ({ readonly named: string } & RecordedSelection) | undefined;
168+
function recordedClientSelection(named: string): RecordedSelection | undefined {
169+
return lazyRecordedClientSelections()?.get(named);
170+
}
171+
172+
function rememberRecordedClientSelection(named: string, answered: RecordedSelection): void {
173+
lazyRecordedClientSelections()?.set(named, answered);
174+
}
156175

157176
/**
158177
* The toolchain half of the cache key. It also names the derived-data directory, so an
@@ -184,7 +203,7 @@ export function requireRunnerToolchainFingerprint(
184203
const answered = { developerDir: resolvedDir, bundleIdentity };
185204
// A report finds its entry by the key this read built, whether the read stored it or hit it, so a
186205
// report never has to resolve or stat anything itself.
187-
if (chosenDir !== undefined) recordedChosenSelection = { named: chosenDir, ...answered };
206+
if (chosenDir !== undefined) rememberRecordedClientSelection(chosenDir, answered);
188207
else recordedDefaultSelection = answered;
189208
return fingerprint;
190209
}
@@ -308,11 +327,7 @@ function probeSelectedDeveloperDir(clock: ToolchainProbeClock): string | undefin
308327
export function memoizedRunnerXcodeVersion(device: DeviceInfo): string | undefined {
309328
const chosenDir = commandDeveloperDir();
310329
const answered =
311-
chosenDir === undefined
312-
? recordedDefaultSelection
313-
: recordedChosenSelection?.named === chosenDir
314-
? recordedChosenSelection
315-
: undefined;
330+
chosenDir === undefined ? recordedDefaultSelection : recordedClientSelection(chosenDir);
316331
if (answered === undefined) return undefined;
317332
return toolchainFingerprintCache().get(
318333
toolchainFingerprintCacheKey(

0 commit comments

Comments
 (0)