microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
34472878f9e8d227bd5d0902161c571864c5d12d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

239lines · 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 { ILaunchArgs, 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 extends DebugProtocol.AttachRequestArguments, ILaunchArgs {
49 webkitRangeMax: number;
50 webkitRangeMin: number;
51 cwd: string /* Automatically set by VS Code to the currently opened folder */;
52 port: number;
53 url?: string;
54 address?: string;
55 trace?: string;
56 skipFiles?: [];
57 sourceMaps?: boolean;
58 sourceMapPathOverrides?: { [key: string]: string };
59}
60
61export interface ILaunchRequestArgs
62 extends DebugProtocol.LaunchRequestArguments,
63 IAttachRequestArgs {}
64
65export abstract class DebugSessionBase extends LoggingDebugSession {
66 protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
67 public static readonly onDidTerminateRootDebugSession =
68 DebugSessionBase.rootSessionTerminatedEventEmitter.event;
69
70 protected readonly stopCommand: string;
71 protected readonly pwaNodeSessionName: string;
72
73 protected appLauncher: AppLauncher;
74 protected projectRootPath: string;
75 protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
76 protected previousAttachArgs: IAttachRequestArgs;
77 protected cdpProxyLogLevel: LogLevel;
78 protected debugSessionStatus: DebugSessionStatus;
79 protected session: vscode.DebugSession;
80 protected cancellationTokenSource: vscode.CancellationTokenSource;
81
82 constructor(session: vscode.DebugSession) {
83 super();
84
85 // constants definition
86 this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
87 this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
88
89 // variables definition
90 this.session = session;
91 this.isSettingsInitialized = false;
92 this.debugSessionStatus = DebugSessionStatus.FirstConnection;
93 this.cancellationTokenSource = new vscode.CancellationTokenSource();
94 }
95
96 protected initializeRequest(
97 response: DebugProtocol.InitializeResponse,
98 // eslint-disable-next-line @typescript-eslint/no-unused-vars
99 args: DebugProtocol.InitializeRequestArguments,
100 ): void {
101 response.body = response.body || {};
102
103 response.body.supportsConfigurationDoneRequest = true;
104 response.body.supportsEvaluateForHovers = true;
105 response.body.supportTerminateDebuggee = true;
106 response.body.supportsCancelRequest = true;
107
108 this.sendResponse(response);
109 }
110
111 protected abstract establishDebugSession(
112 attachArgs: IAttachRequestArgs,
113 resolve?: (value?: void | PromiseLike<void> | undefined) => void,
114 ): void;
115
116 protected initializeSettings(args: any): Promise<any> {
117 if (!this.isSettingsInitialized) {
118 let chromeDebugCoreLogs = getLoggingDirectory();
119 if (chromeDebugCoreLogs) {
120 chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
121 }
122 let logLevel: string = args.trace;
123 if (logLevel) {
124 logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
125 logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
126 this.cdpProxyLogLevel =
127 LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
128 } else {
129 logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
130 this.cdpProxyLogLevel =
131 LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
132 }
133
134 if (typeof args.sourceMaps !== "boolean") {
135 args.sourceMaps = true;
136 }
137
138 if (typeof args.enableDebug !== "boolean") {
139 args.enableDebug = true;
140 }
141
142 const projectRootPath = getProjectRoot(args);
143 return ReactNativeProjectHelper.isReactNativeProject(projectRootPath).then(result => {
144 if (!result) {
145 throw ErrorHelper.getInternalError(
146 InternalErrorCode.NotInReactNativeFolderError,
147 );
148 }
149 this.projectRootPath = projectRootPath;
150 this.appLauncher = AppLauncher.getAppLauncherByProjectRootPath(projectRootPath);
151 this.isSettingsInitialized = true;
152
153 return void 0;
154 });
155 } else {
156 return Promise.resolve();
157 }
158 }
159
160 protected async disconnectRequest(
161 response: DebugProtocol.DisconnectResponse,
162 args: DebugProtocol.DisconnectArguments,
163 // eslint-disable-next-line @typescript-eslint/no-unused-vars
164 request?: DebugProtocol.Request,
165 ): Promise<void> {
166 if (this.appLauncher) {
167 await this.appLauncher.getRnCdpProxy().stopServer();
168 }
169
170 this.cancellationTokenSource.cancel();
171 this.cancellationTokenSource.dispose();
172
173 // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
174 if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
175 try {
176 this.appLauncher.getMobilePlatform().dispose();
177 } catch (err) {
178 logger.warn(
179 localize(
180 "CouldNotStopMonitoringLogcat",
181 "Couldn't stop monitoring logcat: {0}",
182 err.message || err,
183 ),
184 );
185 }
186 }
187
188 await logger.dispose();
189
190 DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
191 debugSession: this.session,
192 args: {
193 forcedStop: !!(<any>args).forcedStop,
194 },
195 });
196
197 this.sendResponse(response);
198 }
199
200 protected showError(error: Error, response: DebugProtocol.Response): void {
201 // We can't print error messages after the debugging session is stopped. This could break the extension work.
202 if (
203 (error instanceof InternalError || error instanceof NestedError) &&
204 error.errorCode === InternalErrorCode.CancellationTokenTriggered
205 ) {
206 return;
207 }
208
209 this.sendErrorResponse(
210 response,
211 { format: error.message, id: 1 },
212 undefined,
213 undefined,
214 ErrorDestination.User,
215 );
216 }
217}
218
219/**
220 * Parses settings.json file for workspace root property
221 */
222export function getProjectRoot(args: any): string {
223 const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
224 const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
225 try {
226 let settingsContent = fs.readFileSync(settingsPath, "utf8");
227 settingsContent = stripJsonComments(settingsContent);
228 let parsedSettings = JSON.parse(settingsContent);
229 let projectRootPath =
230 parsedSettings["react-native-tools.projectRoot"] ||
231 parsedSettings["react-native-tools"].projectRoot;
232 return path.resolve(vsCodeRoot, projectRootPath);
233 } catch (e) {
234 logger.verbose(
235 `${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`,
236 );
237 return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
238 }
239}
240