Skip to content

Commit d7e15e0

Browse files
thymikeeclaude
andcommitted
fix(ios): prepare the reinstall replacement before uninstalling the app
A replacement that cannot be prepared no longer removes the installed app or its data. A failure during install after the uninstall can still leave the app removed. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent ddbfc85 commit d7e15e0

5 files changed

Lines changed: 98 additions & 32 deletions

File tree

‎packages/command-surface/src/management/install.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const installCommandMetadata = defineFieldCommandMetadata(
3838

3939
const reinstallCommandMetadata = defineFieldCommandMetadata(
4040
'reinstall',
41-
'Replace an installed app with a binary from a local path. This preserves the app identity while installing a new build on the selected device. Local Android and iOS install steps time out after 120s; the command allows 480s including device startup and uninstall.',
41+
'Replace an installed app with a binary from a local path. This preserves the app identity while installing a new build on the selected device. Local Android and iOS install steps time out after 120s; the command allows 480s including device startup and uninstall. On iOS, a build that cannot be read leaves the installed app in place; a failure during install after the uninstall can still leave the app removed.',
4242
{
4343
app: requiredField(stringField()),
4444
appPath: requiredField(stringField('Path to app binary.')),

‎packages/platform-apple/src/deployment/runtime.test.ts‎

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ test('simulator push failure surfaces the simctl stderr excerpt', async () => {
440440
test('physical iOS uninstall failure surfaces the devicectl Developer Mode hint', async () => {
441441
const resolveAppBundleId = vi.fn(async () => 'com.example.app');
442442
const executor = {
443-
prepareArtifact: vi.fn(),
443+
prepareArtifact: vi.fn(async () => ({
444+
installablePath: '/tmp/replacement.app',
445+
cleanup: async () => {},
446+
})),
444447
resolveAppBundleId,
445448
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
446449
} as AppleAppDeploymentExecutor;
@@ -525,40 +528,88 @@ test.each([
525528
},
526529
);
527530

528-
test('preserves Apple reinstall partial-failure ordering', async () => {
529-
const order: string[] = [];
530-
const resolveAppBundleId = vi.fn(async () => {
531-
order.push('uninstall');
532-
return 'com.example.replaced';
533-
});
534-
const prepareArtifact = vi.fn(async () => {
535-
order.push('prepare');
536-
throw new Error('replacement artifact is invalid');
531+
function reinstallHarness(options: {
532+
prepare?: (signal: AbortSignal) => Promise<void>;
533+
uninstallExitCode?: number;
534+
signal?: AbortSignal;
535+
}) {
536+
const events: string[] = [];
537+
const signal = options.signal ?? new AbortController().signal;
538+
const executor: AppleAppDeploymentExecutor = {
539+
withInvalidatedAppResolutionCache: async (_device, operation) => await operation(),
540+
resolveAppBundleId: async () => 'com.example.replaced',
541+
prepareArtifact: (async (
542+
_input: unknown,
543+
{ signal: prepareSignal }: { signal: AbortSignal },
544+
) => {
545+
events.push('prepare');
546+
await options.prepare?.(prepareSignal);
547+
return {
548+
installablePath: '/tmp/App.app',
549+
cleanup: async () => {
550+
events.push('cleanup');
551+
},
552+
};
553+
}) as AppleAppDeploymentExecutor['prepareArtifact'],
554+
};
555+
const run = xcrunLikeRun((request) => {
556+
if (request.args[0] === 'uninstall') {
557+
events.push('uninstall');
558+
return { stdout: '', stderr: 'boom', exitCode: options.uninstallExitCode ?? 0 };
559+
}
560+
if (request.args[0] === 'install') events.push('install');
561+
return bootedSimulatorListResult(request);
537562
});
538-
const executor = {
539-
prepareArtifact,
540-
resolveAppBundleId,
541-
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
542-
} as AppleAppDeploymentExecutor;
543-
const host = deploymentHost(executor);
544563
const operations = createAppleAppDeploymentOperations({
545-
host,
564+
host: deploymentHost(executor, run),
546565
device: appleDevice(),
547-
signal: new AbortController().signal,
566+
signal,
548567
});
549-
550-
await expect(
551-
operations.deployApp?.({
568+
const reinstall = () =>
569+
operations.deployApp!({
552570
app: 'com.example.replaced',
553571
appPath: '/tmp/replacement.app',
554572
replaceExisting: true,
555-
}),
556-
).rejects.toThrow('replacement artifact is invalid');
573+
});
574+
return { events, reinstall };
575+
}
576+
577+
test('reinstall keeps the installed app when the replacement cannot be prepared', async () => {
578+
const { events, reinstall } = reinstallHarness({
579+
prepare: async () => {
580+
throw new Error('replacement artifact is invalid');
581+
},
582+
});
583+
584+
await expect(reinstall()).rejects.toThrow('replacement artifact is invalid');
557585

558-
expect(order).toEqual(['uninstall', 'prepare']);
586+
expect(events).toEqual(['prepare']);
559587
});
560588

561-
test('clears a concurrently repopulated fuzzy resolution after a partial-failed Apple reinstall', async () => {
589+
test('reinstall keeps the installed app when cancelled during preparation', async () => {
590+
const controller = new AbortController();
591+
const { events, reinstall } = reinstallHarness({
592+
signal: controller.signal,
593+
prepare: async (signal) => {
594+
controller.abort(new Error('cancelled'));
595+
signal.throwIfAborted();
596+
},
597+
});
598+
599+
await expect(reinstall()).rejects.toThrow('cancelled');
600+
601+
expect(events).toEqual(['prepare']);
602+
});
603+
604+
test('reinstall releases the prepared replacement when uninstall fails', async () => {
605+
const { events, reinstall } = reinstallHarness({ uninstallExitCode: 1 });
606+
607+
await expect(reinstall()).rejects.toMatchObject({ code: 'COMMAND_FAILED' });
608+
609+
expect(events).toEqual(['prepare', 'uninstall', 'cleanup']);
610+
});
611+
612+
test('clears a concurrently repopulated fuzzy resolution after a preparation-failed Apple reinstall', async () => {
562613
const cachedResolutions = new Map([['Maps', 'com.example.stale-before-uninstall']]);
563614
let nextResolution = 'com.example.current';
564615
const resolveFuzzyTarget = vi.fn(async (target: string) => {
@@ -686,7 +737,7 @@ test.each([
686737
);
687738

688739
test.each([false, true])(
689-
'reinstall clears the old app before preparing and installing with a hosted installer: %s',
740+
'reinstall prepares the replacement before clearing the old app and installing with a hosted installer: %s',
690741
async (hosted) => {
691742
const events: string[] = [];
692743
const artifact = {
@@ -741,8 +792,8 @@ test.each([false, true])(
741792
).toEqual({ bundleId: 'com.example.app', launchTarget: 'com.example.app' });
742793
expect(events).toEqual([
743794
'invalidate-before',
744-
'uninstall',
745795
'materialize',
796+
'uninstall',
746797
hosted ? 'hosted-install' : 'native-install',
747798
'cleanup',
748799
'invalidate-after',

‎packages/platform-apple/src/deployment/runtime.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,15 @@ async function deployAppleApp(
102102
signal: AbortSignal,
103103
install?: AppDeploymentInstaller,
104104
): Promise<AppDeploymentResult> {
105-
// Preserve reinstall's established partial-failure boundary: the old route resolved and
106-
// removed the selected app before it materialized the replacement artifact. That ordering is
107-
// observable when artifact preparation fails, so it belongs to the platform deployment facet.
108105
if (input.replaceExisting) {
109106
return await host.appleDeployment.withInvalidatedAppResolutionCache(device, async () => {
110107
const bundleId = await host.appleDeployment.resolveAppBundleId(device, input.app);
111-
await uninstallAppleApp(host, device, bundleId, signal);
112108
const artifact = await host.appleDeployment.prepareArtifact(
113109
{ source: { kind: 'path', path: input.appPath } },
114110
{ appIdentifierHint: input.app, signal },
115111
);
116112
try {
113+
await uninstallAppleApp(host, device, bundleId, signal);
117114
if (install) await install(artifact, signal);
118115
else await installAppleApp(host, device, artifact.installablePath, signal);
119116
return { bundleId, launchTarget: bundleId };

‎test/integration/provider-scenarios/ios-lifecycle.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
23
import path from 'node:path';
34
import { test } from 'vitest';
45
import { assertFlatToolCall, assertRpcOk } from './assertions.ts';
@@ -402,6 +403,22 @@ test('Provider-backed integration iOS physical reinstall uses scripted devicectl
402403
PROVIDER_SCENARIO_IOS_REINSTALL_DEVICE.id,
403404
appPath,
404405
]);
406+
407+
const callsBeforeCorrupt = appleTool.calls.length;
408+
const corruptIpa = path.join(path.dirname(appPath), 'corrupt.ipa');
409+
fs.writeFileSync(corruptIpa, 'not a zip archive');
410+
const corrupt = await daemon.callCommand('reinstall', ['com.example.demo', corruptIpa], {
411+
platform: 'ios',
412+
udid: PROVIDER_SCENARIO_IOS_REINSTALL_DEVICE.id,
413+
});
414+
assert.notEqual(corrupt.statusCode, 200, JSON.stringify(corrupt.json));
415+
assert.ok(corrupt.json?.error, JSON.stringify(corrupt.json));
416+
const laterCalls = appleTool.calls.slice(callsBeforeCorrupt);
417+
assert.equal(
418+
laterCalls.some((call) => call.includes('uninstall') || call.includes('install')),
419+
false,
420+
`Corrupt replacement must not reach uninstall or install: ${JSON.stringify(laterCalls)}`,
421+
);
405422
},
406423
);
407424
});

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ agent-device reinstall com.example.app ./build/MyApp.app --platform ios
685685
```
686686
687687
- `reinstall <app> <path>` uninstalls and installs in one command, for login/logout resets and deterministic test setup. Device coverage, formats, and remote upload match `install`.
688+
- On iOS, `reinstall` reads the new build before it removes the installed app, so a build that is invalid or unreadable leaves the app and its data in place. If the install itself fails after the uninstall, the app stays removed.
688689
689690
## App uninstall
690691

0 commit comments

Comments
 (0)