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