microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0b26572933760cf0f3452564671b267c7ba394cf

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

226lines · modecode

1// 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";
7import { logger } from "vscode-debugadapter";
8import { DebugProtocol } from "vscode-debugprotocol";
9import { ProjectVersionHelper } from "../common/projectVersionHelper";
10import { TelemetryHelper } from "../common/telemetryHelper";
11import { MultipleLifetimesAppWorker } from "./appWorker";
12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
13import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase";
14import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
15import * as nls from "vscode-nls";
16const localize = nls.loadMessageBundle();
17
18export class RNDebugSession extends DebugSessionBase {
19
20 private readonly terminateCommand: string;
21
22 private appWorker: MultipleLifetimesAppWorker | null;
23 private nodeSession: vscode.DebugSession | null;
24 private onDidStartDebugSessionHandler: vscode.Disposable;
25 private onDidTerminateDebugSessionHandler: vscode.Disposable;
26
27 constructor(session: vscode.DebugSession) {
28 super(session);
29
30 // constants definition
31 this.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
32
33 // variables definition
34 this.appWorker = null;
35
36 this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
37 this.handleStartDebugSession.bind(this)
38 );
39
40 this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
41 this.handleTerminateDebugSession.bind(this)
42 );
43 }
44
45 protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
46 return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
47 .then(() => {
48 logger.log("Launching the application");
49 logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
50
51 this.appLauncher.launch(launchArgs)
52 .then(() => {
53 return this.appLauncher.getPackagerPort(launchArgs.cwd);
54 })
55 .then((packagerPort: number) => {
56 launchArgs.port = launchArgs.port || packagerPort;
57 this.attachRequest(response, launchArgs).then(() => {
58 resolve();
59 }).catch((e) => reject(e));
60 })
61 .catch((err) => {
62 logger.error("An error occurred while launching the application. " + err.message || err);
63 reject(err);
64 });
65 }))
66 .catch(err => this.showError(err, response));
67 }
68
69 protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
70 let extProps = {
71 platform: {
72 value: attachArgs.platform,
73 isPii: false,
74 },
75 };
76
77 this.previousAttachArgs = attachArgs;
78 return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
79 .then(() => {
80 logger.log("Attaching to the application");
81 logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
82 return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
83 .then(versions => {
84 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
85 if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
86 extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
87 }
88 return TelemetryHelper.generate("attach", extProps, (generator) => {
89 attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
90 return this.appLauncher.getRnCdpProxy().stopServer()
91 .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel))
92 .then(() => {
93 logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
94
95 const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
96 // Create folder if not exist to avoid problems if
97 // RN project root is not a ${workspaceFolder}
98 mkdirp.sync(sourcesStoragePath);
99
100 // If launch is invoked first time, appWorker is undefined, so create it here
101 this.appWorker = new MultipleLifetimesAppWorker(
102 attachArgs,
103 sourcesStoragePath,
104 this.projectRootPath,
105 undefined
106 );
107 this.appLauncher.setAppWorker(this.appWorker);
108
109 this.appWorker.on("connected", (port: number) => {
110 logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
111
112 this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
113
114 if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
115 return;
116 }
117
118 if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
119 this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
120 this.establishDebugSession(attachArgs, resolve);
121 } else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
122 if (this.nodeSession) {
123 this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
124 this.nodeSession.customRequest(this.terminateCommand);
125 }
126 }
127 });
128 return this.appWorker.start();
129 });
130 })
131 .catch((err) => {
132 logger.error("An error occurred while attaching to the debugger. " + err.message || err);
133 reject(err);
134 });
135 });
136 }))
137 .catch(err => this.showError(err, response));
138 }
139
140 protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
141 // The client is about to disconnect so first we need to stop app worker
142 if (this.appWorker) {
143 this.appWorker.stop();
144 }
145
146 this.onDidStartDebugSessionHandler.dispose();
147 this.onDidTerminateDebugSessionHandler.dispose();
148
149 super.disconnectRequest(response, args, request);
150 }
151
152 protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
153 const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
154 attachArgs,
155 this.appLauncher.getCdpProxyPort(),
156 this.session.id
157 );
158
159 vscode.debug.startDebugging(
160 this.appLauncher.getWorkspaceFolder(),
161 attachConfiguration,
162 {
163 parentSession: this.session,
164 consoleMode: vscode.DebugConsoleMode.MergeWithParent,
165 }
166 )
167 .then((childDebugSessionStarted: boolean) => {
168 if (childDebugSessionStarted) {
169 this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
170 this.setConnectionAllowedIfPossible();
171 if (resolve) {
172 this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
173 resolve();
174 }
175 } else {
176 this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
177 this.setConnectionAllowedIfPossible();
178 this.resetFirstConnectionStatus();
179 throw new Error("Cannot start child debug session");
180 }
181 },
182 err => {
183 this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
184 this.setConnectionAllowedIfPossible();
185 this.resetFirstConnectionStatus();
186 throw err;
187 });
188 }
189
190 private handleStartDebugSession(debugSession: vscode.DebugSession) {
191 if (
192 debugSession.configuration.rnDebugSessionId === this.session.id
193 && debugSession.type === this.pwaNodeSessionName
194 ) {
195 this.nodeSession = debugSession;
196 }
197 }
198
199 private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
200 if (
201 debugSession.configuration.rnDebugSessionId === this.session.id
202 && debugSession.type === this.pwaNodeSessionName
203 ) {
204 if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
205 this.establishDebugSession(this.previousAttachArgs);
206 } else {
207 this.session.customRequest(this.disconnectCommand, {forcedStop: true});
208 }
209 }
210 }
211
212 private setConnectionAllowedIfPossible(): void {
213 if (
214 this.debugSessionStatus === DebugSessionStatus.ConnectionDone
215 || this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
216 ) {
217 this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
218 }
219 }
220
221 private resetFirstConnectionStatus(): void {
222 if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
223 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
224 }
225 }
226}
227