Skip to content

Fix livesync + debug on iOS Simulator #3334

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 1 commit into from
Jan 24, 2018
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
6 changes: 3 additions & 3 deletions PublicAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ Provides methods for debugging applications on devices. The service is also even
* Usage:
```JavaScript
tns.debugService.on("connectionError", errorData => {
console.log(`Unable to start debug operation on device ${errorData.deviceId}. Error is: ${errorData.message}.`);
console.log(`Unable to start debug operation on device ${errorData.deviceIdentifier}. Error is: ${errorData.message}.`);
});
```

Expand Down Expand Up @@ -522,7 +522,7 @@ interface IDebugOptions {
* Usage:
```JavaScript
tns.debugService.on("connectionError", errorData => {
console.log(`Unable to start debug operation on device ${errorData.deviceId}. Error is: ${errorData.message}.`);
console.log(`Unable to start debug operation on device ${errorData.deviceIdentifier}. Error is: ${errorData.message}.`);
});

const debugData = {
Expand Down Expand Up @@ -903,4 +903,4 @@ CLI is designed as command line tool and when it is used as a library, it does n
For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`.

More information how to add a method to public API is available [here](https://github.com/telerik/mobile-cli-lib#how-to-make-a-method-public).
After that add each method that you've exposed to the tests in `tests/nativescript-cli-lib.ts` file. There you'll find an object describing each publicly available module and the methods that you can call.
After that add each method that you've exposed to the tests in `tests/nativescript-cli-lib.ts` file. There you'll find an object describing each publicly available module and the methods that you can call.
2 changes: 1 addition & 1 deletion lib/common
2 changes: 1 addition & 1 deletion lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ interface IAndroidToolsInfoData {

interface ISocketProxyFactory extends NodeJS.EventEmitter {
createTCPSocketProxy(factory: () => Promise<any>): Promise<any>;
createWebSocketProxy(factory: () => Promise<any>): Promise<any>;
createWebSocketProxy(factory: () => Promise<any>, deviceIdentifier: string): Promise<any>;
}

interface IiOSNotification {
Expand Down
3 changes: 2 additions & 1 deletion lib/device-sockets/ios/socket-proxy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class SocketProxyFactory extends EventEmitter implements ISocketProxyFact
return server;
}

public async createWebSocketProxy(factory: () => Promise<net.Socket>): Promise<ws.Server> {
public async createWebSocketProxy(factory: () => Promise<net.Socket>, deviceIdentifier: string): Promise<ws.Server> {
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
const localPort = await this.$net.getAvailablePortInRange(41000);

Expand All @@ -92,6 +92,7 @@ export class SocketProxyFactory extends EventEmitter implements ISocketProxyFact
try {
_socket = await factory();
} catch (err) {
err.deviceIdentifier = deviceIdentifier;
this.$logger.trace(err);
this.emit(CONNECTION_ERROR_EVENT_NAME, err);
this.$errors.failWithoutHelp("Cannot connect to device socket.");
Expand Down
56 changes: 35 additions & 21 deletions lib/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
constructor(protected device: Mobile.IDevice,
protected $devicesService: Mobile.IDevicesService,
private $platformService: IPlatformService,
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $logger: ILogger,
Expand Down Expand Up @@ -125,24 +125,27 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
const lineStream = byline(child_process.stdout);
this._childProcess = child_process;

lineStream.on('data', (line: NodeBuffer) => {
const lineText = line.toString();
if (lineText && _.startsWith(lineText, debugData.applicationIdentifier)) {
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, lineText);
if (!pid) {
this.$logger.trace(`Line ${lineText} does not contain PID of the application ${debugData.applicationIdentifier}.`);
return;
await new Promise((resolve: () => void, reject) => {
lineStream.on('data', (line: NodeBuffer) => {
const lineText = line.toString();
if (lineText && _.startsWith(lineText, debugData.applicationIdentifier)) {
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, lineText);
if (!pid) {
this.$logger.trace(`Line ${lineText} does not contain PID of the application ${debugData.applicationIdentifier}.`);
return;
}

this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
this._lldbProcess.stdout.pipe(process.stdout);
}
this._lldbProcess.stderr.pipe(process.stderr);
this._lldbProcess.stdin.write("process continue\n");
this.connectToApplicationOnEmulator(debugData.deviceIdentifier).then(resolve, reject);
} else {
process.stdout.write(line + "\n");
}

this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
this._lldbProcess.stdout.pipe(process.stdout);
}
this._lldbProcess.stderr.pipe(process.stderr);
this._lldbProcess.stdin.write("process continue\n");
} else {
process.stdout.write(line + "\n");
}
});
});

return this.wireDebuggerClient(debugData, debugOptions);
Expand All @@ -153,11 +156,21 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS

const attachRequestMessage = this.$iOSNotification.getAttachRequest(debugData.applicationIdentifier);

const iOSEmulator = <Mobile.IiOSSimulatorService>this.$iOSEmulatorServices;
await iOSEmulator.postDarwinNotification(attachRequestMessage);
const iOSEmulatorService = <Mobile.IiOSSimulatorService>this.$iOSEmulatorServices;
await iOSEmulatorService.postDarwinNotification(attachRequestMessage);
await this.connectToApplicationOnEmulator(debugData.deviceIdentifier);
return result;
}

private async connectToApplicationOnEmulator(deviceIdentifier: string): Promise<void> {
const socket = await this.$iOSEmulatorServices.connectToPort({ port: inspectorBackendPort });
if (!socket) {
const error = <Mobile.IDeviceError>new Error("Unable to connect to application. Ensure application is running on simulator.");
error.deviceIdentifier = deviceIdentifier;
throw error;
}
}

private async deviceDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const action = async (device: iOSDevice.IOSDevice): Promise<string> => {
Expand Down Expand Up @@ -212,7 +225,8 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
this.$logger.info("'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector.");
}

this._socketProxy = await this.$socketProxyFactory.createWebSocketProxy(this.getSocketFactory(device));
const deviceIdentifier = device ? device.deviceInfo.identifier : debugData.deviceIdentifier;
this._socketProxy = await this.$socketProxyFactory.createWebSocketProxy(this.getSocketFactory(device), deviceIdentifier);
return this.getChromeDebugUrl(debugOptions, this._socketProxy.options.port);
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/services/livesync/ios-device-livesync-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as helpers from "../../common/helpers";
import * as constants from "../../constants";
import * as minimatch from "minimatch";
import * as net from "net";
Expand Down Expand Up @@ -30,10 +29,8 @@ export class IOSDeviceLiveSyncService extends DeviceLiveSyncServiceBase implemen

if (this.device.isEmulator) {
await this.$iOSEmulatorServices.postDarwinNotification(this.$iOSNotification.getAttachRequest(projectId));
try {
this.socket = await helpers.connectEventuallyUntilTimeout(() => net.connect(IOSDeviceLiveSyncService.BACKEND_PORT), 5000);
} catch (e) {
this.$logger.debug(e);
this.socket = await this.$iOSEmulatorServices.connectToPort({ port: IOSDeviceLiveSyncService.BACKEND_PORT });
if (!this.socket) {
return false;
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/services/debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe("debugService", () => {
const debugData = getDebugData();
await assert.isFulfilled(debugService.debug(debugData, null));

const expectedErrorData = { deviceId: "deviceId", message: "my message", code: 2048 };
const expectedErrorData = { deviceIdentifier: "deviceId", message: "my message", code: 2048 };
const platformDebugService = testInjector.resolve<IPlatformDebugService>(`${platform}DebugService`);
platformDebugService.emit(CONNECTION_ERROR_EVENT_NAME, expectedErrorData);
assert.deepEqual(dataRaisedForConnectionError, expectedErrorData);
Expand Down
2 changes: 1 addition & 1 deletion test/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const expectedDevToolsCommitSha = "02e6bde1bbe34e43b309d4ef774b1168d25fd024";
class IOSDebugServiceInheritor extends IOSDebugService {
constructor(protected $devicesService: Mobile.IDevicesService,
$platformService: IPlatformService,
$iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
$iOSEmulatorServices: Mobile.IiOSSimulatorService,
$childProcess: IChildProcess,
$hostInfo: IHostInfo,
$logger: ILogger,
Expand Down