microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.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 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";
27
34472878RedMickey5 years ago28nls.config({
29messageFormat: nls.MessageFormat.bundle,
30bundleFormat: nls.BundleFormat.standalone,
31})();
2c19da7fRedMickey6 years ago32const localize = nls.loadMessageBundle();
33
34export class DirectDebugSession extends DebugSessionBase {
984ca036RedMickey6 years ago35private debuggerEndpointHelper: DebuggerEndpointHelper;
ebbd64f1RedMickey6 years ago36private onDidTerminateDebugSessionHandler: vscode.Disposable;
19df32dcRedMickey4 years ago37private onDidStartDebugSessionHandler: vscode.Disposable;
38private appTargetConnectionClosedHandlerDescriptor?: vscode.Disposable;
39private attachSession: vscode.DebugSession | null;
259c018fYuri Skorokhodov5 years ago40private iOSWKDebugProxyHelper: IWDPHelper;
984ca036RedMickey6 years ago41
d93677adRedMickey4 years ago42constructor(rnSession: RNSession) {
43super(rnSession);
984ca036RedMickey6 years ago44this.debuggerEndpointHelper = new DebuggerEndpointHelper();
259c018fYuri Skorokhodov5 years ago45this.iOSWKDebugProxyHelper = new IWDPHelper();
19df32dcRedMickey4 years ago46this.attachSession = null;
ebbd64f1RedMickey6 years ago47
48this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago49this.handleTerminateDebugSession.bind(this),
ebbd64f1RedMickey6 years ago50);
19df32dcRedMickey4 years ago51
52this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
53this.handleStartDebugSession.bind(this),
54);
2c19da7fRedMickey6 years ago55}
56
34472878RedMickey5 years ago57protected async launchRequest(
58response: DebugProtocol.LaunchResponse,
59launchArgs: ILaunchRequestArgs,
60// eslint-disable-next-line @typescript-eslint/no-unused-vars
61request?: DebugProtocol.Request,
62): Promise<void> {
2c19da7fRedMickey6 years ago63let extProps = {
64platform: {
65value: launchArgs.platform,
66isPii: false,
67},
68isDirect: {
69value: true,
70isPii: false,
71},
72};
73
09f6024fHeniker4 years ago74void TipNotificationService.getInstance().setKnownDateForFeatureById(
f338085detatanova4 years ago75"directDebuggingWithHermes",
76);
77
0d77292aJiglioNero4 years ago78try {
79try {
80await this.initializeSettings(launchArgs);
81logger.log("Launching the application");
82logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
83
84const versions = await ProjectVersionHelper.getReactNativeVersions(
85this.projectRootPath,
86ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs),
87);
88extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
89launchArgs,
90versions,
91extProps,
92);
93
94// eslint-disable-next-line @typescript-eslint/no-unused-vars
95await TelemetryHelper.generate("launch", extProps, generator =>
96this.appLauncher.launch(launchArgs),
97);
98
99if (!launchArgs.enableDebug) {
100this.sendResponse(response);
101// if debugging is not enabled skip attach request
102return;
103}
104} catch (error) {
105throw ErrorHelper.getInternalError(
106InternalErrorCode.ApplicationLaunchFailed,
107error.message || error,
108);
109}
110// if debugging is enabled start attach request
d93677adRedMickey4 years ago111await this.vsCodeDebugSession.customRequest("attach", launchArgs);
112this.sendResponse(response);
0d77292aJiglioNero4 years ago113} catch (error) {
19df32dcRedMickey4 years ago114this.terminateWithErrorResponse(error, response);
0d77292aJiglioNero4 years ago115}
2c19da7fRedMickey6 years ago116}
117
34472878RedMickey5 years ago118protected async attachRequest(
119response: DebugProtocol.AttachResponse,
120attachArgs: IAttachRequestArgs,
121// eslint-disable-next-line @typescript-eslint/no-unused-vars
122request?: DebugProtocol.Request,
123): Promise<void> {
2c19da7fRedMickey6 years ago124let extProps = {
125platform: {
126value: attachArgs.platform,
127isPii: false,
128},
129isDirect: {
130value: true,
131isPii: false,
132},
133};
134
259c018fYuri Skorokhodov5 years ago135attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
136attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;
137
2c19da7fRedMickey6 years ago138this.previousAttachArgs = attachArgs;
139
0d77292aJiglioNero4 years ago140try {
141await this.initializeSettings(attachArgs);
accd6598Heniker4 years ago142
143const packager = this.appLauncher.getPackager();
144const args: Parameters<typeof packager.forMessage> = [
145// message indicates that another debugger has connected
146"Already connected:",
147{
148type: "client_log",
149level: "warn",
150mode: "BRIDGE",
151},
152];
153
154void packager.forMessage(...args).then(
155() => {
156this.showError(
157ErrorHelper.getInternalError(
158InternalErrorCode.AnotherDebuggerConnectedToPackager,
159),
160);
19df32dcRedMickey4 years ago161void this.terminate();
accd6598Heniker4 years ago162},
163() => {},
164);
165
0d77292aJiglioNero4 years ago166logger.log("Attaching to the application");
167logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`);
168
169const versions = await ProjectVersionHelper.getReactNativeVersions(
170this.projectRootPath,
171ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
172);
173extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
174attachArgs,
175versions,
176extProps,
177);
178
179// eslint-disable-next-line @typescript-eslint/no-unused-vars
180await TelemetryHelper.generate("attach", extProps, async generator => {
181const port = attachArgs.useHermesEngine
182? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd)
183: attachArgs.platform === PlatformType.iOS
184? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT
185: null;
186if (port === null) {
187throw ErrorHelper.getInternalError(
188InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
189attachArgs.platform,
34472878RedMickey5 years ago190);
0d77292aJiglioNero4 years ago191}
192attachArgs.port = port;
193logger.log(`Connecting to ${attachArgs.port} port`);
194await this.appLauncher.getRnCdpProxy().stopServer();
195
48ca0c62RedMickey4 years ago196const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine
0d77292aJiglioNero4 years ago197? new HermesCDPMessageHandler()
198: attachArgs.platform === PlatformType.iOS
199? new IOSDirectCDPMessageHandler()
200: null;
201
48ca0c62RedMickey4 years ago202if (!cdpMessageHandler) {
0d77292aJiglioNero4 years ago203throw ErrorHelper.getInternalError(
204InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
205attachArgs.platform,
34472878RedMickey5 years ago206);
0d77292aJiglioNero4 years ago207}
208await this.appLauncher
209.getRnCdpProxy()
dcabd9c8RedMickey4 years ago210.initializeServer(
48ca0c62RedMickey4 years ago211cdpMessageHandler,
dcabd9c8RedMickey4 years ago212this.cdpProxyLogLevel,
213this.cancellationTokenSource.token,
214);
0d77292aJiglioNero4 years ago215
216if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) {
217await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(
218attachArgs.port,
219attachArgs.webkitRangeMin,
220attachArgs.webkitRangeMax,
34472878RedMickey5 years ago221);
0d77292aJiglioNero4 years ago222const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort(
223attachArgs,
34472878RedMickey5 years ago224);
0d77292aJiglioNero4 years ago225attachArgs.port = results.targetPort;
226}
227
bfcc8a29Samriel4 years ago228if (attachArgs.request === "attach") {
229await this.preparePackagerBeforeAttach(attachArgs, versions);
230}
0d77292aJiglioNero4 years ago231
19df32dcRedMickey4 years ago232this.appTargetConnectionClosedHandlerDescriptor = this.appLauncher
233.getRnCdpProxy()
234.onApplicationTargetConnectionClosed(() => {
235if (this.attachSession) {
236if (
237this.debugSessionStatus !== DebugSessionStatus.Stopping &&
238this.debugSessionStatus !== DebugSessionStatus.Stopped
239) {
240void this.terminate();
241}
242this.appTargetConnectionClosedHandlerDescriptor?.dispose();
243}
244});
245
0d77292aJiglioNero4 years ago246const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
247`http://localhost:${attachArgs.port}`,
24890,
249this.cancellationTokenSource.token,
48ca0c62RedMickey4 years ago250attachArgs.useHermesEngine,
0d77292aJiglioNero4 years ago251);
252this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
253await this.establishDebugSession(attachArgs);
254});
d93677adRedMickey4 years ago255this.sendResponse(response);
0d77292aJiglioNero4 years ago256} catch (error) {
19df32dcRedMickey4 years ago257this.terminateWithErrorResponse(
0d77292aJiglioNero4 years ago258ErrorHelper.getInternalError(
259InternalErrorCode.CouldNotAttachToDebugger,
260error.message || error,
261),
262response,
263);
264}
2c19da7fRedMickey6 years ago265}
266
34472878RedMickey5 years ago267protected async disconnectRequest(
268response: DebugProtocol.DisconnectResponse,
269args: DebugProtocol.DisconnectArguments,
270request?: DebugProtocol.Request,
271): Promise<void> {
19df32dcRedMickey4 years ago272this.debugSessionStatus = DebugSessionStatus.Stopping;
273
259c018fYuri Skorokhodov5 years ago274this.iOSWKDebugProxyHelper.cleanUp();
ebbd64f1RedMickey6 years ago275this.onDidTerminateDebugSessionHandler.dispose();
19df32dcRedMickey4 years ago276this.onDidStartDebugSessionHandler.dispose();
accd6598Heniker4 years ago277this.appLauncher.getPackager().closeWsConnection();
19df32dcRedMickey4 years ago278this.appTargetConnectionClosedHandlerDescriptor?.dispose();
d93677adRedMickey4 years ago279return super.disconnectRequest(response, args, request);
2c19da7fRedMickey6 years ago280}
281
0d77292aJiglioNero4 years ago282protected async establishDebugSession(attachArgs: IAttachRequestArgs): Promise<void> {
1bdccb66RedMickey6 years ago283const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
284attachArgs,
285this.appLauncher.getCdpProxyPort(),
d93677adRedMickey4 years ago286this.rnSession.sessionId,
1bdccb66RedMickey6 years ago287);
b7451aefRedMickey6 years ago288
0d77292aJiglioNero4 years ago289const childDebugSessionStarted = await vscode.debug.startDebugging(
290this.appLauncher.getWorkspaceFolder(),
291attachConfiguration,
292{
d93677adRedMickey4 years ago293parentSession: this.vsCodeDebugSession,
ebbd64f1RedMickey6 years ago294consoleMode: vscode.DebugConsoleMode.MergeWithParent,
0d77292aJiglioNero4 years ago295},
296);
297if (!childDebugSessionStarted) {
298throw new Error(
299localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"),
34472878RedMickey5 years ago300);
0d77292aJiglioNero4 years ago301}
b7451aefRedMickey6 years ago302}
ebbd64f1RedMickey6 years ago303
0d77292aJiglioNero4 years ago304private handleTerminateDebugSession(debugSession: vscode.DebugSession): void {
ebbd64f1RedMickey6 years ago305if (
d93677adRedMickey4 years ago306debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
34472878RedMickey5 years ago307debugSession.type === this.pwaNodeSessionName
ebbd64f1RedMickey6 years ago308) {
19df32dcRedMickey4 years ago309void this.terminate();
310}
311}
312
313private handleStartDebugSession(debugSession: vscode.DebugSession): void {
314if (
315this.nodeSession &&
316(debugSession as any).parentSession &&
317this.nodeSession.id === (debugSession as any).parentSession.id
318) {
319this.attachSession = debugSession;
320}
321if (
322debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId &&
323debugSession.type === this.pwaNodeSessionName
324) {
325this.nodeSession = debugSession;
ebbd64f1RedMickey6 years ago326}
327}
6f9a0779JiglioNero5 years ago328
329protected async initializeSettings(args: any): Promise<any> {
330await super.initializeSettings(args);
331if (args.useHermesEngine === undefined) {
332args.useHermesEngine = true;
333}
334}
2c19da7fRedMickey6 years ago335}