microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
improve-package-name-resolver-test

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

332lines · modeblame

2c19da7fRedMickey6 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 path from "path";
5import * as fs from "fs";
09f6024fHeniker4 years ago6import * as vscode from "vscode";
623be8a6Ezio Li2 years ago7import { LoggingDebugSession, Logger, logger, ErrorDestination } from "@vscode/debugadapter";
2c19da7fRedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
09f6024fHeniker4 years ago9import * as nls from "vscode-nls";
dc94981bQuan Jin3 years ago10import { stripJsonTrailingComma } from "../common/utils";
09f6024fHeniker4 years ago11import { getLoggingDirectory, LogHelper, LogLevel } from "../extension/log/LogHelper";
2c19da7fRedMickey6 years ago12import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
13import { ErrorHelper } from "../common/error/errorHelper";
14import { InternalErrorCode } from "../common/error/internalErrorCode";
e23d1841RedMickey6 years ago15import { InternalError, NestedError } from "../common/error/internalError";
bfcc8a29Samriel4 years ago16import { ILaunchArgs, IRunOptions, PlatformType } from "../extension/launchArgs";
2c19da7fRedMickey6 years ago17import { AppLauncher } from "../extension/appLauncher";
bfcc8a29Samriel4 years ago18import { RNPackageVersions } from "../common/projectVersionHelper";
43e6ccc3JiglioNero4 years ago19import { SettingsHelper } from "../extension/settingsHelper";
19df32dcRedMickey4 years ago20import { OutputChannelLogger } from "../extension/log/OutputChannelLogger";
d93677adRedMickey4 years ago21import { RNSession } from "./debugSessionWrapper";
bfcc8a29Samriel4 years ago22
34472878RedMickey5 years ago23nls.config({
24messageFormat: nls.MessageFormat.bundle,
25bundleFormat: nls.BundleFormat.standalone,
26})();
e23d1841RedMickey6 years ago27const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago28
29/**
30* Enum of possible statuses of debug session
31*/
32export enum DebugSessionStatus {
33/** A session has been just created */
34FirstConnection,
35/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
36FirstConnectionPending,
37/** This status means that an application can be reloaded */
38ConnectionAllowed,
39/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
40ConnectionPending,
41/** A debuggee connected successfully */
42ConnectionDone,
43/** A debuggee failed to connect */
44ConnectionFailed,
19df32dcRedMickey4 years ago45/** The session is handling disconnect request now */
46Stopping,
47/** The session is stopped */
48Stopped,
2c19da7fRedMickey6 years ago49}
50
ebbd64f1RedMickey6 years ago51export interface TerminateEventArgs {
52debugSession: vscode.DebugSession;
53args: any;
54}
55
5471436aRedMickey5 years ago56export interface IAttachRequestArgs
57extends DebugProtocol.AttachRequestArguments,
58IRunOptions,
59vscode.DebugConfiguration {
259c018fYuri Skorokhodov5 years ago60webkitRangeMax: number;
61webkitRangeMin: number;
34472878RedMickey5 years ago62cwd: string /* Automatically set by VS Code to the currently opened folder */;
2c19da7fRedMickey6 years ago63port: number;
64url?: string;
6f9a0779JiglioNero5 years ago65useHermesEngine: boolean;
2c19da7fRedMickey6 years ago66address?: string;
67trace?: string;
5d47053fRedMickey6 years ago68skipFiles?: [];
1bdccb66RedMickey6 years ago69sourceMaps?: boolean;
70sourceMapPathOverrides?: { [key: string]: string };
2d89fb47Ezio Li3 years ago71jsDebugTrace?: boolean;
9f8c460dEzio Li2 years ago72browserTarget?: string;
2c19da7fRedMickey6 years ago73}
74
34472878RedMickey5 years ago75export interface ILaunchRequestArgs
76extends DebugProtocol.LaunchRequestArguments,
77IAttachRequestArgs {}
2c19da7fRedMickey6 years ago78
79export abstract class DebugSessionBase extends LoggingDebugSession {
09f6024fHeniker4 years ago80protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> =
81new vscode.EventEmitter<TerminateEventArgs>();
34472878RedMickey5 years ago82public static readonly onDidTerminateRootDebugSession =
83DebugSessionBase.rootSessionTerminatedEventEmitter.event;
ebbd64f1RedMickey6 years ago84
a2ddbba5RedMickey5 years ago85protected readonly stopCommand: string;
19df32dcRedMickey4 years ago86protected readonly terminateCommand: string;
ebbd64f1RedMickey6 years ago87protected readonly pwaNodeSessionName: string;
88
2c19da7fRedMickey6 years ago89protected appLauncher: AppLauncher;
90protected projectRootPath: string;
91protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
92protected previousAttachArgs: IAttachRequestArgs;
93protected cdpProxyLogLevel: LogLevel;
94protected debugSessionStatus: DebugSessionStatus;
19df32dcRedMickey4 years ago95protected nodeSession: vscode.DebugSession | null;
d93677adRedMickey4 years ago96protected rnSession: RNSession;
97protected vsCodeDebugSession: vscode.DebugSession;
e23d1841RedMickey6 years ago98protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago99
d93677adRedMickey4 years ago100constructor(rnSession: RNSession) {
2c19da7fRedMickey6 years ago101super();
102
ebbd64f1RedMickey6 years ago103// constants definition
104this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
a2ddbba5RedMickey5 years ago105this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
19df32dcRedMickey4 years ago106this.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
ebbd64f1RedMickey6 years ago107
108// variables definition
d93677adRedMickey4 years ago109this.rnSession = rnSession;
110this.vsCodeDebugSession = rnSession.vsCodeDebugSession;
2c19da7fRedMickey6 years ago111this.isSettingsInitialized = false;
112this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago113this.cancellationTokenSource = new vscode.CancellationTokenSource();
19df32dcRedMickey4 years ago114this.nodeSession = null;
e23d1841RedMickey6 years ago115}
116
34472878RedMickey5 years ago117protected initializeRequest(
118response: DebugProtocol.InitializeResponse,
119// eslint-disable-next-line @typescript-eslint/no-unused-vars
120args: DebugProtocol.InitializeRequestArguments,
121): void {
e23d1841RedMickey6 years ago122response.body = response.body || {};
123
124response.body.supportsConfigurationDoneRequest = true;
125response.body.supportsEvaluateForHovers = true;
126response.body.supportTerminateDebuggee = true;
127response.body.supportsCancelRequest = true;
128
1bb1a217EzioLi2 years ago129response.body.exceptionBreakpointFilters = [
130{
131filter: "all",
132label: "Caught Exceptions",
133default: false,
134supportsCondition: true,
135description: "Breaks on all throw errors, even if they're caught later.",
136// eslint-disable-next-line @typescript-eslint/quotes
137conditionDescription: 'error.name == "MyError"',
138},
139{
140filter: "uncaught",
141label: "Uncaught Exceptions",
142default: false,
143supportsCondition: true,
144description: "Breaks only on errors or promise rejections that are not handled.",
145// eslint-disable-next-line @typescript-eslint/quotes
146conditionDescription: 'error.name == "MyError"',
147},
148];
149
e23d1841RedMickey6 years ago150this.sendResponse(response);
2c19da7fRedMickey6 years ago151}
152
34472878RedMickey5 years ago153protected abstract establishDebugSession(
154attachArgs: IAttachRequestArgs,
155resolve?: (value?: void | PromiseLike<void> | undefined) => void,
156): void;
b7451aefRedMickey6 years ago157
0d77292aJiglioNero4 years ago158protected async initializeSettings(args: any): Promise<void> {
2c19da7fRedMickey6 years ago159if (!this.isSettingsInitialized) {
160let chromeDebugCoreLogs = getLoggingDirectory();
161if (chromeDebugCoreLogs) {
162chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
163}
164let logLevel: string = args.trace;
165if (logLevel) {
166logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
5b09cf97Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago167logger.setup((Logger.LogLevel as any)[logLevel], chromeDebugCoreLogs || false);
34472878RedMickey5 years ago168this.cdpProxyLogLevel =
5b09cf97Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago169(LogLevel as any)[logLevel] === LogLevel.Verbose
170? LogLevel.Custom
171: LogLevel.None;
2c19da7fRedMickey6 years ago172} else {
173logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
34472878RedMickey5 years ago174this.cdpProxyLogLevel =
175LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago176}
177
2db9ac85Yuri Skorokhodov5 years ago178if (typeof args.sourceMaps !== "boolean") {
2c19da7fRedMickey6 years ago179args.sourceMaps = true;
180}
181
5514e287RedMickey6 years ago182if (typeof args.enableDebug !== "boolean") {
183args.enableDebug = true;
184}
185
81fc1822JiglioNero4 years ago186// 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
188if (typeof args.sourceMapRenames !== "boolean") {
189args.sourceMapRenames = false;
190}
191
43e6ccc3JiglioNero4 years ago192const projectRootPath = SettingsHelper.getReactNativeProjectRoot(args.cwd);
0d77292aJiglioNero4 years ago193const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
194projectRootPath,
195);
196if (!isReactProject) {
197throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
198}
4dfb1c4cetatanova5 years ago199
0d77292aJiglioNero4 years ago200const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
201projectRootPath,
4dfb1c4cetatanova5 years ago202);
0d77292aJiglioNero4 years ago203this.appLauncher = appLauncher;
204this.projectRootPath = projectRootPath;
205this.isSettingsInitialized = true;
206this.appLauncher.getOrUpdateNodeModulesRoot(true);
d93677adRedMickey4 years ago207if (this.vsCodeDebugSession.workspaceFolder) {
0d77292aJiglioNero4 years ago208this.appLauncher.updateDebugConfigurationRoot(
d93677adRedMickey4 years ago209this.vsCodeDebugSession.workspaceFolder.uri.fsPath,
0d77292aJiglioNero4 years ago210);
211}
b84470b5Ezio Li2 years ago212const settingsPort = this.appLauncher.getPackagerPort(projectRootPath);
213if (this.appLauncher.getPackager().getPort() != settingsPort) {
214this.appLauncher.getPackager().resetToSettingsPort();
215}
2c19da7fRedMickey6 years ago216}
217}
984ca036RedMickey6 years ago218
34472878RedMickey5 years ago219protected async disconnectRequest(
220response: DebugProtocol.DisconnectResponse,
221args: DebugProtocol.DisconnectArguments,
222// eslint-disable-next-line @typescript-eslint/no-unused-vars
223request?: DebugProtocol.Request,
224): Promise<void> {
a32e1e1fYuri Skorokhodov5 years ago225if (this.appLauncher) {
226await this.appLauncher.getRnCdpProxy().stopServer();
227}
e23d1841RedMickey6 years ago228
229this.cancellationTokenSource.cancel();
230this.cancellationTokenSource.dispose();
231
232// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
259c018fYuri Skorokhodov5 years ago233if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
e23d1841RedMickey6 years ago234try {
8df5011eYuri Skorokhodov5 years ago235this.appLauncher.getMobilePlatform().dispose();
e23d1841RedMickey6 years ago236} catch (err) {
34472878RedMickey5 years ago237logger.warn(
238localize(
239"CouldNotStopMonitoringLogcat",
240"Couldn't stop monitoring logcat: {0}",
241err.message || err,
242),
243);
e23d1841RedMickey6 years ago244}
245}
246
19df32dcRedMickey4 years ago247this.debugSessionStatus = DebugSessionStatus.Stopped;
67ffa5b4RedMickey6 years ago248await logger.dispose();
249
ebbd64f1RedMickey6 years ago250DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
d93677adRedMickey4 years ago251debugSession: this.vsCodeDebugSession,
ebbd64f1RedMickey6 years ago252args: {
a2ddbba5RedMickey5 years ago253forcedStop: !!(<any>args).forcedStop,
ebbd64f1RedMickey6 years ago254},
255});
256
257this.sendResponse(response);
e23d1841RedMickey6 years ago258}
259
19df32dcRedMickey4 years ago260protected terminateWithErrorResponse(error: Error, response: DebugProtocol.Response): void {
e23d1841RedMickey6 years ago261// We can't print error messages after the debugging session is stopped. This could break the extension work.
34472878RedMickey5 years ago262if (
263(error instanceof InternalError || error instanceof NestedError) &&
264error.errorCode === InternalErrorCode.CancellationTokenTriggered
e23d1841RedMickey6 years ago265) {
266return;
267}
268
28ceac00RedMickey4 years ago269logger.error(error.message);
270
984ca036RedMickey6 years ago271this.sendErrorResponse(
272response,
e23d1841RedMickey6 years ago273{ format: error.message, id: 1 },
984ca036RedMickey6 years ago274undefined,
275undefined,
34472878RedMickey5 years ago276ErrorDestination.User,
984ca036RedMickey6 years ago277);
278}
2c19da7fRedMickey6 years ago279
bfcc8a29Samriel4 years ago280protected async preparePackagerBeforeAttach(
281args: IAttachRequestArgs,
282reactNativeVersions: RNPackageVersions,
283): Promise<void> {
284if (!(await this.appLauncher.getPackager().isRunning())) {
285const runOptions: ILaunchArgs = Object.assign(
286{ reactNativeVersions },
287this.appLauncher.prepareBaseRunOptions(args),
288);
289this.appLauncher.getPackager().setRunOptions(runOptions);
290await this.appLauncher.getPackager().start();
291}
292}
19df32dcRedMickey4 years ago293
294protected showError(error: Error): void {
295void vscode.window.showErrorMessage(error.message, {
296modal: true,
297});
298// We can't print error messages via debug session logger after the session is stopped. This could break the extension work.
299if (this.debugSessionStatus === DebugSessionStatus.Stopped) {
300OutputChannelLogger.getMainChannel().error(error.message);
301return;
302}
303logger.error(error.message);
304}
305
306protected async terminate(): Promise<void> {
307await vscode.commands.executeCommand(this.stopCommand, undefined, {
308sessionId: this.vsCodeDebugSession.id,
309});
310}
2c19da7fRedMickey6 years ago311}
dc94981bQuan Jin3 years ago312
313/**
314* Parses settings.json file for workspace root property
315*/
316export function getProjectRoot(args: any): string {
317const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
318const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
319try {
320const settingsContent = fs.readFileSync(settingsPath, "utf8");
321const parsedSettings = stripJsonTrailingComma(settingsContent);
322const projectRootPath =
323parsedSettings["react-native-tools.projectRoot"] ||
324parsedSettings["react-native-tools"].projectRoot;
325return path.resolve(vsCodeRoot, projectRootPath);
326} catch (e) {
327logger.verbose(
328`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`,
329);
330return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
331}
332}