microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
335lines · 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"; | |
984ca036RedMickey6 years ago | 5 | import { logger } from "vscode-debugadapter"; |
2c19da7fRedMickey6 years ago | 6 | import { DebugProtocol } from "vscode-debugprotocol"; |
09f6024fHeniker4 years ago | 7 | import * as nls from "vscode-nls"; |
| 8 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; | |
| 9 | import { TelemetryHelper } from "../../common/telemetryHelper"; | |
259c018fYuri Skorokhodov5 years ago | 10 | import { HermesCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler"; |
19df32dcRedMickey4 years ago | 11 | import { |
| 12 | DebugSessionBase, | |
| 13 | DebugSessionStatus, | |
| 14 | IAttachRequestArgs, | |
| 15 | ILaunchRequestArgs, | |
| 16 | } from "../debugSessionBase"; | |
1bdccb66RedMickey6 years ago | 17 | import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter"; |
984ca036RedMickey6 years ago | 18 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
5514e287RedMickey6 years ago | 19 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 20 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
259c018fYuri Skorokhodov5 years ago | 21 | import { IOSDirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler"; |
| 22 | import { PlatformType } from "../../extension/launchArgs"; | |
6f9a0779JiglioNero5 years ago | 23 | import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler"; |
ab0238b7RedMickey4 years ago | 24 | import { TipNotificationService } from "../../extension/services/tipsNotificationsService/tipsNotificationService"; |
d93677adRedMickey4 years ago | 25 | import { RNSession } from "../debugSessionWrapper"; |
09f6024fHeniker4 years ago | 26 | import { IWDPHelper } from "./IWDPHelper"; |
| 27 | | |
34472878RedMickey5 years ago | 28 | nls.config({ |
| 29 | messageFormat: nls.MessageFormat.bundle, | |
| 30 | bundleFormat: nls.BundleFormat.standalone, | |
| 31 | })(); | |
2c19da7fRedMickey6 years ago | 32 | const localize = nls.loadMessageBundle(); |
| 33 | | |
| 34 | export class DirectDebugSession extends DebugSessionBase { | |
984ca036RedMickey6 years ago | 35 | private debuggerEndpointHelper: DebuggerEndpointHelper; |
ebbd64f1RedMickey6 years ago | 36 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
19df32dcRedMickey4 years ago | 37 | private onDidStartDebugSessionHandler: vscode.Disposable; |
| 38 | private appTargetConnectionClosedHandlerDescriptor?: vscode.Disposable; | |
| 39 | private attachSession: vscode.DebugSession | null; | |
259c018fYuri Skorokhodov5 years ago | 40 | private iOSWKDebugProxyHelper: IWDPHelper; |
984ca036RedMickey6 years ago | 41 | |
d93677adRedMickey4 years ago | 42 | constructor(rnSession: RNSession) { |
| 43 | super(rnSession); | |
984ca036RedMickey6 years ago | 44 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
259c018fYuri Skorokhodov5 years ago | 45 | this.iOSWKDebugProxyHelper = new IWDPHelper(); |
19df32dcRedMickey4 years ago | 46 | this.attachSession = null; |
ebbd64f1RedMickey6 years ago | 47 | |
| 48 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( | |
34472878RedMickey5 years ago | 49 | this.handleTerminateDebugSession.bind(this), |
ebbd64f1RedMickey6 years ago | 50 | ); |
19df32dcRedMickey4 years ago | 51 | |
| 52 | this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession( | |
| 53 | this.handleStartDebugSession.bind(this), | |
| 54 | ); | |
2c19da7fRedMickey6 years ago | 55 | } |
| 56 | | |
34472878RedMickey5 years ago | 57 | protected async launchRequest( |
| 58 | response: DebugProtocol.LaunchResponse, | |
| 59 | launchArgs: ILaunchRequestArgs, | |
| 60 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 61 | request?: DebugProtocol.Request, | |
| 62 | ): Promise<void> { | |
2c19da7fRedMickey6 years ago | 63 | let extProps = { |
| 64 | platform: { | |
| 65 | value: launchArgs.platform, | |
| 66 | isPii: false, | |
| 67 | }, | |
| 68 | isDirect: { | |
| 69 | value: true, | |
| 70 | isPii: false, | |
| 71 | }, | |
| 72 | }; | |
| 73 | | |
09f6024fHeniker4 years ago | 74 | void TipNotificationService.getInstance().setKnownDateForFeatureById( |
f338085detatanova4 years ago | 75 | "directDebuggingWithHermes", |
| 76 | ); | |
| 77 | | |
0d77292aJiglioNero4 years ago | 78 | try { |
| 79 | try { | |
| 80 | await this.initializeSettings(launchArgs); | |
| 81 | logger.log("Launching the application"); | |
| 82 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`); | |
| 83 | | |
| 84 | const versions = await ProjectVersionHelper.getReactNativeVersions( | |
| 85 | this.projectRootPath, | |
| 86 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(launchArgs), | |
| 87 | ); | |
| 88 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 89 | launchArgs, | |
| 90 | versions, | |
| 91 | extProps, | |
| 92 | ); | |
| 93 | | |
| 94 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 95 | await TelemetryHelper.generate("launch", extProps, generator => | |
| 96 | this.appLauncher.launch(launchArgs), | |
| 97 | ); | |
| 98 | | |
| 99 | if (!launchArgs.enableDebug) { | |
| 100 | this.sendResponse(response); | |
| 101 | // if debugging is not enabled skip attach request | |
| 102 | return; | |
| 103 | } | |
| 104 | } catch (error) { | |
| 105 | throw ErrorHelper.getInternalError( | |
| 106 | InternalErrorCode.ApplicationLaunchFailed, | |
| 107 | error.message || error, | |
| 108 | ); | |
| 109 | } | |
| 110 | // if debugging is enabled start attach request | |
d93677adRedMickey4 years ago | 111 | await this.vsCodeDebugSession.customRequest("attach", launchArgs); |
| 112 | this.sendResponse(response); | |
0d77292aJiglioNero4 years ago | 113 | } catch (error) { |
19df32dcRedMickey4 years ago | 114 | this.terminateWithErrorResponse(error, response); |
0d77292aJiglioNero4 years ago | 115 | } |
2c19da7fRedMickey6 years ago | 116 | } |
| 117 | | |
34472878RedMickey5 years ago | 118 | protected async attachRequest( |
| 119 | response: DebugProtocol.AttachResponse, | |
| 120 | attachArgs: IAttachRequestArgs, | |
| 121 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 122 | request?: DebugProtocol.Request, | |
| 123 | ): Promise<void> { | |
2c19da7fRedMickey6 years ago | 124 | let extProps = { |
| 125 | platform: { | |
| 126 | value: attachArgs.platform, | |
| 127 | isPii: false, | |
| 128 | }, | |
| 129 | isDirect: { | |
| 130 | value: true, | |
| 131 | isPii: false, | |
| 132 | }, | |
| 133 | }; | |
| 134 | | |
259c018fYuri Skorokhodov5 years ago | 135 | attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223; |
| 136 | attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322; | |
| 137 | | |
2c19da7fRedMickey6 years ago | 138 | this.previousAttachArgs = attachArgs; |
| 139 | | |
0d77292aJiglioNero4 years ago | 140 | try { |
| 141 | await this.initializeSettings(attachArgs); | |
accd6598Heniker4 years ago | 142 | |
| 143 | const packager = this.appLauncher.getPackager(); | |
| 144 | const args: Parameters<typeof packager.forMessage> = [ | |
| 145 | // message indicates that another debugger has connected | |
| 146 | "Already connected:", | |
| 147 | { | |
| 148 | type: "client_log", | |
| 149 | level: "warn", | |
| 150 | mode: "BRIDGE", | |
| 151 | }, | |
| 152 | ]; | |
| 153 | | |
| 154 | void packager.forMessage(...args).then( | |
| 155 | () => { | |
| 156 | this.showError( | |
| 157 | ErrorHelper.getInternalError( | |
| 158 | InternalErrorCode.AnotherDebuggerConnectedToPackager, | |
| 159 | ), | |
| 160 | ); | |
19df32dcRedMickey4 years ago | 161 | void this.terminate(); |
accd6598Heniker4 years ago | 162 | }, |
| 163 | () => {}, | |
| 164 | ); | |
| 165 | | |
0d77292aJiglioNero4 years ago | 166 | logger.log("Attaching to the application"); |
| 167 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`); | |
| 168 | | |
| 169 | const versions = await ProjectVersionHelper.getReactNativeVersions( | |
| 170 | this.projectRootPath, | |
| 171 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs), | |
| 172 | ); | |
| 173 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 174 | attachArgs, | |
| 175 | versions, | |
| 176 | extProps, | |
| 177 | ); | |
| 178 | | |
| 179 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 180 | await TelemetryHelper.generate("attach", extProps, async generator => { | |
| 181 | const 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; | |
| 186 | if (port === null) { | |
| 187 | throw ErrorHelper.getInternalError( | |
| 188 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, | |
| 189 | attachArgs.platform, | |
34472878RedMickey5 years ago | 190 | ); |
0d77292aJiglioNero4 years ago | 191 | } |
| 192 | attachArgs.port = port; | |
| 193 | logger.log(`Connecting to ${attachArgs.port} port`); | |
| 194 | await this.appLauncher.getRnCdpProxy().stopServer(); | |
| 195 | | |
48ca0c62RedMickey4 years ago | 196 | const cdpMessageHandler: BaseCDPMessageHandler | null = attachArgs.useHermesEngine |
0d77292aJiglioNero4 years ago | 197 | ? new HermesCDPMessageHandler() |
| 198 | : attachArgs.platform === PlatformType.iOS | |
| 199 | ? new IOSDirectCDPMessageHandler() | |
| 200 | : null; | |
| 201 | | |
48ca0c62RedMickey4 years ago | 202 | if (!cdpMessageHandler) { |
0d77292aJiglioNero4 years ago | 203 | throw ErrorHelper.getInternalError( |
| 204 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, | |
| 205 | attachArgs.platform, | |
34472878RedMickey5 years ago | 206 | ); |
0d77292aJiglioNero4 years ago | 207 | } |
| 208 | await this.appLauncher | |
| 209 | .getRnCdpProxy() | |
dcabd9c8RedMickey4 years ago | 210 | .initializeServer( |
48ca0c62RedMickey4 years ago | 211 | cdpMessageHandler, |
dcabd9c8RedMickey4 years ago | 212 | this.cdpProxyLogLevel, |
| 213 | this.cancellationTokenSource.token, | |
| 214 | ); | |
0d77292aJiglioNero4 years ago | 215 | |
| 216 | if (!attachArgs.useHermesEngine && attachArgs.platform === PlatformType.iOS) { | |
| 217 | await this.iOSWKDebugProxyHelper.startiOSWebkitDebugProxy( | |
| 218 | attachArgs.port, | |
| 219 | attachArgs.webkitRangeMin, | |
| 220 | attachArgs.webkitRangeMax, | |
34472878RedMickey5 years ago | 221 | ); |
0d77292aJiglioNero4 years ago | 222 | const results = await this.iOSWKDebugProxyHelper.getSimulatorProxyPort( |
| 223 | attachArgs, | |
34472878RedMickey5 years ago | 224 | ); |
0d77292aJiglioNero4 years ago | 225 | attachArgs.port = results.targetPort; |
| 226 | } | |
| 227 | | |
bfcc8a29Samriel4 years ago | 228 | if (attachArgs.request === "attach") { |
| 229 | await this.preparePackagerBeforeAttach(attachArgs, versions); | |
| 230 | } | |
0d77292aJiglioNero4 years ago | 231 | |
19df32dcRedMickey4 years ago | 232 | this.appTargetConnectionClosedHandlerDescriptor = this.appLauncher |
| 233 | .getRnCdpProxy() | |
| 234 | .onApplicationTargetConnectionClosed(() => { | |
| 235 | if (this.attachSession) { | |
| 236 | if ( | |
| 237 | this.debugSessionStatus !== DebugSessionStatus.Stopping && | |
| 238 | this.debugSessionStatus !== DebugSessionStatus.Stopped | |
| 239 | ) { | |
| 240 | void this.terminate(); | |
| 241 | } | |
| 242 | this.appTargetConnectionClosedHandlerDescriptor?.dispose(); | |
| 243 | } | |
| 244 | }); | |
| 245 | | |
0d77292aJiglioNero4 years ago | 246 | const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint( |
| 247 | `http://localhost:${attachArgs.port}`, | |
| 248 | 90, | |
| 249 | this.cancellationTokenSource.token, | |
48ca0c62RedMickey4 years ago | 250 | attachArgs.useHermesEngine, |
0d77292aJiglioNero4 years ago | 251 | ); |
| 252 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); | |
| 253 | await this.establishDebugSession(attachArgs); | |
| 254 | }); | |
d93677adRedMickey4 years ago | 255 | this.sendResponse(response); |
0d77292aJiglioNero4 years ago | 256 | } catch (error) { |
19df32dcRedMickey4 years ago | 257 | this.terminateWithErrorResponse( |
0d77292aJiglioNero4 years ago | 258 | ErrorHelper.getInternalError( |
| 259 | InternalErrorCode.CouldNotAttachToDebugger, | |
| 260 | error.message || error, | |
| 261 | ), | |
| 262 | response, | |
| 263 | ); | |
| 264 | } | |
2c19da7fRedMickey6 years ago | 265 | } |
| 266 | | |
34472878RedMickey5 years ago | 267 | protected async disconnectRequest( |
| 268 | response: DebugProtocol.DisconnectResponse, | |
| 269 | args: DebugProtocol.DisconnectArguments, | |
| 270 | request?: DebugProtocol.Request, | |
| 271 | ): Promise<void> { | |
19df32dcRedMickey4 years ago | 272 | this.debugSessionStatus = DebugSessionStatus.Stopping; |
| 273 | | |
259c018fYuri Skorokhodov5 years ago | 274 | this.iOSWKDebugProxyHelper.cleanUp(); |
ebbd64f1RedMickey6 years ago | 275 | this.onDidTerminateDebugSessionHandler.dispose(); |
19df32dcRedMickey4 years ago | 276 | this.onDidStartDebugSessionHandler.dispose(); |
accd6598Heniker4 years ago | 277 | this.appLauncher.getPackager().closeWsConnection(); |
19df32dcRedMickey4 years ago | 278 | this.appTargetConnectionClosedHandlerDescriptor?.dispose(); |
d93677adRedMickey4 years ago | 279 | return super.disconnectRequest(response, args, request); |
2c19da7fRedMickey6 years ago | 280 | } |
| 281 | | |
0d77292aJiglioNero4 years ago | 282 | protected async establishDebugSession(attachArgs: IAttachRequestArgs): Promise<void> { |
1bdccb66RedMickey6 years ago | 283 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 284 | attachArgs, | |
| 285 | this.appLauncher.getCdpProxyPort(), | |
d93677adRedMickey4 years ago | 286 | this.rnSession.sessionId, |
1bdccb66RedMickey6 years ago | 287 | ); |
b7451aefRedMickey6 years ago | 288 | |
0d77292aJiglioNero4 years ago | 289 | const childDebugSessionStarted = await vscode.debug.startDebugging( |
| 290 | this.appLauncher.getWorkspaceFolder(), | |
| 291 | attachConfiguration, | |
| 292 | { | |
d93677adRedMickey4 years ago | 293 | parentSession: this.vsCodeDebugSession, |
ebbd64f1RedMickey6 years ago | 294 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, |
0d77292aJiglioNero4 years ago | 295 | }, |
| 296 | ); | |
| 297 | if (!childDebugSessionStarted) { | |
| 298 | throw new Error( | |
| 299 | localize("CouldNotStartChildDebugSession", "Couldn't start child debug session"), | |
34472878RedMickey5 years ago | 300 | ); |
0d77292aJiglioNero4 years ago | 301 | } |
b7451aefRedMickey6 years ago | 302 | } |
ebbd64f1RedMickey6 years ago | 303 | |
0d77292aJiglioNero4 years ago | 304 | private handleTerminateDebugSession(debugSession: vscode.DebugSession): void { |
ebbd64f1RedMickey6 years ago | 305 | if ( |
d93677adRedMickey4 years ago | 306 | debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId && |
34472878RedMickey5 years ago | 307 | debugSession.type === this.pwaNodeSessionName |
ebbd64f1RedMickey6 years ago | 308 | ) { |
19df32dcRedMickey4 years ago | 309 | void this.terminate(); |
| 310 | } | |
| 311 | } | |
| 312 | | |
| 313 | private handleStartDebugSession(debugSession: vscode.DebugSession): void { | |
| 314 | if ( | |
| 315 | this.nodeSession && | |
| 316 | (debugSession as any).parentSession && | |
| 317 | this.nodeSession.id === (debugSession as any).parentSession.id | |
| 318 | ) { | |
| 319 | this.attachSession = debugSession; | |
| 320 | } | |
| 321 | if ( | |
| 322 | debugSession.configuration.rnDebugSessionId === this.rnSession.sessionId && | |
| 323 | debugSession.type === this.pwaNodeSessionName | |
| 324 | ) { | |
| 325 | this.nodeSession = debugSession; | |
ebbd64f1RedMickey6 years ago | 326 | } |
| 327 | } | |
6f9a0779JiglioNero5 years ago | 328 | |
| 329 | protected async initializeSettings(args: any): Promise<any> { | |
| 330 | await super.initializeSettings(args); | |
| 331 | if (args.useHermesEngine === undefined) { | |
| 332 | args.useHermesEngine = true; | |
| 333 | } | |
| 334 | } | |
2c19da7fRedMickey6 years ago | 335 | } |