microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
960a6ec47eafad42cd52a36af455604d4650cfff

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

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