microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
43e6ccc3781e2fec953ee0059638ffa0e5d403ab

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

296lines · 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";
34472878RedMickey5 years ago13import {
14DebugSessionBase,
15DebugSessionStatus,
16IAttachRequestArgs,
17ILaunchRequestArgs,
18} from "./debugSessionBase";
1bdccb66RedMickey6 years ago19import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
5514e287RedMickey6 years ago20import { ErrorHelper } from "../common/error/errorHelper";
21import { InternalErrorCode } from "../common/error/internalErrorCode";
6e1bcd36RedMickey6 years ago22import * as nls from "vscode-nls";
34472878RedMickey5 years ago23nls.config({
24messageFormat: nls.MessageFormat.bundle,
25bundleFormat: nls.BundleFormat.standalone,
26})();
6e1bcd36RedMickey6 years ago27const localize = nls.loadMessageBundle();
28
2c19da7fRedMickey6 years ago29export class RNDebugSession extends DebugSessionBase {
4c757eebRedMickey6 years ago30private readonly terminateCommand: string;
f872f4d5RedMickey6 years ago31
e23d1841RedMickey6 years ago32private appWorker: MultipleLifetimesAppWorker | null;
4c757eebRedMickey6 years ago33private nodeSession: vscode.DebugSession | null;
6e491635RedMickey6 years ago34private onDidStartDebugSessionHandler: vscode.Disposable;
35private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago36
2c19da7fRedMickey6 years ago37constructor(session: vscode.DebugSession) {
38super(session);
4c757eebRedMickey6 years ago39
40// constants definition
41this.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
42
43// variables definition
e23d1841RedMickey6 years ago44this.appWorker = null;
45
6e491635RedMickey6 years ago46this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
34472878RedMickey5 years ago47this.handleStartDebugSession.bind(this),
4c757eebRedMickey6 years ago48);
49
6e491635RedMickey6 years ago50this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago51this.handleTerminateDebugSession.bind(this),
4c757eebRedMickey6 years ago52);
6e1bcd36RedMickey6 years ago53}
54
34472878RedMickey5 years ago55protected async launchRequest(
56response: DebugProtocol.LaunchResponse,
57launchArgs: ILaunchRequestArgs,
58// eslint-disable-next-line @typescript-eslint/no-unused-vars
59request?: DebugProtocol.Request,
60): Promise<void> {
0d77292aJiglioNero4 years ago61try {
62try {
63await this.initializeSettings(launchArgs);
64logger.log("Launching the application");
65logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
34472878RedMickey5 years ago66
0d77292aJiglioNero4 years ago67await this.appLauncher.launch(launchArgs);
68
69if (!launchArgs.enableDebug) {
70this.sendResponse(response);
71// if debugging is not enabled skip attach request
72return;
73}
74} catch (error) {
75throw ErrorHelper.getInternalError(
76InternalErrorCode.ApplicationLaunchFailed,
77error.message || error,
78);
79}
80// if debugging is enabled start attach request
81await this.attachRequest(response, launchArgs);
82} catch (error) {
83this.showError(error, response);
84}
6e1bcd36RedMickey6 years ago85}
86
34472878RedMickey5 years ago87protected async attachRequest(
88response: DebugProtocol.AttachResponse,
89attachArgs: IAttachRequestArgs,
90// eslint-disable-next-line @typescript-eslint/no-unused-vars
91request?: DebugProtocol.Request,
92): Promise<void> {
6e1bcd36RedMickey6 years ago93let extProps = {
94platform: {
95value: attachArgs.platform,
96isPii: false,
97},
98};
99
100this.previousAttachArgs = attachArgs;
0d77292aJiglioNero4 years ago101
102return new Promise<void>(async (resolve, reject) => {
103try {
104await this.initializeSettings(attachArgs);
105logger.log("Attaching to the application");
106logger.verbose(
107`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`,
108);
109
110const versions = await ProjectVersionHelper.getReactNativeVersions(
111this.projectRootPath,
112ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
113);
114extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
115attachArgs,
116versions,
117extProps,
118);
119
120// eslint-disable-next-line @typescript-eslint/no-unused-vars
121await TelemetryHelper.generate("attach", extProps, async generator => {
122attachArgs.port =
123attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
124
125const cdpProxy = this.appLauncher.getRnCdpProxy();
126await cdpProxy.stopServer();
127await cdpProxy.initializeServer(
128new RnCDPMessageHandler(),
129this.cdpProxyLogLevel,
34472878RedMickey5 years ago130);
0d77292aJiglioNero4 years ago131
132await this.appLauncher.getPackager().start();
133
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;
180this.nodeSession.customRequest(this.terminateCommand);
181}
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}
193}).catch(err =>
194this.showError(
195ErrorHelper.getInternalError(
196InternalErrorCode.CouldNotAttachToDebugger,
197err.message || err,
198),
199response,
200),
201);
6e1bcd36RedMickey6 years ago202}
203
34472878RedMickey5 years ago204protected async disconnectRequest(
205response: DebugProtocol.DisconnectResponse,
206args: DebugProtocol.DisconnectArguments,
207request?: DebugProtocol.Request,
208): Promise<void> {
6e1bcd36RedMickey6 years ago209// The client is about to disconnect so first we need to stop app worker
210if (this.appWorker) {
211this.appWorker.stop();
212}
213
6e491635RedMickey6 years ago214this.onDidStartDebugSessionHandler.dispose();
215this.onDidTerminateDebugSessionHandler.dispose();
216
0d77292aJiglioNero4 years ago217return super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago218}
219
34472878RedMickey5 years ago220protected establishDebugSession(
221attachArgs: IAttachRequestArgs,
222resolve?: (value?: void | PromiseLike<void> | undefined) => void,
223): void {
1bdccb66RedMickey6 years ago224const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
225attachArgs,
226this.appLauncher.getCdpProxyPort(),
34472878RedMickey5 years ago227this.session.id,
1bdccb66RedMickey6 years ago228);
a6562589RedMickey6 years ago229
34472878RedMickey5 years ago230vscode.debug
231.startDebugging(this.appLauncher.getWorkspaceFolder(), attachConfiguration, {
ebbd64f1RedMickey6 years ago232parentSession: this.session,
233consoleMode: vscode.DebugConsoleMode.MergeWithParent,
34472878RedMickey5 years ago234})
235.then(
236(childDebugSessionStarted: boolean) => {
237if (childDebugSessionStarted) {
238this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
239this.setConnectionAllowedIfPossible();
240if (resolve) {
241this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
242resolve();
243}
244} else {
245this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
246this.setConnectionAllowedIfPossible();
247this.resetFirstConnectionStatus();
248throw new Error("Cannot start child debug session");
249}
250},
251err => {
252this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
253this.setConnectionAllowedIfPossible();
254this.resetFirstConnectionStatus();
255throw err;
256},
257);
4c757eebRedMickey6 years ago258}
6e1bcd36RedMickey6 years ago259
0d77292aJiglioNero4 years ago260private handleStartDebugSession(debugSession: vscode.DebugSession): void {
4c757eebRedMickey6 years ago261if (
34472878RedMickey5 years ago262debugSession.configuration.rnDebugSessionId === this.session.id &&
263debugSession.type === this.pwaNodeSessionName
4c757eebRedMickey6 years ago264) {
265this.nodeSession = debugSession;
266}
267}
268
0d77292aJiglioNero4 years ago269private handleTerminateDebugSession(debugSession: vscode.DebugSession): void {
4c757eebRedMickey6 years ago270if (
34472878RedMickey5 years ago271debugSession.configuration.rnDebugSessionId === this.session.id &&
272debugSession.type === this.pwaNodeSessionName
4c757eebRedMickey6 years ago273) {
ebbd64f1RedMickey6 years ago274if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago275this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago276} else {
a2ddbba5RedMickey5 years ago277vscode.commands.executeCommand(this.stopCommand, this.session);
ebbd64f1RedMickey6 years ago278}
4c757eebRedMickey6 years ago279}
280}
281
282private setConnectionAllowedIfPossible(): void {
283if (
34472878RedMickey5 years ago284this.debugSessionStatus === DebugSessionStatus.ConnectionDone ||
285this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
4c757eebRedMickey6 years ago286) {
287this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
288}
289}
290
291private resetFirstConnectionStatus(): void {
292if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
293this.debugSessionStatus = DebugSessionStatus.FirstConnection;
294}
295}
6e1bcd36RedMickey6 years ago296}