microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

338lines · 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";
984ca036RedMickey6 years ago5import { logger } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago6import { DebugProtocol } from "vscode-debugprotocol";
09f6024fHeniker4 years ago7import * as nls from "vscode-nls";
8import { ProjectVersionHelper } from "../../common/projectVersionHelper";
9import { TelemetryHelper } from "../../common/telemetryHelper";
259c018fYuri Skorokhodov5 years ago10import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler";
19df32dcRedMickey4 years ago11import {
12DebugSessionBase,
13DebugSessionStatus,
14IAttachRequestArgs,
15ILaunchRequestArgs,
16} from "../debugSessionBase";
1bdccb66RedMickey6 years ago17import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter";
984ca036RedMickey6 years ago18import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper";
5514e287RedMickey6 years ago19import { ErrorHelper } from "../../common/error/errorHelper";
20import { InternalErrorCode } from "../../common/error/internalErrorCode";
259c018fYuri Skorokhodov5 years ago21import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler";
22import { PlatformType } from "../../extension/launchArgs";
6f9a0779JiglioNero5 years ago23import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler";
ab0238b7RedMickey4 years ago24import { TipNotificationService } from "../../extension/services/tipsNotificationsService/tipsNotificationService";
d93677adRedMickey4 years ago25import { RNSession } from "../debugSessionWrapper";
09f6024fHeniker4 years ago26import { IWDPHelper } from "./IWDPHelper";
b84470b5Ezio Li2 years ago27import { SettingsHelper } from "../../extension/settingsHelper";
09f6024fHeniker4 years ago28
34472878RedMickey5 years ago29nls.config({
30messageFormat: nls.MessageFormat.bundle,
31bundleFormat: nls.BundleFormat.standalone,
32})();
2c19da7fRedMickey6 years ago33const localize = nls.loadMessageBundle();
34
35export class DirectDebugSession extends DebugSessionBase {
984ca036RedMickey6 years ago36private debuggerEndpointHelper: DebuggerEndpointHelper;
ebbd64f1RedMickey6 years ago37private onDidTerminateDebugSessionHandler: vscode.Disposable;
19df32dcRedMickey4 years ago38private onDidStartDebugSessionHandler: vscode.Disposable;
39private appTargetConnectionClosedHandlerDescriptor?: vscode.Disposable;
40private attachSession: vscode.DebugSession | null;
259c018fYuri Skorokhodov5 years ago41private iOSWKDebugProxyHelper: IWDPHelper;
984ca036RedMickey6 years ago42
d93677adRedMickey4 years ago43constructor(rnSession: RNSession) {
44super(rnSession);
984ca036RedMickey6 years ago45this.debuggerEndpointHelper = new DebuggerEndpointHelper();
259c018fYuri Skorokhodov5 years ago46this.iOSWKDebugProxyHelper = new IWDPHelper();
19df32dcRedMickey4 years ago47this.attachSession = null;
ebbd64f1RedMickey6 years ago48
49this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago50this.handleTerminateDebugSession.bind(this),
ebbd64f1RedMickey6 years ago51);
19df32dcRedMickey4 years ago52
53this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
54this.handleStartDebugSession.bind(this),
55);
2c19da7fRedMickey6 years ago56}
57
34472878RedMickey5 years ago58protected async launchRequest(
59response: DebugProtocol.LaunchResponse,
60launchArgs: ILaunchRequestArgs,
61// eslint-disable-next-line @typescript-eslint/no-unused-vars
62request?: DebugProtocol.Request,
63): Promise<void> {
2c19da7fRedMickey6 years ago64let extProps = {
65platform: {
66value: launchArgs.platform,
67isPii: false,
68},
69isDirect: {
70value: true,
71isPii: false,
72},
73};
74
09f6024fHeniker4 years ago75void TipNotificationService.getInstance().setKnownDateForFeatureById(
f338085detatanova4 years ago76"directDebuggingWithHermes",
77);
78
0d77292aJiglioNero4 years ago79try {
80try {
81await this.initializeSettings(launchArgs);
82logger.log("Launching the application");
83logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
84
85const versions = await ProjectVersionHelper.getReactNativeVersions(
86this.projectRootPath,
87ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs),
88);
89extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
90launchArgs,
91versions,
92extProps,
93);
94
95// eslint-disable-next-line @typescript-eslint/no-unused-vars
96await TelemetryHelper.generate("launch", extProps, generator =>
97this.appLauncher.launch(launchArgs),
98);
99
100if (!launchArgs.enableDebug) {
101this.sendResponse(response);
102// if debugging is not enabled skip attach request
103return;
104}
105} catch (error) {
106throw ErrorHelper.getInternalError(
107InternalErrorCode.ApplicationLaunchFailed,
108error.message || error,
109);
110}
111// if debugging is enabled start attach request
d93677adRedMickey4 years ago112await this.vsCodeDebugSession.customRequest("attach", launchArgs);
113this.sendResponse(response);
0d77292aJiglioNero4 years ago114} catch (error) {
19df32dcRedMickey4 years ago115this.terminateWithErrorResponse(error, response);
0d77292aJiglioNero4 years ago116}
2c19da7fRedMickey6 years ago117}
118
34472878RedMickey5 years ago119protected async attachRequest(
120response: DebugProtocol.AttachResponse,
121attachArgs: IAttachRequestArgs,
122// eslint-disable-next-line @typescript-eslint/no-unused-vars
123request?: DebugProtocol.Request,
124): Promise<void> {
2c19da7fRedMickey6 years ago125let extProps = {
126platform: {
127value: attachArgs.platform,
128isPii: false,
129},
130isDirect: {
131value: true,
132isPii: false,
133},
134};
135
259c018fYuri Skorokhodov5 years ago136attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
137attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;
138
2c19da7fRedMickey6 years ago139this.previousAttachArgs = attachArgs;
140
0d77292aJiglioNero4 years ago141try {
142await this.initializeSettings(attachArgs);
accd6598Heniker4 years ago143
144const packager = this.appLauncher.getPackager();
145const args: Parameters<typeof packager.forMessage> = [
146// message indicates that another debugger has connected
147"Already connected:",
148{
149type: "client_log",
150level: "warn",
151mode: "BRIDGE",
152},
153];
154
155void packager.forMessage(...args).then(
156() => {
157this.showError(
158ErrorHelper.getInternalError(
159InternalErrorCode.AnotherDebuggerConnectedToPackager,
160),
161);
19df32dcRedMickey4 years ago162void this.terminate();
accd6598Heniker4 years ago163},
164() => {},
165);
166
0d77292aJiglioNero4 years ago167logger.log("Attaching to the application");
168logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`);
169
170const versions = await ProjectVersionHelper.getReactNativeVersions(
171this.projectRootPath,
172ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
173);
174extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
175attachArgs,
176versions,
177extProps,
178);
179
180// eslint-disable-next-line @typescript-eslint/no-unused-vars
181await TelemetryHelper.generate("attach", extProps, async generator => {
182const port = attachArgs.useHermesEngine
183? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd)
184: attachArgs.platform === PlatformType.iOS
185? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT
186: null;
187if (port === null) {
188throw ErrorHelper.getInternalError(
189InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
190attachArgs.platform,
34472878RedMickey5 years ago191);
0d77292aJiglioNero4 years ago192}
193attachArgs.port = port;
194logger.log(`Connecting to ${attachArgs.port} port`);
195await this.appLauncher.getRnCdpProxy().stopServer();
196
48ca0c62RedMickey4 years ago197const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine
0d77292aJiglioNero4 years ago198? new HermesCDPMessageHandler()
199: attachArgs.platform === PlatformType.iOS
200? new IOSDirectCDPMessageHandler()
201: null;
202
48ca0c62RedMickey4 years ago203if (!cdpMessageHandler) {
0d77292aJiglioNero4 years ago204throw ErrorHelper.getInternalError(
205InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
206attachArgs.platform,
34472878RedMickey5 years ago207);
0d77292aJiglioNero4 years ago208}
209await this.appLauncher
210.getRnCdpProxy()
dcabd9c8RedMickey4 years ago211.initializeServer(
48ca0c62RedMickey4 years ago212cdpMessageHandler,
dcabd9c8RedMickey4 years ago213this.cdpProxyLogLevel,
214this.cancellationTokenSource.token,
215);
0d77292aJiglioNero4 years ago216
217if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) {
218await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(
219attachArgs.port,
220attachArgs.webkitRangeMin,
221attachArgs.webkitRangeMax,
34472878RedMickey5 years ago222);
0d77292aJiglioNero4 years ago223const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort(
224attachArgs,
34472878RedMickey5 years ago225);
0d77292aJiglioNero4 years ago226attachArgs.port = results.targetPort;
227}
228
bfcc8a29Samriel4 years ago229if (attachArgs.request === "attach") {
230await this.preparePackagerBeforeAttach(attachArgs, versions);
231}
0d77292aJiglioNero4 years ago232
19df32dcRedMickey4 years ago233this.appTargetConnectionClosedHandlerDescriptor = this.appLauncher
234.getRnCdpProxy()
235.onApplicationTargetConnectionClosed(() => {
236if (this.attachSession) {
237if (
238this.debugSessionStatus !== DebugSessionStatus.Stopping &&
239this.debugSessionStatus !== DebugSessionStatus.Stopped
240) {
241void this.terminate();
242}
243this.appTargetConnectionClosedHandlerDescriptor?.dispose();
244}
245});
246
b84470b5Ezio Li2 years ago247const settingsPorts = SettingsHelper.getPackagerPort(attachArgs.cwd);
0d77292aJiglioNero4 years ago248const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
249`http://localhost:${attachArgs.port}`,
25090,
251this.cancellationTokenSource.token,
48ca0c62RedMickey4 years ago252attachArgs.useHermesEngine,
b84470b5Ezio Li2 years ago253settingsPorts,
0d77292aJiglioNero4 years ago254);
255this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
256await this.establishDebugSession(attachArgs);
257});
d93677adRedMickey4 years ago258this.sendResponse(response);
0d77292aJiglioNero4 years ago259} catch (error) {
19df32dcRedMickey4 years ago260this.terminateWithErrorResponse(
0d77292aJiglioNero4 years ago261ErrorHelper.getInternalError(
262InternalErrorCode.CouldNotAttachToDebugger,
263error.message || error,
264),
265response,
266);
267}
2c19da7fRedMickey6 years ago268}
269
34472878RedMickey5 years ago270protected async disconnectRequest(
271response: DebugProtocol.DisconnectResponse,
272args: DebugProtocol.DisconnectArguments,
273request?: DebugProtocol.Request,
274): Promise<void> {
19df32dcRedMickey4 years ago275this.debugSessionStatus = DebugSessionStatus.Stopping;
276
259c018fYuri Skorokhodov5 years ago277this.iOSWKDebugProxyHelper.cleanUp();
ebbd64f1RedMickey6 years ago278this.onDidTerminateDebugSessionHandler.dispose();
19df32dcRedMickey4 years ago279this.onDidStartDebugSessionHandler.dispose();
accd6598Heniker4 years ago280this.appLauncher.getPackager().closeWsConnection();
19df32dcRedMickey4 years ago281this.appTargetConnectionClosedHandlerDescriptor?.dispose();
d93677adRedMickey4 years ago282return super.disconnectRequest(response, args, request);
2c19da7fRedMickey6 years ago283}
284
0d77292aJiglioNero4 years ago285protected async establishDebugSession(attachArgs: IAttachRequestArgs): Promise<void> {
1bdccb66RedMickey6 years ago286const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
287attachArgs,
288this.appLauncher.getCdpProxyPort(),
d93677adRedMickey4 years ago289this.rnSession.sessionId,
1bdccb66RedMickey6 years ago290);
b7451aefRedMickey6 years ago291
0d77292aJiglioNero4 years ago292const childDebugSessionStarted = await vscode.debug.startDebugging(
293this.appLauncher.getWorkspaceFolder(),
294attachConfiguration,
295{
d93677adRedMickey4 years ago296parentSession: this.vsCodeDebugSession,
ebbd64f1RedMickey6 years ago297consoleMode: vscode.DebugConsoleMode.MergeWithParent,
0d77292aJiglioNero4 years ago298},
299);
300if (!childDebugSessionStarted) {
301throw new Error(
302localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"),
34472878RedMickey5 years ago303);
0d77292aJiglioNero4 years ago304}
b7451aefRedMickey6 years ago305}
ebbd64f1RedMickey6 years ago306
0d77292aJiglioNero4 years ago307private handleTerminateDebugSession(debugSession: vscode.DebugSession): void {
ebbd64f1RedMickey6 years ago308if (
d93677adRedMickey4 years ago309debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
34472878RedMickey5 years ago310debugSession.type === this.pwaNodeSessionName
ebbd64f1RedMickey6 years ago311) {
19df32dcRedMickey4 years ago312void this.terminate();
313}
314}
315
316private handleStartDebugSession(debugSession: vscode.DebugSession): void {
317if (
318this.nodeSession &&
319(debugSession as any).parentSession &&
320this.nodeSession.id === (debugSession as any).parentSession.id
321) {
322this.attachSession = debugSession;
323}
324if (
325debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
326debugSession.type === this.pwaNodeSessionName
327) {
328this.nodeSession = debugSession;
ebbd64f1RedMickey6 years ago329}
330}
6f9a0779JiglioNero5 years ago331
332protected async initializeSettings(args: any): Promise<any> {
333await super.initializeSettings(args);
334if (args.useHermesEngine === undefined) {
335args.useHermesEngine = true;
336}
337}
2c19da7fRedMickey6 years ago338}