microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
transitive-dependency-serialize-javascript

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

332lines · 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 path from "path";
5import * as fs from "fs";
6import * as vscode from "vscode";
7import { LoggingDebugSession, Logger, logger, ErrorDestination } from "@vscode/debugadapter";
8import { DebugProtocol } from "vscode-debugprotocol";
9import * as nls from "vscode-nls";
10import { stripJsonTrailingComma } from "../common/utils";
11import { getLoggingDirectory, LogHelper, LogLevel } from "../extension/log/LogHelper";
12import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
13import { ErrorHelper } from "../common/error/errorHelper";
14import { InternalErrorCode } from "../common/error/internalErrorCode";
15import { InternalError, NestedError } from "../common/error/internalError";
16import { ILaunchArgs, IRunOptions, PlatformType } from "../extension/launchArgs";
17import { AppLauncher } from "../extension/appLauncher";
18import { RNPackageVersions } from "../common/projectVersionHelper";
19import { SettingsHelper } from "../extension/settingsHelper";
20import { OutputChannelLogger } from "../extension/log/OutputChannelLogger";
21import { RNSession } from "./debugSessionWrapper";
22
23nls.config({
24 messageFormat: nls.MessageFormat.bundle,
25 bundleFormat: nls.BundleFormat.standalone,
26})();
27const localize = nls.loadMessageBundle();
28
29/**
30 * Enum of possible statuses of debug session
31 */
32export enum DebugSessionStatus {
33 /** A session has been just created */
34 FirstConnection,
35 /** This status is required in order to exclude the possible creation of several debug sessions at the first start */
36 FirstConnectionPending,
37 /** This status means that an application can be reloaded */
38 ConnectionAllowed,
39 /** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
40 ConnectionPending,
41 /** A debuggee connected successfully */
42 ConnectionDone,
43 /** A debuggee failed to connect */
44 ConnectionFailed,
45 /** The session is handling disconnect request now */
46 Stopping,
47 /** The session is stopped */
48 Stopped,
49}
50
51export interface TerminateEventArgs {
52 debugSession: vscode.DebugSession;
53 args: any;
54}
55
56export interface IAttachRequestArgs
57 extends DebugProtocol.AttachRequestArguments,
58 IRunOptions,
59 vscode.DebugConfiguration {
60 webkitRangeMax: number;
61 webkitRangeMin: number;
62 cwd: string /* Automatically set by VS Code to the currently opened folder */;
63 port: number;
64 url?: string;
65 useHermesEngine: boolean;
66 address?: string;
67 trace?: string;
68 skipFiles?: [];
69 sourceMaps?: boolean;
70 sourceMapPathOverrides?: { [key: string]: string };
71 jsDebugTrace?: boolean;
72 browserTarget?: string;
73}
74
75export interface ILaunchRequestArgs
76 extends DebugProtocol.LaunchRequestArguments,
77 IAttachRequestArgs {}
78
79export abstract class DebugSessionBase extends LoggingDebugSession {
80 protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> =
81 new vscode.EventEmitter<TerminateEventArgs>();
82 public static readonly onDidTerminateRootDebugSession =
83 DebugSessionBase.rootSessionTerminatedEventEmitter.event;
84
85 protected readonly stopCommand: string;
86 protected readonly terminateCommand: string;
87 protected readonly pwaNodeSessionName: string;
88
89 protected appLauncher: AppLauncher;
90 protected projectRootPath: string;
91 protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
92 protected previousAttachArgs: IAttachRequestArgs;
93 protected cdpProxyLogLevel: LogLevel;
94 protected debugSessionStatus: DebugSessionStatus;
95 protected nodeSession: vscode.DebugSession | null;
96 protected rnSession: RNSession;
97 protected vsCodeDebugSession: vscode.DebugSession;
98 protected cancellationTokenSource: vscode.CancellationTokenSource;
99
100 constructor(rnSession: RNSession) {
101 super();
102
103 // constants definition
104 this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
105 this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
106 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
107
108 // variables definition
109 this.rnSession = rnSession;
110 this.vsCodeDebugSession = rnSession.vsCodeDebugSession;
111 this.isSettingsInitialized = false;
112 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
113 this.cancellationTokenSource = new vscode.CancellationTokenSource();
114 this.nodeSession = null;
115 }
116
117 protected initializeRequest(
118 response: DebugProtocol.InitializeResponse,
119 // eslint-disable-next-line @typescript-eslint/no-unused-vars
120 args: DebugProtocol.InitializeRequestArguments,
121 ): void {
122 response.body = response.body || {};
123
124 response.body.supportsConfigurationDoneRequest = true;
125 response.body.supportsEvaluateForHovers = true;
126 response.body.supportTerminateDebuggee = true;
127 response.body.supportsCancelRequest = true;
128
129 response.body.exceptionBreakpointFilters = [
130 {
131 filter: "all",
132 label: "Caught Exceptions",
133 default: false,
134 supportsCondition: true,
135 description: "Breaks on all throw errors, even if they're caught later.",
136 // eslint-disable-next-line @typescript-eslint/quotes
137 conditionDescription: 'error.name == "MyError"',
138 },
139 {
140 filter: "uncaught",
141 label: "Uncaught Exceptions",
142 default: false,
143 supportsCondition: true,
144 description: "Breaks only on errors or promise rejections that are not handled.",
145 // eslint-disable-next-line @typescript-eslint/quotes
146 conditionDescription: 'error.name == "MyError"',
147 },
148 ];
149
150 this.sendResponse(response);
151 }
152
153 protected abstract establishDebugSession(
154 attachArgs: IAttachRequestArgs,
155 resolve?: (value?: void | PromiseLike<void> | undefined) => void,
156 ): void;
157
158 protected async initializeSettings(args: any): Promise<void> {
159 if (!this.isSettingsInitialized) {
160 let chromeDebugCoreLogs = getLoggingDirectory();
161 if (chromeDebugCoreLogs) {
162 chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
163 }
164 let logLevel: string = args.trace;
165 if (logLevel) {
166 logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
167 logger.setup((Logger.LogLevel as any)[logLevel], chromeDebugCoreLogs || false);
168 this.cdpProxyLogLevel =
169 (LogLevel as any)[logLevel] === LogLevel.Verbose
170 ? LogLevel.Custom
171 : LogLevel.None;
172 } else {
173 logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
174 this.cdpProxyLogLevel =
175 LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
176 }
177
178 if (typeof args.sourceMaps !== "boolean") {
179 args.sourceMaps = true;
180 }
181
182 if (typeof args.enableDebug !== "boolean") {
183 args.enableDebug = true;
184 }
185
186 // Now there is a problem with processing time of 'createFromSourceMap' function of js-debug
187 // So we disable this functionality by default https://github.com/microsoft/vscode-js-debug/issues/1033
188 if (typeof args.sourceMapRenames !== "boolean") {
189 args.sourceMapRenames = false;
190 }
191
192 const projectRootPath = SettingsHelper.getReactNativeProjectRoot(args.cwd);
193 const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
194 projectRootPath,
195 );
196 if (!isReactProject) {
197 throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
198 }
199
200 const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
201 projectRootPath,
202 );
203 this.appLauncher = appLauncher;
204 this.projectRootPath = projectRootPath;
205 this.isSettingsInitialized = true;
206 this.appLauncher.getOrUpdateNodeModulesRoot(true);
207 if (this.vsCodeDebugSession.workspaceFolder) {
208 this.appLauncher.updateDebugConfigurationRoot(
209 this.vsCodeDebugSession.workspaceFolder.uri.fsPath,
210 );
211 }
212 const settingsPort = this.appLauncher.getPackagerPort(projectRootPath);
213 if (this.appLauncher.getPackager().getPort() != settingsPort) {
214 this.appLauncher.getPackager().resetToSettingsPort();
215 }
216 }
217 }
218
219 protected async disconnectRequest(
220 response: DebugProtocol.DisconnectResponse,
221 args: DebugProtocol.DisconnectArguments,
222 // eslint-disable-next-line @typescript-eslint/no-unused-vars
223 request?: DebugProtocol.Request,
224 ): Promise<void> {
225 if (this.appLauncher) {
226 await this.appLauncher.getRnCdpProxy().stopServer();
227 }
228
229 this.cancellationTokenSource.cancel();
230 this.cancellationTokenSource.dispose();
231
232 // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
233 if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
234 try {
235 this.appLauncher.getMobilePlatform().dispose();
236 } catch (err) {
237 logger.warn(
238 localize(
239 "CouldNotStopMonitoringLogcat",
240 "Couldn't stop monitoring logcat: {0}",
241 err.message || err,
242 ),
243 );
244 }
245 }
246
247 this.debugSessionStatus = DebugSessionStatus.Stopped;
248 await logger.dispose();
249
250 DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
251 debugSession: this.vsCodeDebugSession,
252 args: {
253 forcedStop: !!(<any>args).forcedStop,
254 },
255 });
256
257 this.sendResponse(response);
258 }
259
260 protected terminateWithErrorResponse(error: Error, response: DebugProtocol.Response): void {
261 // We can't print error messages after the debugging session is stopped. This could break the extension work.
262 if (
263 (error instanceof InternalError || error instanceof NestedError) &&
264 error.errorCode === InternalErrorCode.CancellationTokenTriggered
265 ) {
266 return;
267 }
268
269 logger.error(error.message);
270
271 this.sendErrorResponse(
272 response,
273 { format: error.message, id: 1 },
274 undefined,
275 undefined,
276 ErrorDestination.User,
277 );
278 }
279
280 protected async preparePackagerBeforeAttach(
281 args: IAttachRequestArgs,
282 reactNativeVersions: RNPackageVersions,
283 ): Promise<void> {
284 if (!(await this.appLauncher.getPackager().isRunning())) {
285 const runOptions: ILaunchArgs = Object.assign(
286 { reactNativeVersions },
287 this.appLauncher.prepareBaseRunOptions(args),
288 );
289 this.appLauncher.getPackager().setRunOptions(runOptions);
290 await this.appLauncher.getPackager().start();
291 }
292 }
293
294 protected showError(error: Error): void {
295 void vscode.window.showErrorMessage(error.message, {
296 modal: true,
297 });
298 // We can't print error messages via debug session logger after the session is stopped. This could break the extension work.
299 if (this.debugSessionStatus === DebugSessionStatus.Stopped) {
300 OutputChannelLogger.getMainChannel().error(error.message);
301 return;
302 }
303 logger.error(error.message);
304 }
305
306 protected async terminate(): Promise<void> {
307 await vscode.commands.executeCommand(this.stopCommand, undefined, {
308 sessionId: this.vsCodeDebugSession.id,
309 });
310 }
311}
312
313/**
314 * Parses settings.json file for workspace root property
315 */
316export function getProjectRoot(args: any): string {
317 const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
318 const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
319 try {
320 const settingsContent = fs.readFileSync(settingsPath, "utf8");
321 const parsedSettings = stripJsonTrailingComma(settingsContent);
322 const projectRootPath =
323 parsedSettings["react-native-tools.projectRoot"] ||
324 parsedSettings["react-native-tools"].projectRoot;
325 return path.resolve(vsCodeRoot, projectRootPath);
326 } catch (e) {
327 logger.verbose(
328 `${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`,
329 );
330 return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
331 }
332}
333