microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a03236a7c2c0f067819adb583d3ad0ab4b5784f7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

256lines · 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 vscode from "vscode";
5import * as path from "path";
6import * as fs from "fs";
7import stripJsonComments = require("strip-json-comments");
984ca036RedMickey6 years ago8import { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago9import { 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";
e23d1841RedMickey6 years ago14import { InternalError, NestedError } from "../common/error/internalError";
5471436aRedMickey5 years ago15import { IRunOptions, PlatformType } from "../extension/launchArgs";
2c19da7fRedMickey6 years ago16import { AppLauncher } from "../extension/appLauncher";
17import { LogLevel } from "../extension/log/LogHelper";
e23d1841RedMickey6 years ago18import * as nls from "vscode-nls";
34472878RedMickey5 years ago19nls.config({
20messageFormat: nls.MessageFormat.bundle,
21bundleFormat: nls.BundleFormat.standalone,
22})();
e23d1841RedMickey6 years ago23const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago24
25/**
26* Enum of possible statuses of debug session
27*/
28export enum DebugSessionStatus {
29/** A session has been just created */
30FirstConnection,
31/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
32FirstConnectionPending,
33/** This status means that an application can be reloaded */
34ConnectionAllowed,
35/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
36ConnectionPending,
37/** A debuggee connected successfully */
38ConnectionDone,
39/** A debuggee failed to connect */
40ConnectionFailed,
41}
42
ebbd64f1RedMickey6 years ago43export interface TerminateEventArgs {
44debugSession: vscode.DebugSession;
45args: any;
46}
47
5471436aRedMickey5 years ago48export interface IAttachRequestArgs
49extends DebugProtocol.AttachRequestArguments,
50IRunOptions,
51vscode.DebugConfiguration {
259c018fYuri Skorokhodov5 years ago52webkitRangeMax: number;
53webkitRangeMin: number;
34472878RedMickey5 years ago54cwd: string /* Automatically set by VS Code to the currently opened folder */;
2c19da7fRedMickey6 years ago55port: number;
56url?: string;
6f9a0779JiglioNero5 years ago57useHermesEngine: boolean;
2c19da7fRedMickey6 years ago58address?: string;
59trace?: string;
5d47053fRedMickey6 years ago60skipFiles?: [];
1bdccb66RedMickey6 years ago61sourceMaps?: boolean;
62sourceMapPathOverrides?: { [key: string]: string };
2c19da7fRedMickey6 years ago63}
64
34472878RedMickey5 years ago65export interface ILaunchRequestArgs
66extends DebugProtocol.LaunchRequestArguments,
67IAttachRequestArgs {}
2c19da7fRedMickey6 years ago68
69export abstract class DebugSessionBase extends LoggingDebugSession {
ebbd64f1RedMickey6 years ago70protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
34472878RedMickey5 years ago71public static readonly onDidTerminateRootDebugSession =
72DebugSessionBase.rootSessionTerminatedEventEmitter.event;
ebbd64f1RedMickey6 years ago73
a2ddbba5RedMickey5 years ago74protected readonly stopCommand: string;
ebbd64f1RedMickey6 years ago75protected readonly pwaNodeSessionName: string;
76
2c19da7fRedMickey6 years ago77protected appLauncher: AppLauncher;
78protected projectRootPath: string;
79protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
80protected previousAttachArgs: IAttachRequestArgs;
81protected cdpProxyLogLevel: LogLevel;
82protected debugSessionStatus: DebugSessionStatus;
83protected session: vscode.DebugSession;
e23d1841RedMickey6 years ago84protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago85
86constructor(session: vscode.DebugSession) {
87super();
88
ebbd64f1RedMickey6 years ago89// constants definition
90this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
a2ddbba5RedMickey5 years ago91this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
ebbd64f1RedMickey6 years ago92
93// variables definition
2c19da7fRedMickey6 years ago94this.session = session;
95this.isSettingsInitialized = false;
96this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago97this.cancellationTokenSource = new vscode.CancellationTokenSource();
98}
99
34472878RedMickey5 years ago100protected initializeRequest(
101response: DebugProtocol.InitializeResponse,
102// eslint-disable-next-line @typescript-eslint/no-unused-vars
103args: DebugProtocol.InitializeRequestArguments,
104): void {
e23d1841RedMickey6 years ago105response.body = response.body || {};
106
107response.body.supportsConfigurationDoneRequest = true;
108response.body.supportsEvaluateForHovers = true;
109response.body.supportTerminateDebuggee = true;
110response.body.supportsCancelRequest = true;
111
112this.sendResponse(response);
2c19da7fRedMickey6 years ago113}
114
34472878RedMickey5 years ago115protected abstract establishDebugSession(
116attachArgs: IAttachRequestArgs,
117resolve?: (value?: void | PromiseLike<void> | undefined) => void,
118): void;
b7451aefRedMickey6 years ago119
0d77292aJiglioNero4 years ago120protected async initializeSettings(args: any): Promise<void> {
2c19da7fRedMickey6 years ago121if (!this.isSettingsInitialized) {
122let chromeDebugCoreLogs = getLoggingDirectory();
123if (chromeDebugCoreLogs) {
124chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
125}
126let logLevel: string = args.trace;
127if (logLevel) {
128logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
129logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
34472878RedMickey5 years ago130this.cdpProxyLogLevel =
131LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago132} else {
133logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
34472878RedMickey5 years ago134this.cdpProxyLogLevel =
135LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago136}
137
2db9ac85Yuri Skorokhodov5 years ago138if (typeof args.sourceMaps !== "boolean") {
2c19da7fRedMickey6 years ago139args.sourceMaps = true;
140}
141
5514e287RedMickey6 years ago142if (typeof args.enableDebug !== "boolean") {
143args.enableDebug = true;
144}
145
81fc1822JiglioNero4 years ago146// 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
148if (typeof args.sourceMapRenames !== "boolean") {
149args.sourceMapRenames = false;
150}
151
2c19da7fRedMickey6 years ago152const projectRootPath = getProjectRoot(args);
0d77292aJiglioNero4 years ago153const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
154projectRootPath,
155);
156if (!isReactProject) {
157throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
158}
4dfb1c4cetatanova5 years ago159
0d77292aJiglioNero4 years ago160const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
161projectRootPath,
4dfb1c4cetatanova5 years ago162);
0d77292aJiglioNero4 years ago163this.appLauncher = appLauncher;
164this.projectRootPath = projectRootPath;
165this.isSettingsInitialized = true;
166this.appLauncher.getOrUpdateNodeModulesRoot(true);
167if (this.session.workspaceFolder) {
168this.appLauncher.updateDebugConfigurationRoot(
169this.session.workspaceFolder.uri.fsPath,
170);
171}
2c19da7fRedMickey6 years ago172}
173}
984ca036RedMickey6 years ago174
34472878RedMickey5 years ago175protected async disconnectRequest(
176response: DebugProtocol.DisconnectResponse,
177args: DebugProtocol.DisconnectArguments,
178// eslint-disable-next-line @typescript-eslint/no-unused-vars
179request?: DebugProtocol.Request,
180): Promise<void> {
a32e1e1fYuri Skorokhodov5 years ago181if (this.appLauncher) {
182await this.appLauncher.getRnCdpProxy().stopServer();
183}
e23d1841RedMickey6 years ago184
185this.cancellationTokenSource.cancel();
186this.cancellationTokenSource.dispose();
187
188// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
259c018fYuri Skorokhodov5 years ago189if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
e23d1841RedMickey6 years ago190try {
8df5011eYuri Skorokhodov5 years ago191this.appLauncher.getMobilePlatform().dispose();
e23d1841RedMickey6 years ago192} catch (err) {
34472878RedMickey5 years ago193logger.warn(
194localize(
195"CouldNotStopMonitoringLogcat",
196"Couldn't stop monitoring logcat: {0}",
197err.message || err,
198),
199);
e23d1841RedMickey6 years ago200}
201}
202
67ffa5b4RedMickey6 years ago203await logger.dispose();
204
ebbd64f1RedMickey6 years ago205DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
206debugSession: this.session,
207args: {
a2ddbba5RedMickey5 years ago208forcedStop: !!(<any>args).forcedStop,
ebbd64f1RedMickey6 years ago209},
210});
211
212this.sendResponse(response);
e23d1841RedMickey6 years ago213}
214
215protected showError(error: Error, response: DebugProtocol.Response): void {
216// We can't print error messages after the debugging session is stopped. This could break the extension work.
34472878RedMickey5 years ago217if (
218(error instanceof InternalError || error instanceof NestedError) &&
219error.errorCode === InternalErrorCode.CancellationTokenTriggered
e23d1841RedMickey6 years ago220) {
221return;
222}
223
28ceac00RedMickey4 years ago224logger.error(error.message);
225
984ca036RedMickey6 years ago226this.sendErrorResponse(
227response,
e23d1841RedMickey6 years ago228{ format: error.message, id: 1 },
984ca036RedMickey6 years ago229undefined,
230undefined,
34472878RedMickey5 years ago231ErrorDestination.User,
984ca036RedMickey6 years ago232);
233}
2c19da7fRedMickey6 years ago234}
235
236/**
237* Parses settings.json file for workspace root property
238*/
239export function getProjectRoot(args: any): string {
240const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
241const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
242try {
243let settingsContent = fs.readFileSync(settingsPath, "utf8");
244settingsContent = stripJsonComments(settingsContent);
245let parsedSettings = JSON.parse(settingsContent);
34472878RedMickey5 years ago246let projectRootPath =
247parsedSettings["react-native-tools.projectRoot"] ||
248parsedSettings["react-native-tools"].projectRoot;
2c19da7fRedMickey6 years ago249return path.resolve(vsCodeRoot, projectRootPath);
250} catch (e) {
34472878RedMickey5 years ago251logger.verbose(
252`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`,
253);
2c19da7fRedMickey6 years ago254return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
255}
256}