Skip to content

Commit 9cbbae2

Browse files
thymikeeclaude
andauthored
fix(device-selection): keep host env out of the request device cache key (#4464)
* fix(device-selection): keep host env out of the request device cache key Android SDK discovery adds ANDROID_HOME, ANDROID_SDK_ROOT and PATH to the host env during the first inventory read. The request-scoped resolution cache keyed on the serialized env, so the open handler missed the selection the request lock had just made and read the whole inventory again: simctl, devicectl, xctrace and adb. The request part of the key already holds every value selection derives from env. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(device-selection): pin env-derived allowlists as separate cache entries Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0702d70 commit 9cbbae2

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎packages/device-selection/src/__tests__/dispatch-resolve.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,40 @@ test('resolveTargetDevice reuses request-scoped device resolution cache for iden
278278
assert.equal(mockListAppleDevices.mock.calls.length, 1);
279279
});
280280

281+
test('resolveTargetDevice reuses the request cache after the inventory configures the host env', async () => {
282+
const env: Record<string, string | undefined> = { PATH: '/usr/bin' };
283+
mockListAppleDevices.mockImplementation(async () => {
284+
env.ANDROID_HOME = '/sdk';
285+
env.PATH = `/sdk/platform-tools:${env.PATH}`;
286+
return [bootedSimulator];
287+
});
288+
289+
await withResolveTargetDeviceCacheScope(async () => {
290+
const first = await resolveTargetDevice({ udid: 'sim-2' }, { env });
291+
const inventoryReads = mockListAppleDevices.mock.calls.length;
292+
const second = await resolveTargetDevice({ udid: 'sim-2' }, { env });
293+
294+
assert.equal(first.id, 'sim-2');
295+
assert.equal(second.id, 'sim-2');
296+
assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads);
297+
});
298+
});
299+
300+
test('resolveTargetDevice request cache key separates env-derived device allowlists', async () => {
301+
mockListAppleDevices.mockResolvedValue([bootedSimulator]);
302+
303+
await withResolveTargetDeviceCacheScope(async () => {
304+
await resolveTargetDevice({ udid: 'sim-2' }, { env: {} });
305+
const inventoryReads = mockListAppleDevices.mock.calls.length;
306+
await resolveTargetDevice(
307+
{ udid: 'sim-2' },
308+
{ env: { AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST: 'emulator-5554' } },
309+
);
310+
311+
assert.equal(mockListAppleDevices.mock.calls.length, inventoryReads * 2);
312+
});
313+
});
314+
281315
test('resolveTargetDevice request cache key separates device selectors', async () => {
282316
mockListAppleDevices.mockResolvedValue([simulator, bootedSimulator]);
283317

‎packages/device-selection/src/dispatch-resolve.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ function buildResolveTargetDeviceCacheKey(
309309
): string {
310310
// The app target only informs the first device choice. Once a request has
311311
// chosen a device, every later resolution must reuse that same device even
312-
// when dispatch has no app target to pass back through this seam.
312+
// when dispatch has no app target to pass back through this seam. The request
313+
// holds every value selection derives from env, so env itself stays out.
313314
const {
315+
env: _env,
314316
appleSimulatorAppTarget: _appleSimulatorAppTarget,
315317
appDeviceTarget: _appDeviceTarget,
316318
webSession: _webSession,

0 commit comments

Comments
 (0)