Skip to content

fix: avoid getting crashed LiveSync when the fast sync fails #4917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
abstract class TestCommandBase {
public allowedParameters: ICommandParameter[] = [];
public dashedOptions = {
hmr: { type: OptionType.Boolean, default: false, hasSensitiveValue: false },
};

protected abstract platform: string;
protected abstract $projectData: IProjectData;
protected abstract $testExecutionService: ITestExecutionService;
Expand Down Expand Up @@ -50,6 +54,13 @@ abstract class TestCommandBase {

async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
if (!this.$options.force) {
if (this.$options.hmr) {
// With HMR we are not restarting after LiveSync which is causing a 30 seconds app start on Android
// because the Runtime does not watch for the `/data/local/tmp<appId>-livesync-in-progress` file deletion.
// The App is closing itself after each test execution and the bug will be reproducible on each LiveSync.
this.$errors.fail("The `--hmr` option is not supported for this command.");
}

await this.$migrateController.validate({ projectDir: this.$projectData.projectDir, platforms: [this.platform] });
}

Expand Down
51 changes: 36 additions & 15 deletions lib/controllers/run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HmrConstants, DeviceDiscoveryEventNames } from "../common/constants";
import { PREPARE_READY_EVENT_NAME, TrackActionNames, DEBUGGER_DETACHED_EVENT_NAME, RunOnDeviceEvents, USER_INTERACTION_NEEDED_EVENT_NAME } from "../constants";
import { cache, performanceLog } from "../common/decorators";
import { EventEmitter } from "events";
import * as util from "util";

export class RunController extends EventEmitter implements IRunController {
private prepareReadyEventHandler: any = null;
Expand Down Expand Up @@ -270,7 +271,7 @@ export class RunController extends EventEmitter implements IRunController {
}

private async syncInitialDataOnDevices(projectData: IProjectData, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = {};

const deviceAction = async (device: Mobile.IDevice) => {
const deviceDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
Expand Down Expand Up @@ -333,15 +334,16 @@ export class RunController extends EventEmitter implements IRunController {
error: err,
});

await this.stop({ projectDir: projectData.projectDir, deviceIdentifiers: [device.deviceInfo.identifier], stopOptions: { shouldAwaitAllActions: false }});
await this.stop({ projectDir: projectData.projectDir, deviceIdentifiers: [device.deviceInfo.identifier], stopOptions: { shouldAwaitAllActions: false } });
}
};

await this.addActionToChain(projectData.projectDir, () => this.$devicesService.execute(deviceAction, (device: Mobile.IDevice) => _.some(deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier)));
}

private async syncChangedDataOnDevices(data: IFilesChangeEventData, projectData: IProjectData, liveSyncInfo: ILiveSyncInfo): Promise<void> {
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
const successfullySyncedMessageFormat = `Successfully synced application %s on device %s.`;
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = {};

const deviceAction = async (device: Mobile.IDevice) => {
const deviceDescriptors = this.$liveSyncProcessDataService.getDeviceDescriptors(projectData.projectDir);
Expand Down Expand Up @@ -380,28 +382,47 @@ export class RunController extends EventEmitter implements IRunController {
await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
await platformLiveSyncService.syncAfterInstall(device, watchInfo);
await this.refreshApplication(projectData, { deviceAppData, modifiedFilesData: [], isFullSync: false, useHotModuleReload: liveSyncInfo.useHotModuleReload }, data, deviceDescriptor);
this.$logger.info(util.format(successfullySyncedMessageFormat, deviceAppData.appIdentifier, device.deviceInfo.identifier));
} else {
const isInHMRMode = liveSyncInfo.useHotModuleReload && data.hmrData && data.hmrData.hash;
if (isInHMRMode) {
this.$hmrStatusService.watchHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
}

let liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);

if (!liveSyncResultInfo.didRecover && isInHMRMode) {
const status = await this.$hmrStatusService.getHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
if (status === HmrConstants.HMR_ERROR_STATUS) {
watchInfo.filesToSync = data.hmrData.fallbackFiles;
liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
// We want to force a restart of the application.
liveSyncResultInfo.isFullSync = true;
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
const watchAction = async (): Promise<void> => {
let liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);

if (!liveSyncResultInfo.didRecover && isInHMRMode) {
const status = await this.$hmrStatusService.getHmrStatus(device.deviceInfo.identifier, data.hmrData.hash);
if (status === HmrConstants.HMR_ERROR_STATUS) {
watchInfo.filesToSync = data.hmrData.fallbackFiles;
liveSyncResultInfo = await platformLiveSyncService.liveSyncWatchAction(device, watchInfo);
// We want to force a restart of the application.
liveSyncResultInfo.isFullSync = true;
await this.refreshApplication(projectData, liveSyncResultInfo, data, deviceDescriptor);
}
}

this.$logger.info(util.format(successfullySyncedMessageFormat, deviceAppData.appIdentifier, device.deviceInfo.identifier));
};

if (liveSyncInfo.useHotModuleReload) {
try {
this.$logger.trace("Try executing watch action without any preparation of files.");
await watchAction();
this.$logger.trace("Successfully executed watch action without any preparation of files.");
return;
} catch (err) {
this.$logger.trace(`Error while trying to execute fast sync. Now we'll check the state of the app and we'll try to resurrect from the error. The error is: ${err}`);
}
}

await this.$deviceInstallAppService.installOnDeviceIfNeeded(device, deviceDescriptor.buildData);
watchInfo.connectTimeout = null;
await watchAction();
}

this.$logger.info(`Successfully synced application ${deviceAppData.appIdentifier} on device ${device.deviceInfo.identifier}.`);
} catch (err) {
this.$logger.warn(`Unable to apply changes for device: ${device.deviceInfo.identifier}. Error is: ${err && err.message}.`);

Expand Down
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nativescript",
"preferGlobal": true,
"version": "6.0.2",
"version": "6.0.3",
"author": "Telerik <[email protected]>",
"description": "Command-line interface for building NativeScript projects",
"bin": {
Expand Down Expand Up @@ -139,4 +139,4 @@
"engines": {
"node": ">=10.0.0 <13.0.0"
}
}
}