microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
257lines · modeblame
2c19da7fRedMickey6 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as vscode from "vscode"; | |
| 5 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; | |
984ca036RedMickey6 years ago | 6 | import { logger } from "vscode-debugadapter"; |
2c19da7fRedMickey6 years ago | 7 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; | |
259c018fYuri Skorokhodov5 years ago | 9 | import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler"; |
2c19da7fRedMickey6 years ago | 10 | import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase"; |
1bdccb66RedMickey6 years ago | 11 | import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter"; |
984ca036RedMickey6 years ago | 12 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
5514e287RedMickey6 years ago | 13 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 14 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
2c19da7fRedMickey6 years ago | 15 | import * as nls from "vscode-nls"; |
259c018fYuri Skorokhodov5 years ago | 16 | import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler"; |
| 17 | import { PlatformType } from "../../extension/launchArgs"; | |
| 18 | import { IWDPHelper } from "./IWDPHelper"; | |
6f9a0779JiglioNero5 years ago | 19 | import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler"; |
f338085detatanova4 years ago | 20 | import { TipNotificationService } from "../../extension/tipsNotificationsService/tipsNotificationService"; |
34472878RedMickey5 years ago | 21 | nls.config({ |
| 22 | messageFormat: nls.MessageFormat.bundle, | |
| 23 | bundleFormat: nls.BundleFormat.standalone, | |
| 24 | })(); | |
2c19da7fRedMickey6 years ago | 25 | const localize = nls.loadMessageBundle(); |
| 26 | | |
| 27 | export class DirectDebugSession extends DebugSessionBase { | |
984ca036RedMickey6 years ago | 28 | private debuggerEndpointHelper: DebuggerEndpointHelper; |
ebbd64f1RedMickey6 years ago | 29 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
259c018fYuri Skorokhodov5 years ago | 30 | private iOSWKDebugProxyHelper: IWDPHelper; |
984ca036RedMickey6 years ago | 31 | |
2c19da7fRedMickey6 years ago | 32 | constructor(session: vscode.DebugSession) { |
| 33 | super(session); | |
984ca036RedMickey6 years ago | 34 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
259c018fYuri Skorokhodov5 years ago | 35 | this.iOSWKDebugProxyHelper = new IWDPHelper(); |
ebbd64f1RedMickey6 years ago | 36 | |
| 37 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( | |
34472878RedMickey5 years ago | 38 | this.handleTerminateDebugSession.bind(this), |
ebbd64f1RedMickey6 years ago | 39 | ); |
2c19da7fRedMickey6 years ago | 40 | } |
| 41 | | |
34472878RedMickey5 years ago | 42 | protected async launchRequest( |
| 43 | response: DebugProtocol.LaunchResponse, | |
| 44 | launchArgs: ILaunchRequestArgs, | |
| 45 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 46 | request?: DebugProtocol.Request, | |
| 47 | ): Promise<void> { | |
2c19da7fRedMickey6 years ago | 48 | let extProps = { |
| 49 | platform: { | |
| 50 | value: launchArgs.platform, | |
| 51 | isPii: false, | |
| 52 | }, | |
| 53 | isDirect: { | |
| 54 | value: true, | |
| 55 | isPii: false, | |
| 56 | }, | |
| 57 | }; | |
| 58 | | |
f338085detatanova4 years ago | 59 | TipNotificationService.getInstance().setKnownDateForFeatureById( |
| 60 | "directDebuggingWithHermes", | |
| 61 | ); | |
| 62 | | |
0d77292aJiglioNero4 years ago | 63 | try { |
| 64 | try { | |
| 65 | await this.initializeSettings(launchArgs); | |
| 66 | logger.log("Launching the application"); | |
| 67 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`); | |
| 68 | | |
| 69 | const versions = await ProjectVersionHelper.getReactNativeVersions( | |
| 70 | this.projectRootPath, | |
| 71 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs), | |
| 72 | ); | |
| 73 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 74 | launchArgs, | |
| 75 | versions, | |
| 76 | extProps, | |
| 77 | ); | |
| 78 | | |
| 79 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 80 | await TelemetryHelper.generate("launch", extProps, generator => | |
| 81 | this.appLauncher.launch(launchArgs), | |
| 82 | ); | |
| 83 | | |
| 84 | if (!launchArgs.enableDebug) { | |
| 85 | this.sendResponse(response); | |
| 86 | // if debugging is not enabled skip attach request | |
| 87 | return; | |
| 88 | } | |
| 89 | } catch (error) { | |
| 90 | throw ErrorHelper.getInternalError( | |
| 91 | InternalErrorCode.ApplicationLaunchFailed, | |
| 92 | error.message || error, | |
| 93 | ); | |
| 94 | } | |
| 95 | // if debugging is enabled start attach request | |
| 96 | await this.attachRequest(response, launchArgs); | |
| 97 | } catch (error) { | |
| 98 | this.showError(error, response); | |
| 99 | } | |
2c19da7fRedMickey6 years ago | 100 | } |
| 101 | | |
34472878RedMickey5 years ago | 102 | protected async attachRequest( |
| 103 | response: DebugProtocol.AttachResponse, | |
| 104 | attachArgs: IAttachRequestArgs, | |
| 105 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 106 | request?: DebugProtocol.Request, | |
| 107 | ): Promise<void> { | |
2c19da7fRedMickey6 years ago | 108 | let extProps = { |
| 109 | platform: { | |
| 110 | value: attachArgs.platform, | |
| 111 | isPii: false, | |
| 112 | }, | |
| 113 | isDirect: { | |
| 114 | value: true, | |
| 115 | isPii: false, | |
| 116 | }, | |
| 117 | }; | |
| 118 | | |
259c018fYuri Skorokhodov5 years ago | 119 | attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223; |
| 120 | attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322; | |
| 121 | | |
2c19da7fRedMickey6 years ago | 122 | this.previousAttachArgs = attachArgs; |
| 123 | | |
0d77292aJiglioNero4 years ago | 124 | try { |
| 125 | await this.initializeSettings(attachArgs); | |
| 126 | logger.log("Attaching to the application"); | |
| 127 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`); | |
| 128 | | |
| 129 | const versions = await ProjectVersionHelper.getReactNativeVersions( | |
| 130 | this.projectRootPath, | |
| 131 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs), | |
| 132 | ); | |
| 133 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 134 | attachArgs, | |
| 135 | versions, | |
| 136 | extProps, | |
| 137 | ); | |
| 138 | | |
| 139 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 140 | await TelemetryHelper.generate("attach", extProps, async generator => { | |
| 141 | const 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; | |
| 146 | if (port === null) { | |
| 147 | throw ErrorHelper.getInternalError( | |
| 148 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, | |
| 149 | attachArgs.platform, | |
34472878RedMickey5 years ago | 150 | ); |
0d77292aJiglioNero4 years ago | 151 | } |
| 152 | attachArgs.port = port; | |
| 153 | logger.log(`Connecting to ${attachArgs.port} port`); | |
| 154 | await this.appLauncher.getRnCdpProxy().stopServer(); | |
| 155 | | |
48ca0c62RedMickey4 years ago | 156 | const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine |
0d77292aJiglioNero4 years ago | 157 | ? new HermesCDPMessageHandler() |
| 158 | : attachArgs.platform === PlatformType.iOS | |
| 159 | ? new IOSDirectCDPMessageHandler() | |
| 160 | : null; | |
| 161 | | |
48ca0c62RedMickey4 years ago | 162 | if (!cdpMessageHandler) { |
0d77292aJiglioNero4 years ago | 163 | throw ErrorHelper.getInternalError( |
| 164 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, | |
| 165 | attachArgs.platform, | |
34472878RedMickey5 years ago | 166 | ); |
0d77292aJiglioNero4 years ago | 167 | } |
| 168 | await this.appLauncher | |
| 169 | .getRnCdpProxy() | |
dcabd9c8RedMickey4 years ago | 170 | .initializeServer( |
48ca0c62RedMickey4 years ago | 171 | cdpMessageHandler, |
dcabd9c8RedMickey4 years ago | 172 | this.cdpProxyLogLevel, |
| 173 | this.cancellationTokenSource.token, | |
| 174 | ); | |
0d77292aJiglioNero4 years ago | 175 | |
| 176 | if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) { | |
| 177 | await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy( | |
| 178 | attachArgs.port, | |
| 179 | attachArgs.webkitRangeMin, | |
| 180 | attachArgs.webkitRangeMax, | |
34472878RedMickey5 years ago | 181 | ); |
0d77292aJiglioNero4 years ago | 182 | const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort( |
| 183 | attachArgs, | |
34472878RedMickey5 years ago | 184 | ); |
0d77292aJiglioNero4 years ago | 185 | attachArgs.port = results.targetPort; |
| 186 | } | |
| 187 | | |
| 188 | await this.appLauncher.getPackager().start(); | |
| 189 | | |
| 190 | const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint( | |
| 191 | `http://localhost:${attachArgs.port}`, | |
| 192 | 90, | |
| 193 | this.cancellationTokenSource.token, | |
48ca0c62RedMickey4 years ago | 194 | attachArgs.useHermesEngine, |
0d77292aJiglioNero4 years ago | 195 | ); |
| 196 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); | |
| 197 | await this.establishDebugSession(attachArgs); | |
| 198 | }); | |
| 199 | } catch (error) { | |
| 200 | this.showError( | |
| 201 | ErrorHelper.getInternalError( | |
| 202 | InternalErrorCode.CouldNotAttachToDebugger, | |
| 203 | error.message || error, | |
| 204 | ), | |
| 205 | response, | |
| 206 | ); | |
| 207 | } | |
2c19da7fRedMickey6 years ago | 208 | } |
| 209 | | |
34472878RedMickey5 years ago | 210 | protected async disconnectRequest( |
| 211 | response: DebugProtocol.DisconnectResponse, | |
| 212 | args: DebugProtocol.DisconnectArguments, | |
| 213 | request?: DebugProtocol.Request, | |
| 214 | ): Promise<void> { | |
259c018fYuri Skorokhodov5 years ago | 215 | this.iOSWKDebugProxyHelper.cleanUp(); |
ebbd64f1RedMickey6 years ago | 216 | this.onDidTerminateDebugSessionHandler.dispose(); |
2c19da7fRedMickey6 years ago | 217 | super.disconnectRequest(response, args, request); |
| 218 | } | |
| 219 | | |
0d77292aJiglioNero4 years ago | 220 | protected async establishDebugSession(attachArgs: IAttachRequestArgs): Promise<void> { |
1bdccb66RedMickey6 years ago | 221 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 222 | attachArgs, | |
| 223 | this.appLauncher.getCdpProxyPort(), | |
34472878RedMickey5 years ago | 224 | this.session.id, |
1bdccb66RedMickey6 years ago | 225 | ); |
b7451aefRedMickey6 years ago | 226 | |
0d77292aJiglioNero4 years ago | 227 | const childDebugSessionStarted = await vscode.debug.startDebugging( |
| 228 | this.appLauncher.getWorkspaceFolder(), | |
| 229 | attachConfiguration, | |
| 230 | { | |
ebbd64f1RedMickey6 years ago | 231 | parentSession: this.session, |
| 232 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
0d77292aJiglioNero4 years ago | 233 | }, |
| 234 | ); | |
| 235 | if (!childDebugSessionStarted) { | |
| 236 | throw new Error( | |
| 237 | localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"), | |
34472878RedMickey5 years ago | 238 | ); |
0d77292aJiglioNero4 years ago | 239 | } |
b7451aefRedMickey6 years ago | 240 | } |
ebbd64f1RedMickey6 years ago | 241 | |
0d77292aJiglioNero4 years ago | 242 | private handleTerminateDebugSession(debugSession: vscode.DebugSession): void { |
ebbd64f1RedMickey6 years ago | 243 | if ( |
34472878RedMickey5 years ago | 244 | debugSession.configuration.rnDebugSessionId === this.session.id && |
| 245 | debugSession.type === this.pwaNodeSessionName | |
ebbd64f1RedMickey6 years ago | 246 | ) { |
a2ddbba5RedMickey5 years ago | 247 | vscode.commands.executeCommand(this.stopCommand, this.session); |
ebbd64f1RedMickey6 years ago | 248 | } |
| 249 | } | |
6f9a0779JiglioNero5 years ago | 250 | |
| 251 | protected async initializeSettings(args: any): Promise<any> { | |
| 252 | await super.initializeSettings(args); | |
| 253 | if (args.useHermesEngine === undefined) { | |
| 254 | args.useHermesEngine = true; | |
| 255 | } | |
| 256 | } | |
2c19da7fRedMickey6 years ago | 257 | } |