microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

300lines · 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 path from "path";
09f6024fHeniker4 years ago5import * as vscode from "vscode";
6e1bcd36RedMickey6 years ago6import * as mkdirp from "mkdirp";
2c19da7fRedMickey6 years ago7import { logger } from "vscode-debugadapter";
6e1bcd36RedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
09f6024fHeniker4 years ago9import * as nls from "vscode-nls";
6e1bcd36RedMickey6 years ago10import { ProjectVersionHelper } from "../common/projectVersionHelper";
11import { TelemetryHelper } from "../common/telemetryHelper";
a6562589RedMickey6 years ago12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
09f6024fHeniker4 years ago13import { ErrorHelper } from "../common/error/errorHelper";
14import { InternalErrorCode } from "../common/error/internalErrorCode";
15import { MultipleLifetimesAppWorker } from "./appWorker";
34472878RedMickey5 years ago16import {
17DebugSessionBase,
18DebugSessionStatus,
19IAttachRequestArgs,
20ILaunchRequestArgs,
21} from "./debugSessionBase";
1bdccb66RedMickey6 years ago22import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
d93677adRedMickey4 years ago23import { RNSession } from "./debugSessionWrapper";
09f6024fHeniker4 years ago24
34472878RedMickey5 years ago25nls.config({
26messageFormat: nls.MessageFormat.bundle,
27bundleFormat: nls.BundleFormat.standalone,
28})();
6e1bcd36RedMickey6 years ago29const localize = nls.loadMessageBundle();
30
2c19da7fRedMickey6 years ago31export class RNDebugSession extends DebugSessionBase {
e23d1841RedMickey6 years ago32private appWorker: MultipleLifetimesAppWorker | null;
6e491635RedMickey6 years ago33private onDidStartDebugSessionHandler: vscode.Disposable;
34private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago35
d93677adRedMickey4 years ago36constructor(rnSession: RNSession) {
37super(rnSession);
4c757eebRedMickey6 years ago38
39// variables definition
e23d1841RedMickey6 years ago40this.appWorker = null;
41
6e491635RedMickey6 years ago42this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
34472878RedMickey5 years ago43this.handleStartDebugSession.bind(this),
4c757eebRedMickey6 years ago44);
45
6e491635RedMickey6 years ago46this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago47this.handleTerminateDebugSession.bind(this),
4c757eebRedMickey6 years ago48);
6e1bcd36RedMickey6 years ago49}
50
34472878RedMickey5 years ago51protected async launchRequest(
52response: DebugProtocol.LaunchResponse,
53launchArgs: ILaunchRequestArgs,
54// eslint-disable-next-line @typescript-eslint/no-unused-vars
55request?: DebugProtocol.Request,
56): Promise<void> {
0d77292aJiglioNero4 years ago57try {
58try {
59await this.initializeSettings(launchArgs);
60logger.log("Launching the application");
61logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
34472878RedMickey5 years ago62
0d77292aJiglioNero4 years ago63await this.appLauncher.launch(launchArgs);
64
65if (!launchArgs.enableDebug) {
66this.sendResponse(response);
67// if debugging is not enabled skip attach request
68return;
69}
70} catch (error) {
71throw ErrorHelper.getInternalError(
72InternalErrorCode.ApplicationLaunchFailed,
73error.message || error,
74);
75}
76// if debugging is enabled start attach request
d93677adRedMickey4 years ago77await this.vsCodeDebugSession.customRequest("attach", launchArgs);
78this.sendResponse(response);
0d77292aJiglioNero4 years ago79} catch (error) {
19df32dcRedMickey4 years ago80this.terminateWithErrorResponse(error, response);
0d77292aJiglioNero4 years ago81}
6e1bcd36RedMickey6 years ago82}
83
34472878RedMickey5 years ago84protected async attachRequest(
85response: DebugProtocol.AttachResponse,
86attachArgs: IAttachRequestArgs,
87// eslint-disable-next-line @typescript-eslint/no-unused-vars
88request?: DebugProtocol.Request,
89): Promise<void> {
6e1bcd36RedMickey6 years ago90let extProps = {
91platform: {
92value: attachArgs.platform,
93isPii: false,
94},
95};
96
97this.previousAttachArgs = attachArgs;
0d77292aJiglioNero4 years ago98
99return new Promise<void>(async (resolve, reject) => {
100try {
101await this.initializeSettings(attachArgs);
102logger.log("Attaching to the application");
103logger.verbose(
104`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`,
105);
106
107const versions = await ProjectVersionHelper.getReactNativeVersions(
108this.projectRootPath,
109ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
110);
111extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
112attachArgs,
113versions,
114extProps,
115);
116
117// eslint-disable-next-line @typescript-eslint/no-unused-vars
118await TelemetryHelper.generate("attach", extProps, async generator => {
119attachArgs.port =
120attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
121
122const cdpProxy = this.appLauncher.getRnCdpProxy();
123await cdpProxy.stopServer();
124await cdpProxy.initializeServer(
125new RnCDPMessageHandler(),
126this.cdpProxyLogLevel,
dcabd9c8RedMickey4 years ago127this.cancellationTokenSource.token,
34472878RedMickey5 years ago128);
0d77292aJiglioNero4 years ago129
bfcc8a29Samriel4 years ago130if (attachArgs.request === "attach") {
131await this.preparePackagerBeforeAttach(attachArgs, versions);
132}
0d77292aJiglioNero4 years ago133
134logger.log(
135localize("StartingDebuggerAppWorker", "Starting debugger app worker."),
34472878RedMickey5 years ago136);
0d77292aJiglioNero4 years ago137
138const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
139// Create folder if not exist to avoid problems if
140// RN project root is not a ${workspaceFolder}
141mkdirp.sync(sourcesStoragePath);
142
143// If launch is invoked first time, appWorker is undefined, so create it here
144this.appWorker = new MultipleLifetimesAppWorker(
34472878RedMickey5 years ago145attachArgs,
0d77292aJiglioNero4 years ago146sourcesStoragePath,
147this.projectRootPath,
148this.cancellationTokenSource.token,
149undefined,
34472878RedMickey5 years ago150);
0d77292aJiglioNero4 years ago151this.appLauncher.setAppWorker(this.appWorker);
152
153this.appWorker.on("connected", (port: number) => {
154if (this.cancellationTokenSource.token.isCancellationRequested) {
155return this.appWorker?.stop();
156}
157
158logger.log(
159localize(
160"DebuggerWorkerLoadedRuntimeOnPort",
161"Debugger worker loaded runtime on port {0}",
162port,
163),
164);
165
166cdpProxy.setApplicationTargetPort(port);
167
168if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
169return;
170}
34472878RedMickey5 years ago171
0d77292aJiglioNero4 years ago172if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
173this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
174this.establishDebugSession(attachArgs, resolve);
175} else if (
176this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed
177) {
178if (this.nodeSession) {
179this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
09f6024fHeniker4 years ago180void this.nodeSession.customRequest(this.terminateCommand);
0d77292aJiglioNero4 years ago181}
182}
34472878RedMickey5 years ago183});
0d77292aJiglioNero4 years ago184
185if (this.cancellationTokenSource.token.isCancellationRequested) {
186return this.appWorker.stop();
187}
188return await this.appWorker.start();
189});
190} catch (error) {
191reject(error);
192}
d93677adRedMickey4 years ago193})
194.then(() => {
195this.sendResponse(response);
196})
197.catch(err =>
19df32dcRedMickey4 years ago198this.terminateWithErrorResponse(
d93677adRedMickey4 years ago199ErrorHelper.getInternalError(
200InternalErrorCode.CouldNotAttachToDebugger,
201err.message || err,
202),
203response,
0d77292aJiglioNero4 years ago204),
d93677adRedMickey4 years ago205);
6e1bcd36RedMickey6 years ago206}
207
34472878RedMickey5 years ago208protected async disconnectRequest(
209response: DebugProtocol.DisconnectResponse,
210args: DebugProtocol.DisconnectArguments,
211request?: DebugProtocol.Request,
212): Promise<void> {
6e1bcd36RedMickey6 years ago213// The client is about to disconnect so first we need to stop app worker
214if (this.appWorker) {
215this.appWorker.stop();
216}
217
6e491635RedMickey6 years ago218this.onDidStartDebugSessionHandler.dispose();
219this.onDidTerminateDebugSessionHandler.dispose();
220
0d77292aJiglioNero4 years ago221return super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago222}
223
34472878RedMickey5 years ago224protected establishDebugSession(
225attachArgs: IAttachRequestArgs,
226resolve?: (value?: void | PromiseLike<void> | undefined) => void,
227): void {
1bdccb66RedMickey6 years ago228const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
229attachArgs,
230this.appLauncher.getCdpProxyPort(),
d93677adRedMickey4 years ago231this.rnSession.sessionId,
1bdccb66RedMickey6 years ago232);
a6562589RedMickey6 years ago233
34472878RedMickey5 years ago234vscode.debug
235.startDebugging(this.appLauncher.getWorkspaceFolder(), attachConfiguration, {
d93677adRedMickey4 years ago236parentSession: this.vsCodeDebugSession,
ebbd64f1RedMickey6 years ago237consoleMode: vscode.DebugConsoleMode.MergeWithParent,
34472878RedMickey5 years ago238})
239.then(
240(childDebugSessionStarted: boolean) => {
241if (childDebugSessionStarted) {
242this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
243this.setConnectionAllowedIfPossible();
244if (resolve) {
245this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
246resolve();
247}
248} else {
249this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
250this.setConnectionAllowedIfPossible();
251this.resetFirstConnectionStatus();
252throw new Error("Cannot start child debug session");
253}
254},
255err => {
256this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
257this.setConnectionAllowedIfPossible();
258this.resetFirstConnectionStatus();
259throw err;
260},
261);
4c757eebRedMickey6 years ago262}
6e1bcd36RedMickey6 years ago263
0d77292aJiglioNero4 years ago264private handleStartDebugSession(debugSession: vscode.DebugSession): void {
4c757eebRedMickey6 years ago265if (
d93677adRedMickey4 years ago266debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
34472878RedMickey5 years ago267debugSession.type === this.pwaNodeSessionName
4c757eebRedMickey6 years ago268) {
269this.nodeSession = debugSession;
270}
271}
272
0d77292aJiglioNero4 years ago273private handleTerminateDebugSession(debugSession: vscode.DebugSession): void {
4c757eebRedMickey6 years ago274if (
d93677adRedMickey4 years ago275debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
34472878RedMickey5 years ago276debugSession.type === this.pwaNodeSessionName
4c757eebRedMickey6 years ago277) {
ebbd64f1RedMickey6 years ago278if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago279this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago280} else {
19df32dcRedMickey4 years ago281void this.terminate();
ebbd64f1RedMickey6 years ago282}
4c757eebRedMickey6 years ago283}
284}
285
286private setConnectionAllowedIfPossible(): void {
287if (
34472878RedMickey5 years ago288this.debugSessionStatus === DebugSessionStatus.ConnectionDone ||
289this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
4c757eebRedMickey6 years ago290) {
291this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
292}
293}
294
295private resetFirstConnectionStatus(): void {
296if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
297this.debugSessionStatus = DebugSessionStatus.FirstConnection;
298}
299}
6e1bcd36RedMickey6 years ago300}