microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
85bb9b097ee2563e4aba1cfa9d70e4eb8e26cc3f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

233lines · 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";
984ca036RedMickey6 years ago6import { LoggingDebugSession, Logger, logger, ErrorDestination } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago7import { DebugProtocol } from "vscode-debugprotocol";
8import { getLoggingDirectory, LogHelper } from "../extension/log/LogHelper";
9import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
10import { ErrorHelper } from "../common/error/errorHelper";
11import { InternalErrorCode } from "../common/error/internalErrorCode";
e23d1841RedMickey6 years ago12import { InternalError, NestedError } from "../common/error/internalError";
5471436aRedMickey5 years ago13import { IRunOptions, PlatformType } from "../extension/launchArgs";
2c19da7fRedMickey6 years ago14import { AppLauncher } from "../extension/appLauncher";
15import { LogLevel } from "../extension/log/LogHelper";
e23d1841RedMickey6 years ago16import * as nls from "vscode-nls";
43e6ccc3JiglioNero4 years ago17import { SettingsHelper } from "../extension/settingsHelper";
34472878RedMickey5 years ago18nls.config({
19messageFormat: nls.MessageFormat.bundle,
20bundleFormat: nls.BundleFormat.standalone,
21})();
e23d1841RedMickey6 years ago22const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago23
24/**
25* Enum of possible statuses of debug session
26*/
27export enum DebugSessionStatus {
28/** A session has been just created */
29FirstConnection,
30/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
31FirstConnectionPending,
32/** This status means that an application can be reloaded */
33ConnectionAllowed,
34/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
35ConnectionPending,
36/** A debuggee connected successfully */
37ConnectionDone,
38/** A debuggee failed to connect */
39ConnectionFailed,
40}
41
ebbd64f1RedMickey6 years ago42export interface TerminateEventArgs {
43debugSession: vscode.DebugSession;
44args: any;
45}
46
5471436aRedMickey5 years ago47export interface IAttachRequestArgs
48extends DebugProtocol.AttachRequestArguments,
49IRunOptions,
50vscode.DebugConfiguration {
259c018fYuri Skorokhodov5 years ago51webkitRangeMax: number;
52webkitRangeMin: number;
34472878RedMickey5 years ago53cwd: string /* Automatically set by VS Code to the currently opened folder */;
2c19da7fRedMickey6 years ago54port: number;
55url?: string;
6f9a0779JiglioNero5 years ago56useHermesEngine: boolean;
2c19da7fRedMickey6 years ago57address?: string;
58trace?: string;
5d47053fRedMickey6 years ago59skipFiles?: [];
1bdccb66RedMickey6 years ago60sourceMaps?: boolean;
61sourceMapPathOverrides?: { [key: string]: string };
2c19da7fRedMickey6 years ago62}
63
34472878RedMickey5 years ago64export interface ILaunchRequestArgs
65extends DebugProtocol.LaunchRequestArguments,
66IAttachRequestArgs {}
2c19da7fRedMickey6 years ago67
68export abstract class DebugSessionBase extends LoggingDebugSession {
ebbd64f1RedMickey6 years ago69protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> = new vscode.EventEmitter<TerminateEventArgs>();
34472878RedMickey5 years ago70public static readonly onDidTerminateRootDebugSession =
71DebugSessionBase.rootSessionTerminatedEventEmitter.event;
ebbd64f1RedMickey6 years ago72
a2ddbba5RedMickey5 years ago73protected readonly stopCommand: string;
ebbd64f1RedMickey6 years ago74protected readonly pwaNodeSessionName: string;
75
2c19da7fRedMickey6 years ago76protected appLauncher: AppLauncher;
77protected projectRootPath: string;
78protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
79protected previousAttachArgs: IAttachRequestArgs;
80protected cdpProxyLogLevel: LogLevel;
81protected debugSessionStatus: DebugSessionStatus;
82protected session: vscode.DebugSession;
e23d1841RedMickey6 years ago83protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago84
85constructor(session: vscode.DebugSession) {
86super();
87
ebbd64f1RedMickey6 years ago88// constants definition
89this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
a2ddbba5RedMickey5 years ago90this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
ebbd64f1RedMickey6 years ago91
92// variables definition
2c19da7fRedMickey6 years ago93this.session = session;
94this.isSettingsInitialized = false;
95this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago96this.cancellationTokenSource = new vscode.CancellationTokenSource();
97}
98
34472878RedMickey5 years ago99protected initializeRequest(
100response: DebugProtocol.InitializeResponse,
101// eslint-disable-next-line @typescript-eslint/no-unused-vars
102args: DebugProtocol.InitializeRequestArguments,
103): void {
e23d1841RedMickey6 years ago104response.body = response.body || {};
105
106response.body.supportsConfigurationDoneRequest = true;
107response.body.supportsEvaluateForHovers = true;
108response.body.supportTerminateDebuggee = true;
109response.body.supportsCancelRequest = true;
110
111this.sendResponse(response);
2c19da7fRedMickey6 years ago112}
113
34472878RedMickey5 years ago114protected abstract establishDebugSession(
115attachArgs: IAttachRequestArgs,
116resolve?: (value?: void | PromiseLike<void> | undefined) => void,
117): void;
b7451aefRedMickey6 years ago118
0d77292aJiglioNero4 years ago119protected async initializeSettings(args: any): Promise<void> {
2c19da7fRedMickey6 years ago120if (!this.isSettingsInitialized) {
121let chromeDebugCoreLogs = getLoggingDirectory();
122if (chromeDebugCoreLogs) {
123chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
124}
125let logLevel: string = args.trace;
126if (logLevel) {
127logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
128logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
34472878RedMickey5 years ago129this.cdpProxyLogLevel =
130LogLevel[logLevel] === LogLevel.Verbose ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago131} else {
132logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
34472878RedMickey5 years ago133this.cdpProxyLogLevel =
134LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago135}
136
2db9ac85Yuri Skorokhodov5 years ago137if (typeof args.sourceMaps !== "boolean") {
2c19da7fRedMickey6 years ago138args.sourceMaps = true;
139}
140
5514e287RedMickey6 years ago141if (typeof args.enableDebug !== "boolean") {
142args.enableDebug = true;
143}
144
81fc1822JiglioNero4 years ago145// Now there is a problem with processing time of 'createFromSourceMap' function of js-debug
146// So we disable this functionality by default https://github.com/microsoft/vscode-js-debug/issues/1033
147if (typeof args.sourceMapRenames !== "boolean") {
148args.sourceMapRenames = false;
149}
150
43e6ccc3JiglioNero4 years ago151const projectRootPath = SettingsHelper.getReactNativeProjectRoot(args.cwd);
0d77292aJiglioNero4 years ago152const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
153projectRootPath,
154);
155if (!isReactProject) {
156throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
157}
4dfb1c4cetatanova5 years ago158
0d77292aJiglioNero4 years ago159const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
160projectRootPath,
4dfb1c4cetatanova5 years ago161);
0d77292aJiglioNero4 years ago162this.appLauncher = appLauncher;
163this.projectRootPath = projectRootPath;
164this.isSettingsInitialized = true;
165this.appLauncher.getOrUpdateNodeModulesRoot(true);
166if (this.session.workspaceFolder) {
167this.appLauncher.updateDebugConfigurationRoot(
168this.session.workspaceFolder.uri.fsPath,
169);
170}
2c19da7fRedMickey6 years ago171}
172}
984ca036RedMickey6 years ago173
34472878RedMickey5 years ago174protected async disconnectRequest(
175response: DebugProtocol.DisconnectResponse,
176args: DebugProtocol.DisconnectArguments,
177// eslint-disable-next-line @typescript-eslint/no-unused-vars
178request?: DebugProtocol.Request,
179): Promise<void> {
a32e1e1fYuri Skorokhodov5 years ago180if (this.appLauncher) {
181await this.appLauncher.getRnCdpProxy().stopServer();
182}
e23d1841RedMickey6 years ago183
184this.cancellationTokenSource.cancel();
185this.cancellationTokenSource.dispose();
186
187// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
259c018fYuri Skorokhodov5 years ago188if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
e23d1841RedMickey6 years ago189try {
8df5011eYuri Skorokhodov5 years ago190this.appLauncher.getMobilePlatform().dispose();
e23d1841RedMickey6 years ago191} catch (err) {
34472878RedMickey5 years ago192logger.warn(
193localize(
194"CouldNotStopMonitoringLogcat",
195"Couldn't stop monitoring logcat: {0}",
196err.message || err,
197),
198);
e23d1841RedMickey6 years ago199}
200}
201
67ffa5b4RedMickey6 years ago202await logger.dispose();
203
ebbd64f1RedMickey6 years ago204DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
205debugSession: this.session,
206args: {
a2ddbba5RedMickey5 years ago207forcedStop: !!(<any>args).forcedStop,
ebbd64f1RedMickey6 years ago208},
209});
210
211this.sendResponse(response);
e23d1841RedMickey6 years ago212}
213
214protected showError(error: Error, response: DebugProtocol.Response): void {
215// We can't print error messages after the debugging session is stopped. This could break the extension work.
34472878RedMickey5 years ago216if (
217(error instanceof InternalError || error instanceof NestedError) &&
218error.errorCode === InternalErrorCode.CancellationTokenTriggered
e23d1841RedMickey6 years ago219) {
220return;
221}
222
28ceac00RedMickey4 years ago223logger.error(error.message);
224
984ca036RedMickey6 years ago225this.sendErrorResponse(
226response,
e23d1841RedMickey6 years ago227{ format: error.message, id: 1 },
984ca036RedMickey6 years ago228undefined,
229undefined,
34472878RedMickey5 years ago230ErrorDestination.User,
984ca036RedMickey6 years ago231);
232}
2c19da7fRedMickey6 years ago233}