microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
17bf2e6423d9b2b85ca346a4f0116dfe25b16cff

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

233lines · modeblame

6e1bcd36RedMickey6 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as vscode from "vscode";
5import * as path from "path";
6import * as mkdirp from "mkdirp";
2c19da7fRedMickey6 years ago7import { logger } from "vscode-debugadapter";
6e1bcd36RedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
9import { ProjectVersionHelper } from "../common/projectVersionHelper";
10import { TelemetryHelper } from "../common/telemetryHelper";
11import { MultipleLifetimesAppWorker } from "./appWorker";
a6562589RedMickey6 years ago12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
2c19da7fRedMickey6 years ago13import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase";
6e1bcd36RedMickey6 years ago14import * as nls from "vscode-nls";
15const localize = nls.loadMessageBundle();
16
2c19da7fRedMickey6 years ago17export class RNDebugSession extends DebugSessionBase {
6e1bcd36RedMickey6 years ago18
4c757eebRedMickey6 years ago19private readonly terminateCommand: string;
f872f4d5RedMickey6 years ago20
e23d1841RedMickey6 years ago21private appWorker: MultipleLifetimesAppWorker | null;
4c757eebRedMickey6 years ago22private nodeSession: vscode.DebugSession | null;
6e491635RedMickey6 years ago23private onDidStartDebugSessionHandler: vscode.Disposable;
24private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago25
2c19da7fRedMickey6 years ago26constructor(session: vscode.DebugSession) {
27super(session);
4c757eebRedMickey6 years ago28
29// constants definition
30this.terminateCommand = "terminate"; // the "terminate" command is sent from the client to the debug adapter in order to give the debuggee a chance for terminating itself
31
32// variables definition
e23d1841RedMickey6 years ago33this.appWorker = null;
34
6e491635RedMickey6 years ago35this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
4c757eebRedMickey6 years ago36this.handleStartDebugSession.bind(this)
37);
38
6e491635RedMickey6 years ago39this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
4c757eebRedMickey6 years ago40this.handleTerminateDebugSession.bind(this)
41);
6e1bcd36RedMickey6 years ago42}
43
984ca036RedMickey6 years ago44protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago45return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
46.then(() => {
47logger.log("Launching the application");
48logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
49
50this.appLauncher.launch(launchArgs)
51.then(() => {
52return this.appLauncher.getPackagerPort(launchArgs.cwd);
53})
54.then((packagerPort: number) => {
55launchArgs.port = launchArgs.port || packagerPort;
56this.attachRequest(response, launchArgs).then(() => {
57resolve();
58}).catch((e) => reject(e));
59})
60.catch((err) => {
2c19da7fRedMickey6 years ago61logger.error("An error occurred while launching the application. " + err.message || err);
6e1bcd36RedMickey6 years ago62reject(err);
63});
984ca036RedMickey6 years ago64}))
e23d1841RedMickey6 years ago65.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago66}
67
984ca036RedMickey6 years ago68protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago69let extProps = {
70platform: {
71value: attachArgs.platform,
72isPii: false,
73},
74};
75
76this.previousAttachArgs = attachArgs;
77return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
78.then(() => {
79logger.log("Attaching to the application");
80logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
81return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
82.then(versions => {
83extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
84if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
85extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
86}
87return TelemetryHelper.generate("attach", extProps, (generator) => {
a324603aRedMickey6 years ago88attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
984ca036RedMickey6 years ago89return this.appLauncher.getRnCdpProxy().stopServer()
90.then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel))
f872f4d5RedMickey6 years ago91.then(() => {
92logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
93
94const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
95// Create folder if not exist to avoid problems if
96// RN project root is not a ${workspaceFolder}
97mkdirp.sync(sourcesStoragePath);
98
99// If launch is invoked first time, appWorker is undefined, so create it here
100this.appWorker = new MultipleLifetimesAppWorker(
101attachArgs,
102sourcesStoragePath,
103this.projectRootPath,
a324603aRedMickey6 years ago104undefined
105);
7e74daf7Yuri Skorokhodov6 years ago106this.appLauncher.setAppWorker(this.appWorker);
f872f4d5RedMickey6 years ago107
108this.appWorker.on("connected", (port: number) => {
109logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
110
a6562589RedMickey6 years ago111this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
4c757eebRedMickey6 years ago112
113if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
114return;
115}
d9c9ddcbRedMickey6 years ago116
4c757eebRedMickey6 years ago117if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
118this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
5d47053fRedMickey6 years ago119this.establishDebugSession(attachArgs, resolve);
4c757eebRedMickey6 years ago120} else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
121if (this.nodeSession) {
122this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
123this.nodeSession.customRequest(this.terminateCommand);
124}
f872f4d5RedMickey6 years ago125}
126});
127return this.appWorker.start();
6e1bcd36RedMickey6 years ago128});
129})
130.catch((err) => {
131logger.error("An error occurred while attaching to the debugger. " + err.message || err);
132reject(err);
133});
134});
984ca036RedMickey6 years ago135}))
e23d1841RedMickey6 years ago136.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago137}
138
984ca036RedMickey6 years ago139protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago140// The client is about to disconnect so first we need to stop app worker
141if (this.appWorker) {
142this.appWorker.stop();
143}
144
6e491635RedMickey6 years ago145this.onDidStartDebugSessionHandler.dispose();
146this.onDidTerminateDebugSessionHandler.dispose();
147
6e1bcd36RedMickey6 years ago148super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago149}
150
5d47053fRedMickey6 years ago151protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
a6562589RedMickey6 years ago152const attachArguments = {
153type: "pwa-node",
154request: "attach",
155name: "Attach",
156continueOnAttach: true,
157port: this.appLauncher.getCdpProxyPort(),
158smartStep: false,
5d47053fRedMickey6 years ago159skipFiles: attachArgs.skipFiles || [],
a6562589RedMickey6 years ago160// The unique identifier of the debug session. It is used to distinguish React Native extension's
161// debug sessions from other ones. So we can save and process only the extension's debug sessions
162// in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession".
163rnDebugSessionId: this.session.id,
164};
165
166vscode.debug.startDebugging(
167this.appLauncher.getWorkspaceFolder(),
168attachArguments,
ebbd64f1RedMickey6 years ago169{
170parentSession: this.session,
171consoleMode: vscode.DebugConsoleMode.MergeWithParent,
172}
a6562589RedMickey6 years ago173)
174.then((childDebugSessionStarted: boolean) => {
175if (childDebugSessionStarted) {
176this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
177this.setConnectionAllowedIfPossible();
178if (resolve) {
179this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
180resolve();
4c757eebRedMickey6 years ago181}
a6562589RedMickey6 years ago182} else {
4c757eebRedMickey6 years ago183this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
184this.setConnectionAllowedIfPossible();
185this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago186throw new Error("Cannot start child debug session");
187}
188},
189err => {
190this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
191this.setConnectionAllowedIfPossible();
4c757eebRedMickey6 years ago192this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago193throw err;
194});
4c757eebRedMickey6 years ago195}
6e1bcd36RedMickey6 years ago196
4c757eebRedMickey6 years ago197private handleStartDebugSession(debugSession: vscode.DebugSession) {
198if (
199debugSession.configuration.rnDebugSessionId === this.session.id
200&& debugSession.type === this.pwaNodeSessionName
201) {
202this.nodeSession = debugSession;
203}
204}
205
206private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
207if (
208debugSession.configuration.rnDebugSessionId === this.session.id
209&& debugSession.type === this.pwaNodeSessionName
210) {
ebbd64f1RedMickey6 years ago211if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago212this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago213} else {
214this.session.customRequest(this.disconnectCommand, {forcedStop: true});
215}
4c757eebRedMickey6 years ago216}
217}
218
219private setConnectionAllowedIfPossible(): void {
220if (
221this.debugSessionStatus === DebugSessionStatus.ConnectionDone
222|| this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
223) {
224this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
225}
226}
227
228private resetFirstConnectionStatus(): void {
229if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
230this.debugSessionStatus = DebugSessionStatus.FirstConnection;
231}
232}
6e1bcd36RedMickey6 years ago233}