microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4cd259621ddfbd348fade892a2f3ee87fd1924c5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

233lines · 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 { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
7import { DebugProtocol } from "vscode-debugprotocol";
8import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper";
9import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
10import { ErrorHelper } from "../common/error/errorHelper";
11import { InternalErrorCode } from "../common/error/internalErrorCode";
12import { InternalError, NestedError } from "../common/error/internalError";
13import { IRunOptions, PlatformType } from "../extension/launchArgs";
14import { AppLauncher } from "../extension/appLauncher";
15import { LogLevel } from "../extension/log/LogHelper";
16import * as nls from "vscode-nls";
17import { SettingsHelper } from "../extension/settingsHelper";
18nls.config({
19 messageFormat: nls.MessageFormat.bundle,
20 bundleFormat: nls.BundleFormat.standalone,
21})();
22const localize = nls.loadMessageBundle();
23
24/**
25 * Enum of possible statuses of debug session
26 */
27export enum DebugSessionStatus {
28 /** A session has been just created */
29 FirstConnection,
30 /** This status is required in order to exclude the possible creation of several debug sessions at the first start */
31 FirstConnectionPending,
32 /** This status means that an application can be reloaded */
33 ConnectionAllowed,
34 /** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
35 ConnectionPending,
36 /** A debuggee connected successfully */
37 ConnectionDone,
38 /** A debuggee failed to connect */
39 ConnectionFailed,
40}
41
42export interface TerminateEventArgs {
43 debugSession: vscode.DebugSession;
44 args: any;
45}
46
47export interface IAttachRequestArgs
48 extends DebugProtocol.AttachRequestArguments,
49 IRunOptions,
50 vscode.DebugConfiguration {
51 webkitRangeMax: number;
52 webkitRangeMin: number;
53 cwd: string /* Automatically set by VS Code to the currently opened folder */;
54 port: number;
55 url?: string;
56 useHermesEngine: boolean;
57 address?: string;
58 trace?: string;
59 skipFiles?: [];
60 sourceMaps?: boolean;
61 sourceMapPathOverrides?: { [key: string]: string };
62}
63
64export interface ILaunchRequestArgs
65 extends DebugProtocol.LaunchRequestArguments,
66 IAttachRequestArgs {}
67
68export abstract class DebugSessionBase extends LoggingDebugSession {
69 protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
70 public static readonly onDidTerminateRootDebugSession =
71 DebugSessionBase.rootSessionTerminatedEventEmitter.event;
72
73 protected readonly stopCommand: string;
74 protected readonly pwaNodeSessionName: string;
75
76 protected appLauncher: AppLauncher;
77 protected projectRootPath: string;
78 protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
79 protected previousAttachArgs: IAttachRequestArgs;
80 protected cdpProxyLogLevel: LogLevel;
81 protected debugSessionStatus: DebugSessionStatus;
82 protected session: vscode.DebugSession;
83 protected cancellationTokenSource: vscode.CancellationTokenSource;
84
85 constructor(session: vscode.DebugSession) {
86 super();
87
88 // constants definition
89 this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
90 this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
91
92 // variables definition
93 this.session = session;
94 this.isSettingsInitialized = false;
95 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
96 this.cancellationTokenSource = new vscode.CancellationTokenSource();
97 }
98
99 protected initializeRequest(
100 response: DebugProtocol.InitializeResponse,
101 // eslint-disable-next-line @typescript-eslint/no-unused-vars
102 args: DebugProtocol.InitializeRequestArguments,
103 ): void {
104 response.body = response.body || {};
105
106 response.body.supportsConfigurationDoneRequest = true;
107 response.body.supportsEvaluateForHovers = true;
108 response.body.supportTerminateDebuggee = true;
109 response.body.supportsCancelRequest = true;
110
111 this.sendResponse(response);
112 }
113
114 protected abstract establishDebugSession(
115 attachArgs: IAttachRequestArgs,
116 resolve?: (value?: void | PromiseLike<void> | undefined) => void,
117 ): void;
118
119 protected async initializeSettings(args: any): Promise<void> {
120 if (!this.isSettingsInitialized) {
121 let chromeDebugCoreLogs = getLoggingDirectory();
122 if (chromeDebugCoreLogs) {
123 chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
124 }
125 let logLevel: string = args.trace;
126 if (logLevel) {
127 logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
128 logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
129 this.cdpProxyLogLevel =
130 LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
131 } else {
132 logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
133 this.cdpProxyLogLevel =
134 LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
135 }
136
137 if (typeof args.sourceMaps !== "boolean") {
138 args.sourceMaps = true;
139 }
140
141 if (typeof args.enableDebug !== "boolean") {
142 args.enableDebug = true;
143 }
144
145 // Now there is a problem with processing time of 'createFromSourceMap' function of js-debug
146 // So we disable this functionality by default https://github.com/microsoft/vscode-js-debug/issues/1033
147 if (typeof args.sourceMapRenames !== "boolean") {
148 args.sourceMapRenames = false;
149 }
150
151 const projectRootPath = SettingsHelper.getReactNativeProjectRoot(args.cwd);
152 const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
153 projectRootPath,
154 );
155 if (!isReactProject) {
156 throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
157 }
158
159 const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
160 projectRootPath,
161 );
162 this.appLauncher = appLauncher;
163 this.projectRootPath = projectRootPath;
164 this.isSettingsInitialized = true;
165 this.appLauncher.getOrUpdateNodeModulesRoot(true);
166 if (this.session.workspaceFolder) {
167 this.appLauncher.updateDebugConfigurationRoot(
168 this.session.workspaceFolder.uri.fsPath,
169 );
170 }
171 }
172 }
173
174 protected async disconnectRequest(
175 response: DebugProtocol.DisconnectResponse,
176 args: DebugProtocol.DisconnectArguments,
177 // eslint-disable-next-line @typescript-eslint/no-unused-vars
178 request?: DebugProtocol.Request,
179 ): Promise<void> {
180 if (this.appLauncher) {
181 await this.appLauncher.getRnCdpProxy().stopServer();
182 }
183
184 this.cancellationTokenSource.cancel();
185 this.cancellationTokenSource.dispose();
186
187 // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
188 if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
189 try {
190 this.appLauncher.getMobilePlatform().dispose();
191 } catch (err) {
192 logger.warn(
193 localize(
194 "CouldNotStopMonitoringLogcat",
195 "Couldn't stop monitoring logcat: {0}",
196 err.message || err,
197 ),
198 );
199 }
200 }
201
202 await logger.dispose();
203
204 DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
205 debugSession: this.session,
206 args: {
207 forcedStop: !!(<any>args).forcedStop,
208 },
209 });
210
211 this.sendResponse(response);
212 }
213
214 protected showError(error: Error, response: DebugProtocol.Response): void {
215 // We can't print error messages after the debugging session is stopped. This could break the extension work.
216 if (
217 (error instanceof InternalError || error instanceof NestedError) &&
218 error.errorCode === InternalErrorCode.CancellationTokenTriggered
219 ) {
220 return;
221 }
222
223 logger.error(error.message);
224
225 this.sendErrorResponse(
226 response,
227 { format: error.message, id: 1 },
228 undefined,
229 undefined,
230 ErrorDestination.User,
231 );
232 }
233}
234