Skip to content

Commit 300c0f8

Browse files
edusperoniNathanWalker
authored andcommitted
fix(vite): deliver dev-server.json again after a recovery reinstall
When an HMR fast sync fails, the run controller reinstalls the app if it needs to and syncs again. A reinstall drops `dev-server.json` (the simulator gets a new `.app`, and an APK's first launch replaces the Android app directory), so the app could no longer find the dev server. `installOnDeviceIfNeeded` now resolves whether it installed, and on a reinstall a device whose `@nativescript/vite` reads `dev-server.json` gets the platform's after-install sync, which writes the file again.
1 parent d86f455 commit 300c0f8

4 files changed

Lines changed: 99 additions & 6 deletions

File tree

‎lib/controllers/run-controller.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,17 @@ export class RunController extends EventEmitter implements IRunController {
13371337
}
13381338
}
13391339

1340-
await this.$deviceInstallAppService.installOnDeviceIfNeeded(
1341-
device,
1342-
deviceDescriptor.buildData,
1343-
);
1340+
const reinstalled =
1341+
await this.$deviceInstallAppService.installOnDeviceIfNeeded(
1342+
device,
1343+
deviceDescriptor.buildData,
1344+
);
1345+
if (
1346+
reinstalled &&
1347+
this.usesDevServerFile(device, projectData, liveSyncInfo)
1348+
) {
1349+
await platformLiveSyncService.syncAfterInstall(device, watchInfo);
1350+
}
13441351
watchInfo.connectTimeout = null;
13451352
await watchAction();
13461353
}

‎lib/definitions/run.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ declare global {
4141
buildData: IBuildData,
4242
packageFile?: string,
4343
): Promise<void>;
44+
/** Resolves whether it installed the app. */
4445
installOnDeviceIfNeeded(
4546
device: Mobile.IDevice,
4647
buildData: IBuildData,
4748
packageFile?: string,
48-
): Promise<void>;
49+
): Promise<boolean>;
4950
shouldInstall(
5051
device: Mobile.IDevice,
5152
buildData: IBuildData,

‎lib/services/device/device-install-app-service.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ export class DeviceInstallAppService implements IDeviceInstallAppService {
9797
device: Mobile.IDevice,
9898
buildData: IBuildData,
9999
packageFile?: string,
100-
): Promise<void> {
100+
): Promise<boolean> {
101101
const shouldInstall = await this.shouldInstall(device, buildData);
102102
if (shouldInstall) {
103103
await this.installOnDevice(device, buildData, packageFile);
104104
}
105+
return shouldInstall;
105106
}
106107

107108
public async shouldInstall(

‎test/controllers/run-controller.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,90 @@ describe("RunController", () => {
610610
);
611611
});
612612

613+
describe("when a failed fast sync reinstalls the app", () => {
614+
let watchActions: number;
615+
let afterInstall: string[];
616+
let reinstalls: boolean;
617+
618+
const syncChange = async (device: any) => {
619+
const projectData = injector
620+
.resolve("projectDataService")
621+
.getProjectData(projectDir);
622+
await (<any>runController).syncChangedDataOnDevices(
623+
{
624+
files: ["/project/platforms/app/bundle.mjs"],
625+
staleFiles: [],
626+
hasOnlyHotUpdateFiles: false,
627+
hasNativeChanges: false,
628+
hmrData: null,
629+
platform: device.deviceInfo.platform,
630+
},
631+
projectData,
632+
hmrLiveSyncInfo,
633+
);
634+
};
635+
636+
beforeEach(() => {
637+
watchActions = 0;
638+
afterInstall = [];
639+
reinstalls = true;
640+
injector.resolve("deviceInstallAppService").installOnDeviceIfNeeded =
641+
async () => reinstalls;
642+
for (const name of ["iOSLiveSyncService", "androidLiveSyncService"]) {
643+
const liveSyncService = injector.resolve(name);
644+
liveSyncService.liveSyncWatchAction = async (
645+
device: Mobile.IDevice,
646+
) => {
647+
if (++watchActions === 1) {
648+
throw new Error("app not running");
649+
}
650+
return {
651+
modifiedFilesData: <any[]>[],
652+
isFullSync: false,
653+
useHotModuleReload: true,
654+
deviceAppData: {
655+
appIdentifier,
656+
device,
657+
platform: device.deviceInfo.platform,
658+
},
659+
};
660+
};
661+
liveSyncService.syncAfterInstall = async (device: Mobile.IDevice) => {
662+
afterInstall.push(device.deviceInfo.identifier);
663+
};
664+
}
665+
});
666+
667+
for (const target of [androidEmulator, iOSSimulator, iOSDevice]) {
668+
it(`delivers dev-server.json again to ${target.deviceInfo.identifier}`, async () => {
669+
await run([target]);
670+
671+
await syncChange(target);
672+
673+
assert.deepStrictEqual(afterInstall, [target.deviceInfo.identifier]);
674+
assert.equal(watchActions, 2);
675+
});
676+
}
677+
678+
it("delivers nothing when nothing was reinstalled", async () => {
679+
reinstalls = false;
680+
await run([iOSSimulator]);
681+
682+
await syncChange(iOSSimulator);
683+
684+
assert.deepStrictEqual(afterInstall, []);
685+
});
686+
687+
it("delivers nothing for a @nativescript/vite without dev-server.json", async () => {
688+
devServerFilePlatforms = [];
689+
await run([iOSSimulator]);
690+
691+
await syncChange(iOSSimulator);
692+
693+
assert.deepStrictEqual(afterInstall, []);
694+
});
695+
});
696+
613697
it("skips adb reverse when the user opts out", async () => {
614698
process.env.NS_HMR_NO_ADB_REVERSE = "1";
615699

0 commit comments

Comments
 (0)