microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
smoke-actionbar-use-packager-helper

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debugSessionBase.ts

335lines · 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";
2a7f3de7ConnorQi013 months ago21import { DeviceStatusIndicator } from "../extension/deviceStatusIndicator";
d93677adRedMickey4 years ago22import { RNSession } from "./debugSessionWrapper";
bfcc8a29Samriel4 years ago23
34472878RedMickey5 years ago24nls.config({
25messageFormat: nls.MessageFormat.bundle,
26bundleFormat: nls.BundleFormat.standalone,
27})();
e23d1841RedMickey6 years ago28const localize = nls.loadMessageBundle();
2c19da7fRedMickey6 years ago29
30/**
31* Enum of possible statuses of debug session
32*/
33export enum DebugSessionStatus {
34/** A session has been just created */
35FirstConnection,
36/** This status is required in order to exclude the possible creation of several debug sessions at the first start */
37FirstConnectionPending,
38/** This status means that an application can be reloaded */
39ConnectionAllowed,
40/** This status means that an application is reloading now, and we shouldn't terminate the current debug session */
41ConnectionPending,
42/** A debuggee connected successfully */
43ConnectionDone,
44/** A debuggee failed to connect */
45ConnectionFailed,
19df32dcRedMickey4 years ago46/** The session is handling disconnect request now */
47Stopping,
48/** The session is stopped */
49Stopped,
2c19da7fRedMickey6 years ago50}
51
ebbd64f1RedMickey6 years ago52export interface TerminateEventArgs {
53debugSession: vscode.DebugSession;
54args: any;
55}
56
5471436aRedMickey5 years ago57export interface IAttachRequestArgs
58extends DebugProtocol.AttachRequestArguments,
59IRunOptions,
60vscode.DebugConfiguration {
259c018fYuri Skorokhodov5 years ago61webkitRangeMax: number;
62webkitRangeMin: number;
34472878RedMickey5 years ago63cwd: string /* Automatically set by VS Code to the currently opened folder */;
2c19da7fRedMickey6 years ago64port: number;
65url?: string;
6f9a0779JiglioNero5 years ago66useHermesEngine: boolean;
2c19da7fRedMickey6 years ago67address?: string;
68trace?: string;
5d47053fRedMickey6 years ago69skipFiles?: [];
1bdccb66RedMickey6 years ago70sourceMaps?: boolean;
71sourceMapPathOverrides?: { [key: string]: string };
2d89fb47Ezio Li3 years ago72jsDebugTrace?: boolean;
9f8c460dEzio Li2 years ago73browserTarget?: string;
2c19da7fRedMickey6 years ago74}
75
34472878RedMickey5 years ago76export interface ILaunchRequestArgs
77extends DebugProtocol.LaunchRequestArguments,
78IAttachRequestArgs {}
2c19da7fRedMickey6 years ago79
80export abstract class DebugSessionBase extends LoggingDebugSession {
09f6024fHeniker4 years ago81protected static rootSessionTerminatedEventEmitter: vscode.EventEmitter<TerminateEventArgs> =
82new vscode.EventEmitter<TerminateEventArgs>();
34472878RedMickey5 years ago83public static readonly onDidTerminateRootDebugSession =
84DebugSessionBase.rootSessionTerminatedEventEmitter.event;
ebbd64f1RedMickey6 years ago85
a2ddbba5RedMickey5 years ago86protected readonly stopCommand: string;
19df32dcRedMickey4 years ago87protected readonly terminateCommand: string;
ebbd64f1RedMickey6 years ago88protected readonly pwaNodeSessionName: string;
89
176f99c8ConnorQi013 months ago90protected appLauncher!: AppLauncher;
91protected projectRootPath!: string;
2c19da7fRedMickey6 years ago92protected isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
176f99c8ConnorQi013 months ago93protected previousAttachArgs!: IAttachRequestArgs;
94protected cdpProxyLogLevel!: LogLevel;
2c19da7fRedMickey6 years ago95protected debugSessionStatus: DebugSessionStatus;
19df32dcRedMickey4 years ago96protected nodeSession: vscode.DebugSession | null;
d93677adRedMickey4 years ago97protected rnSession: RNSession;
98protected vsCodeDebugSession: vscode.DebugSession;
e23d1841RedMickey6 years ago99protected cancellationTokenSource: vscode.CancellationTokenSource;
2c19da7fRedMickey6 years ago100
d93677adRedMickey4 years ago101constructor(rnSession: RNSession) {
2c19da7fRedMickey6 years ago102super();
103
ebbd64f1RedMickey6 years ago104// constants definition
105this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension
a2ddbba5RedMickey5 years ago106this.stopCommand = "workbench.action.debug.stop"; // the command which simulates a click on the "Stop" button
19df32dcRedMickey4 years ago107this.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 ago108
109// variables definition
d93677adRedMickey4 years ago110this.rnSession = rnSession;
111this.vsCodeDebugSession = rnSession.vsCodeDebugSession;
2c19da7fRedMickey6 years ago112this.isSettingsInitialized = false;
113this.debugSessionStatus = DebugSessionStatus.FirstConnection;
e23d1841RedMickey6 years ago114this.cancellationTokenSource = new vscode.CancellationTokenSource();
19df32dcRedMickey4 years ago115this.nodeSession = null;
e23d1841RedMickey6 years ago116}
117
34472878RedMickey5 years ago118protected initializeRequest(
119response: DebugProtocol.InitializeResponse,
120// eslint-disable-next-line @typescript-eslint/no-unused-vars
121args: DebugProtocol.InitializeRequestArguments,
122): void {
e23d1841RedMickey6 years ago123response.body = response.body || {};
124
125response.body.supportsConfigurationDoneRequest = true;
126response.body.supportsEvaluateForHovers = true;
127response.body.supportTerminateDebuggee = true;
128response.body.supportsCancelRequest = true;
129
1bb1a217EzioLi2 years ago130response.body.exceptionBreakpointFilters = [
131{
132filter: "all",
133label: "Caught Exceptions",
134default: false,
135supportsCondition: true,
136description: "Breaks on all throw errors, even if they're caught later.",
137// eslint-disable-next-line @typescript-eslint/quotes
138conditionDescription: 'error.name == "MyError"',
139},
140{
141filter: "uncaught",
142label: "Uncaught Exceptions",
143default: false,
144supportsCondition: true,
145description: "Breaks only on errors or promise rejections that are not handled.",
146// eslint-disable-next-line @typescript-eslint/quotes
147conditionDescription: 'error.name == "MyError"',
148},
149];
150
e23d1841RedMickey6 years ago151this.sendResponse(response);
2c19da7fRedMickey6 years ago152}
153
34472878RedMickey5 years ago154protected abstract establishDebugSession(
155attachArgs: IAttachRequestArgs,
156resolve?: (value?: void | PromiseLike<void> | undefined) => void,
157): void;
b7451aefRedMickey6 years ago158
0d77292aJiglioNero4 years ago159protected async initializeSettings(args: any): Promise<void> {
2c19da7fRedMickey6 years ago160if (!this.isSettingsInitialized) {
161let chromeDebugCoreLogs = getLoggingDirectory();
162if (chromeDebugCoreLogs) {
163chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "DebugSessionLogs.txt");
164}
165let logLevel: string = args.trace;
166if (logLevel) {
167logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
5b09cf97Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago168logger.setup((Logger.LogLevel as any)[logLevel], chromeDebugCoreLogs || false);
34472878RedMickey5 years ago169this.cdpProxyLogLevel =
5b09cf97Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago170(LogLevel as any)[logLevel] === LogLevel.Verbose
171? LogLevel.Custom
172: LogLevel.None;
2c19da7fRedMickey6 years ago173} else {
174logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
34472878RedMickey5 years ago175this.cdpProxyLogLevel =
176LogHelper.LOG_LEVEL === LogLevel.Trace ? LogLevel.Custom : LogLevel.None;
2c19da7fRedMickey6 years ago177}
178
2db9ac85Yuri Skorokhodov5 years ago179if (typeof args.sourceMaps !== "boolean") {
2c19da7fRedMickey6 years ago180args.sourceMaps = true;
181}
182
5514e287RedMickey6 years ago183if (typeof args.enableDebug !== "boolean") {
184args.enableDebug = true;
185}
186
81fc1822JiglioNero4 years ago187// Now there is a problem with processing time of 'createFromSourceMap' function of js-debug
188// So we disable this functionality by default https://github.com/microsoft/vscode-js-debug/issues/1033
189if (typeof args.sourceMapRenames !== "boolean") {
190args.sourceMapRenames = false;
191}
192
43e6ccc3JiglioNero4 years ago193const projectRootPath = SettingsHelper.getReactNativeProjectRoot(args.cwd);
0d77292aJiglioNero4 years ago194const isReactProject = await ReactNativeProjectHelper.isReactNativeProject(
195projectRootPath,
196);
197if (!isReactProject) {
198throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
199}
4dfb1c4cetatanova5 years ago200
0d77292aJiglioNero4 years ago201const appLauncher = await AppLauncher.getOrCreateAppLauncherByProjectRootPath(
202projectRootPath,
4dfb1c4cetatanova5 years ago203);
0d77292aJiglioNero4 years ago204this.appLauncher = appLauncher;
205this.projectRootPath = projectRootPath;
206this.isSettingsInitialized = true;
207this.appLauncher.getOrUpdateNodeModulesRoot(true);
d93677adRedMickey4 years ago208if (this.vsCodeDebugSession.workspaceFolder) {
0d77292aJiglioNero4 years ago209this.appLauncher.updateDebugConfigurationRoot(
d93677adRedMickey4 years ago210this.vsCodeDebugSession.workspaceFolder.uri.fsPath,
0d77292aJiglioNero4 years ago211);
212}
b84470b5Ezio Li2 years ago213const settingsPort = this.appLauncher.getPackagerPort(projectRootPath);
214if (this.appLauncher.getPackager().getPort() != settingsPort) {
215this.appLauncher.getPackager().resetToSettingsPort();
216}
2c19da7fRedMickey6 years ago217}
218}
984ca036RedMickey6 years ago219
34472878RedMickey5 years ago220protected async disconnectRequest(
221response: DebugProtocol.DisconnectResponse,
222args: DebugProtocol.DisconnectArguments,
223// eslint-disable-next-line @typescript-eslint/no-unused-vars
224request?: DebugProtocol.Request,
225): Promise<void> {
a32e1e1fYuri Skorokhodov5 years ago226if (this.appLauncher) {
227await this.appLauncher.getRnCdpProxy().stopServer();
228}
e23d1841RedMickey6 years ago229
2a7f3de7ConnorQi013 months ago230DeviceStatusIndicator.hide();
231
e23d1841RedMickey6 years ago232this.cancellationTokenSource.cancel();
233this.cancellationTokenSource.dispose();
234
235// Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
259c018fYuri Skorokhodov5 years ago236if (this.previousAttachArgs && this.previousAttachArgs.platform === PlatformType.Android) {
e23d1841RedMickey6 years ago237try {
8df5011eYuri Skorokhodov5 years ago238this.appLauncher.getMobilePlatform().dispose();
e23d1841RedMickey6 years ago239} catch (err) {
34472878RedMickey5 years ago240logger.warn(
241localize(
242"CouldNotStopMonitoringLogcat",
243"Couldn't stop monitoring logcat: {0}",
fff0cefdConnorQi013 months ago244err instanceof Error ? err.message : String(err),
34472878RedMickey5 years ago245),
246);
e23d1841RedMickey6 years ago247}
248}
249
19df32dcRedMickey4 years ago250this.debugSessionStatus = DebugSessionStatus.Stopped;
67ffa5b4RedMickey6 years ago251await logger.dispose();
252
ebbd64f1RedMickey6 years ago253DebugSessionBase.rootSessionTerminatedEventEmitter.fire({
d93677adRedMickey4 years ago254debugSession: this.vsCodeDebugSession,
ebbd64f1RedMickey6 years ago255args: {
a2ddbba5RedMickey5 years ago256forcedStop: !!(<any>args).forcedStop,
ebbd64f1RedMickey6 years ago257},
258});
259
260this.sendResponse(response);
e23d1841RedMickey6 years ago261}
262
19df32dcRedMickey4 years ago263protected terminateWithErrorResponse(error: Error, response: DebugProtocol.Response): void {
e23d1841RedMickey6 years ago264// We can't print error messages after the debugging session is stopped. This could break the extension work.
34472878RedMickey5 years ago265if (
266(error instanceof InternalError || error instanceof NestedError) &&
267error.errorCode === InternalErrorCode.CancellationTokenTriggered
e23d1841RedMickey6 years ago268) {
269return;
270}
271
28ceac00RedMickey4 years ago272logger.error(error.message);
273
984ca036RedMickey6 years ago274this.sendErrorResponse(
275response,
e23d1841RedMickey6 years ago276{ format: error.message, id: 1 },
984ca036RedMickey6 years ago277undefined,
278undefined,
34472878RedMickey5 years ago279ErrorDestination.User,
984ca036RedMickey6 years ago280);
281}
2c19da7fRedMickey6 years ago282
bfcc8a29Samriel4 years ago283protected async preparePackagerBeforeAttach(
284args: IAttachRequestArgs,
285reactNativeVersions: RNPackageVersions,
286): Promise<void> {
287if (!(await this.appLauncher.getPackager().isRunning())) {
288const runOptions: ILaunchArgs = Object.assign(
289{ reactNativeVersions },
290this.appLauncher.prepareBaseRunOptions(args),
291);
292this.appLauncher.getPackager().setRunOptions(runOptions);
293await this.appLauncher.getPackager().start();
294}
295}
19df32dcRedMickey4 years ago296
297protected showError(error: Error): void {
298void vscode.window.showErrorMessage(error.message, {
299modal: true,
300});
301// We can't print error messages via debug session logger after the session is stopped. This could break the extension work.
302if (this.debugSessionStatus === DebugSessionStatus.Stopped) {
303OutputChannelLogger.getMainChannel().error(error.message);
304return;
305}
306logger.error(error.message);
307}
308
309protected async terminate(): Promise<void> {
310await vscode.commands.executeCommand(this.stopCommand, undefined, {
311sessionId: this.vsCodeDebugSession.id,
312});
313}
2c19da7fRedMickey6 years ago314}
dc94981bQuan Jin3 years ago315
316/**
317* Parses settings.json file for workspace root property
318*/
319export function getProjectRoot(args: any): string {
320const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
321const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json");
322try {
323const settingsContent = fs.readFileSync(settingsPath, "utf8");
324const parsedSettings = stripJsonTrailingComma(settingsContent);
325const projectRootPath =
326parsedSettings["react-native-tools.projectRoot"] ||
327parsedSettings["react-native-tools"].projectRoot;
328return path.resolve(vsCodeRoot, projectRootPath);
329} catch (e) {
330logger.verbose(
331`${settingsPath} file doesn't exist or its content is incorrect. This file will be ignored.`,
332);
333return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../..");
334}
335}