microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ff70c05a6c821e99785e3f91dca1daf27094e4fd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

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