microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8f322bcd71555e02f0b00a89a99da351ef8412b7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugSession.ts

257lines · 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 { ProjectVersionHelper } from "../../common/projectVersionHelper";
984ca036RedMickey6 years ago6import { logger } from "vscode-debugadapter";
2c19da7fRedMickey6 years ago7import { TelemetryHelper } from "../../common/telemetryHelper";
8import { DebugProtocol } from "vscode-debugprotocol";
259c018fYuri Skorokhodov5 years ago9import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler";
2c19da7fRedMickey6 years ago10import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase";
1bdccb66RedMickey6 years ago11import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter";
984ca036RedMickey6 years ago12import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper";
5514e287RedMickey6 years ago13import { ErrorHelper } from "../../common/error/errorHelper";
14import { InternalErrorCode } from "../../common/error/internalErrorCode";
2c19da7fRedMickey6 years ago15import * as nls from "vscode-nls";
259c018fYuri Skorokhodov5 years ago16import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler";
17import { PlatformType } from "../../extension/launchArgs";
18import { IWDPHelper } from "./IWDPHelper";
6f9a0779JiglioNero5 years ago19import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler";
f338085detatanova4 years ago20import { TipNotificationService } from "../../extension/tipsNotificationsService/tipsNotificationService";
34472878RedMickey5 years ago21nls.config({
22messageFormat: nls.MessageFormat.bundle,
23bundleFormat: nls.BundleFormat.standalone,
24})();
2c19da7fRedMickey6 years ago25const localize = nls.loadMessageBundle();
26
27export class DirectDebugSession extends DebugSessionBase {
984ca036RedMickey6 years ago28private debuggerEndpointHelper: DebuggerEndpointHelper;
ebbd64f1RedMickey6 years ago29private onDidTerminateDebugSessionHandler: vscode.Disposable;
259c018fYuri Skorokhodov5 years ago30private iOSWKDebugProxyHelper: IWDPHelper;
984ca036RedMickey6 years ago31
2c19da7fRedMickey6 years ago32constructor(session: vscode.DebugSession) {
33super(session);
984ca036RedMickey6 years ago34this.debuggerEndpointHelper = new DebuggerEndpointHelper();
259c018fYuri Skorokhodov5 years ago35this.iOSWKDebugProxyHelper = new IWDPHelper();
ebbd64f1RedMickey6 years ago36
37this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
34472878RedMickey5 years ago38this.handleTerminateDebugSession.bind(this),
ebbd64f1RedMickey6 years ago39);
2c19da7fRedMickey6 years ago40}
41
34472878RedMickey5 years ago42protected async launchRequest(
43response: DebugProtocol.LaunchResponse,
44launchArgs: ILaunchRequestArgs,
45// eslint-disable-next-line @typescript-eslint/no-unused-vars
46request?: DebugProtocol.Request,
47): Promise<void> {
2c19da7fRedMickey6 years ago48let extProps = {
49platform: {
50value: launchArgs.platform,
51isPii: false,
52},
53isDirect: {
54value: true,
55isPii: false,
56},
57};
58
f338085detatanova4 years ago59TipNotificationService.getInstance().setKnownDateForFeatureById(
60"directDebuggingWithHermes",
61);
62
0d77292aJiglioNero4 years ago63try {
64try {
65await this.initializeSettings(launchArgs);
66logger.log("Launching the application");
67logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
68
69const versions = await ProjectVersionHelper.getReactNativeVersions(
70this.projectRootPath,
71ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs),
72);
73extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
74launchArgs,
75versions,
76extProps,
77);
78
79// eslint-disable-next-line @typescript-eslint/no-unused-vars
80await TelemetryHelper.generate("launch", extProps, generator =>
81this.appLauncher.launch(launchArgs),
82);
83
84if (!launchArgs.enableDebug) {
85this.sendResponse(response);
86// if debugging is not enabled skip attach request
87return;
88}
89} catch (error) {
90throw ErrorHelper.getInternalError(
91InternalErrorCode.ApplicationLaunchFailed,
92error.message || error,
93);
94}
95// if debugging is enabled start attach request
96await this.attachRequest(response, launchArgs);
97} catch (error) {
98this.showError(error, response);
99}
2c19da7fRedMickey6 years ago100}
101
34472878RedMickey5 years ago102protected async attachRequest(
103response: DebugProtocol.AttachResponse,
104attachArgs: IAttachRequestArgs,
105// eslint-disable-next-line @typescript-eslint/no-unused-vars
106request?: DebugProtocol.Request,
107): Promise<void> {
2c19da7fRedMickey6 years ago108let extProps = {
109platform: {
110value: attachArgs.platform,
111isPii: false,
112},
113isDirect: {
114value: true,
115isPii: false,
116},
117};
118
259c018fYuri Skorokhodov5 years ago119attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223;
120attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322;
121
2c19da7fRedMickey6 years ago122this.previousAttachArgs = attachArgs;
123
0d77292aJiglioNero4 years ago124try {
125await this.initializeSettings(attachArgs);
126logger.log("Attaching to the application");
127logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`);
128
129const versions = await ProjectVersionHelper.getReactNativeVersions(
130this.projectRootPath,
131ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs),
132);
133extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
134attachArgs,
135versions,
136extProps,
137);
138
139// eslint-disable-next-line @typescript-eslint/no-unused-vars
140await TelemetryHelper.generate("attach", extProps, async generator => {
141const port = attachArgs.useHermesEngine
142? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd)
143: attachArgs.platform === PlatformType.iOS
144? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT
145: null;
146if (port === null) {
147throw ErrorHelper.getInternalError(
148InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
149attachArgs.platform,
34472878RedMickey5 years ago150);
0d77292aJiglioNero4 years ago151}
152attachArgs.port = port;
153logger.log(`Connecting to ${attachArgs.port} port`);
154await this.appLauncher.getRnCdpProxy().stopServer();
155
48ca0c62RedMickey4 years ago156const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine
0d77292aJiglioNero4 years ago157? new HermesCDPMessageHandler()
158: attachArgs.platform === PlatformType.iOS
159? new IOSDirectCDPMessageHandler()
160: null;
161
48ca0c62RedMickey4 years ago162if (!cdpMessageHandler) {
0d77292aJiglioNero4 years ago163throw ErrorHelper.getInternalError(
164InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine,
165attachArgs.platform,
34472878RedMickey5 years ago166);
0d77292aJiglioNero4 years ago167}
168await this.appLauncher
169.getRnCdpProxy()
dcabd9c8RedMickey4 years ago170.initializeServer(
48ca0c62RedMickey4 years ago171cdpMessageHandler,
dcabd9c8RedMickey4 years ago172this.cdpProxyLogLevel,
173this.cancellationTokenSource.token,
174);
0d77292aJiglioNero4 years ago175
176if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) {
177await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy(
178attachArgs.port,
179attachArgs.webkitRangeMin,
180attachArgs.webkitRangeMax,
34472878RedMickey5 years ago181);
0d77292aJiglioNero4 years ago182const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort(
183attachArgs,
34472878RedMickey5 years ago184);
0d77292aJiglioNero4 years ago185attachArgs.port = results.targetPort;
186}
187
188await this.appLauncher.getPackager().start();
189
190const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
191`http://localhost:${attachArgs.port}`,
19290,
193this.cancellationTokenSource.token,
48ca0c62RedMickey4 years ago194attachArgs.useHermesEngine,
0d77292aJiglioNero4 years ago195);
196this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
197await this.establishDebugSession(attachArgs);
198});
199} catch (error) {
200this.showError(
201ErrorHelper.getInternalError(
202InternalErrorCode.CouldNotAttachToDebugger,
203error.message || error,
204),
205response,
206);
207}
2c19da7fRedMickey6 years ago208}
209
34472878RedMickey5 years ago210protected async disconnectRequest(
211response: DebugProtocol.DisconnectResponse,
212args: DebugProtocol.DisconnectArguments,
213request?: DebugProtocol.Request,
214): Promise<void> {
259c018fYuri Skorokhodov5 years ago215this.iOSWKDebugProxyHelper.cleanUp();
ebbd64f1RedMickey6 years ago216this.onDidTerminateDebugSessionHandler.dispose();
2c19da7fRedMickey6 years ago217super.disconnectRequest(response, args, request);
218}
219
0d77292aJiglioNero4 years ago220protected async establishDebugSession(attachArgs: IAttachRequestArgs): Promise<void> {
1bdccb66RedMickey6 years ago221const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes(
222attachArgs,
223this.appLauncher.getCdpProxyPort(),
34472878RedMickey5 years ago224this.session.id,
1bdccb66RedMickey6 years ago225);
b7451aefRedMickey6 years ago226
0d77292aJiglioNero4 years ago227const childDebugSessionStarted = await vscode.debug.startDebugging(
228this.appLauncher.getWorkspaceFolder(),
229attachConfiguration,
230{
ebbd64f1RedMickey6 years ago231parentSession: this.session,
232consoleMode: vscode.DebugConsoleMode.MergeWithParent,
0d77292aJiglioNero4 years ago233},
234);
235if (!childDebugSessionStarted) {
236throw new Error(
237localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"),
34472878RedMickey5 years ago238);
0d77292aJiglioNero4 years ago239}
b7451aefRedMickey6 years ago240}
ebbd64f1RedMickey6 years ago241
0d77292aJiglioNero4 years ago242private handleTerminateDebugSession(debugSession: vscode.DebugSession): void {
ebbd64f1RedMickey6 years ago243if (
34472878RedMickey5 years ago244debugSession.configuration.rnDebugSessionId === this.session.id &&
245debugSession.type === this.pwaNodeSessionName
ebbd64f1RedMickey6 years ago246) {
a2ddbba5RedMickey5 years ago247vscode.commands.executeCommand(this.stopCommand, this.session);
ebbd64f1RedMickey6 years ago248}
249}
6f9a0779JiglioNero5 years ago250
251protected async initializeSettings(args: any): Promise<any> {
252await super.initializeSettings(args);
253if (args.useHermesEngine === undefined) {
254args.useHermesEngine = true;
255}
256}
2c19da7fRedMickey6 years ago257}