microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
302lines · 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"; |
259c018fYuri Skorokhodov5 years ago | 20 | |
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 | | |
34472878RedMickey5 years ago | 59 | return new Promise<void>((resolve, reject) => |
| 60 | this.initializeSettings(launchArgs) | |
| 61 | .then(() => { | |
| 62 | logger.log("Launching the application"); | |
| 63 | logger.verbose( | |
| 64 | `Launching the application: ${JSON.stringify(launchArgs, null, 2)}`, | |
| 65 | ); | |
341dba36Yuri Skorokhodov5 years ago | 66 | |
34472878RedMickey5 years ago | 67 | return ProjectVersionHelper.getReactNativeVersions( |
| 68 | launchArgs.cwd, | |
| 69 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform( | |
| 70 | launchArgs, | |
| 71 | ), | |
| 72 | ); | |
| 73 | }) | |
| 74 | .then(versions => { | |
| 75 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 76 | launchArgs, | |
| 77 | versions, | |
| 78 | extProps, | |
| 79 | ); | |
341dba36Yuri Skorokhodov5 years ago | 80 | |
34472878RedMickey5 years ago | 81 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 82 | return TelemetryHelper.generate("launch", extProps, generator => { | |
| 83 | return this.appLauncher.launch(launchArgs).then(() => { | |
5514e287RedMickey6 years ago | 84 | if (launchArgs.enableDebug) { |
34472878RedMickey5 years ago | 85 | this.attachRequest(response, launchArgs) |
| 86 | .then(() => { | |
| 87 | resolve(); | |
| 88 | }) | |
| 89 | .catch(e => reject(e)); | |
5514e287RedMickey6 years ago | 90 | } else { |
| 91 | this.sendResponse(response); | |
| 92 | resolve(); | |
| 93 | } | |
2c19da7fRedMickey6 years ago | 94 | }); |
34472878RedMickey5 years ago | 95 | }); |
| 96 | }) | |
| 97 | .catch(err => { | |
| 98 | reject( | |
| 99 | ErrorHelper.getInternalError( | |
| 100 | InternalErrorCode.ApplicationLaunchFailed, | |
| 101 | err.message || err, | |
| 102 | ), | |
| 103 | ); | |
| 104 | }), | |
| 105 | ).catch(err => this.showError(err, response)); | |
2c19da7fRedMickey6 years ago | 106 | } |
| 107 | | |
34472878RedMickey5 years ago | 108 | protected async attachRequest( |
| 109 | response: DebugProtocol.AttachResponse, | |
| 110 | attachArgs: IAttachRequestArgs, | |
| 111 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 112 | request?: DebugProtocol.Request, | |
| 113 | ): Promise<void> { | |
2c19da7fRedMickey6 years ago | 114 | let extProps = { |
| 115 | platform: { | |
| 116 | value: attachArgs.platform, | |
| 117 | isPii: false, | |
| 118 | }, | |
| 119 | isDirect: { | |
| 120 | value: true, | |
| 121 | isPii: false, | |
| 122 | }, | |
| 123 | }; | |
| 124 | | |
259c018fYuri Skorokhodov5 years ago | 125 | attachArgs.webkitRangeMin = attachArgs.webkitRangeMin || 9223; |
| 126 | attachArgs.webkitRangeMax = attachArgs.webkitRangeMax || 9322; | |
| 127 | | |
2c19da7fRedMickey6 years ago | 128 | this.previousAttachArgs = attachArgs; |
| 129 | | |
34472878RedMickey5 years ago | 130 | return new Promise<void>((resolve, reject) => |
| 131 | this.initializeSettings(attachArgs) | |
| 132 | .then(() => { | |
| 133 | logger.log("Attaching to the application"); | |
| 134 | logger.verbose( | |
| 135 | `Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`, | |
| 136 | ); | |
| 137 | return ProjectVersionHelper.getReactNativeVersions( | |
| 138 | attachArgs.cwd, | |
| 139 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform( | |
| 140 | attachArgs, | |
| 141 | ), | |
| 142 | ); | |
| 143 | }) | |
| 144 | .then(versions => { | |
| 145 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 146 | attachArgs, | |
| 147 | versions, | |
| 148 | extProps, | |
| 149 | ); | |
| 150 | | |
| 151 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 152 | return TelemetryHelper.generate("attach", extProps, generator => { | |
6f9a0779JiglioNero5 years ago | 153 | const port = attachArgs.useHermesEngine |
| 154 | ? attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd) | |
| 155 | : attachArgs.platform === PlatformType.iOS | |
| 156 | ? attachArgs.port || IWDPHelper.iOS_WEBKIT_DEBUG_PROXY_DEFAULT_PORT | |
| 157 | : null; | |
| 158 | if (port === null) { | |
| 159 | return Promise.reject( | |
| 160 | ErrorHelper.getInternalError( | |
5c9b8eb8RedMickey5 years ago | 161 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, |
| 162 | attachArgs.platform, | |
6f9a0779JiglioNero5 years ago | 163 | ), |
| 164 | ); | |
| 165 | } | |
| 166 | attachArgs.port = port; | |
34472878RedMickey5 years ago | 167 | logger.log(`Connecting to ${attachArgs.port} port`); |
| 168 | return this.appLauncher | |
| 169 | .getRnCdpProxy() | |
| 170 | .stopServer() | |
6f9a0779JiglioNero5 years ago | 171 | .then(() => { |
| 172 | const cdpProxy: BaseCDPMessageHandler | null = attachArgs.useHermesEngine | |
| 173 | ? new HermesCDPMessageHandler() | |
| 174 | : attachArgs.platform === PlatformType.iOS | |
| 175 | ? new IOSDirectCDPMessageHandler() | |
| 176 | : null; | |
| 177 | | |
| 178 | if (!cdpProxy) { | |
| 179 | return Promise.reject( | |
| 180 | ErrorHelper.getInternalError( | |
5c9b8eb8RedMickey5 years ago | 181 | InternalErrorCode.CouldNotDirectDebugWithoutHermesEngine, |
| 182 | attachArgs.platform, | |
6f9a0779JiglioNero5 years ago | 183 | ), |
| 184 | ); | |
| 185 | } | |
| 186 | return this.appLauncher | |
34472878RedMickey5 years ago | 187 | .getRnCdpProxy() |
6f9a0779JiglioNero5 years ago | 188 | .initializeServer(cdpProxy, this.cdpProxyLogLevel); |
| 189 | }) | |
34472878RedMickey5 years ago | 190 | .then(() => { |
6f9a0779JiglioNero5 years ago | 191 | if ( |
| 192 | !attachArgs.useHermesEngine && | |
| 193 | attachArgs.platform === PlatformType.iOS | |
| 194 | ) { | |
34472878RedMickey5 years ago | 195 | return this.iOSWKDebugProxyHelper |
| 196 | .startiOSWebkitDebugProxy( | |
| 197 | attachArgs.port, | |
| 198 | attachArgs.webkitRangeMin, | |
| 199 | attachArgs.webkitRangeMax, | |
| 200 | ) | |
| 201 | .then(() => | |
| 202 | this.iOSWKDebugProxyHelper.getSimulatorProxyPort( | |
| 203 | attachArgs, | |
| 204 | ), | |
| 205 | ) | |
| 206 | .then(results => { | |
| 207 | attachArgs.port = results.targetPort; | |
| 208 | }); | |
| 209 | } else { | |
| 210 | return Promise.resolve(); | |
| 211 | } | |
| 212 | }) | |
| 213 | .then(() => this.appLauncher.getPackager().start()) | |
| 214 | .then(() => | |
| 215 | this.debuggerEndpointHelper.retryGetWSEndpoint( | |
| 216 | `http://localhost:${attachArgs.port}`, | |
| 217 | 90, | |
| 218 | this.cancellationTokenSource.token, | |
| 219 | ), | |
| 220 | ) | |
| 221 | .then(browserInspectUri => { | |
| 222 | this.appLauncher | |
| 223 | .getRnCdpProxy() | |
| 224 | .setBrowserInspectUri(browserInspectUri); | |
| 225 | this.establishDebugSession(attachArgs, resolve); | |
| 226 | }) | |
| 227 | .catch(e => reject(e)); | |
| 228 | }); | |
| 229 | }) | |
| 230 | .catch(err => { | |
| 231 | reject( | |
| 232 | ErrorHelper.getInternalError( | |
| 233 | InternalErrorCode.CouldNotAttachToDebugger, | |
| 234 | err.message || err, | |
| 235 | ), | |
| 236 | ); | |
| 237 | }), | |
| 238 | ).catch(err => this.showError(err, response)); | |
2c19da7fRedMickey6 years ago | 239 | } |
| 240 | | |
34472878RedMickey5 years ago | 241 | protected async disconnectRequest( |
| 242 | response: DebugProtocol.DisconnectResponse, | |
| 243 | args: DebugProtocol.DisconnectArguments, | |
| 244 | request?: DebugProtocol.Request, | |
| 245 | ): Promise<void> { | |
259c018fYuri Skorokhodov5 years ago | 246 | this.iOSWKDebugProxyHelper.cleanUp(); |
ebbd64f1RedMickey6 years ago | 247 | this.onDidTerminateDebugSessionHandler.dispose(); |
2c19da7fRedMickey6 years ago | 248 | super.disconnectRequest(response, args, request); |
| 249 | } | |
| 250 | | |
34472878RedMickey5 years ago | 251 | protected establishDebugSession( |
| 252 | attachArgs: IAttachRequestArgs, | |
| 253 | resolve?: (value?: void | PromiseLike<void> | undefined) => void, | |
| 254 | ): void { | |
1bdccb66RedMickey6 years ago | 255 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 256 | attachArgs, | |
| 257 | this.appLauncher.getCdpProxyPort(), | |
34472878RedMickey5 years ago | 258 | this.session.id, |
1bdccb66RedMickey6 years ago | 259 | ); |
b7451aefRedMickey6 years ago | 260 | |
34472878RedMickey5 years ago | 261 | vscode.debug |
| 262 | .startDebugging(this.appLauncher.getWorkspaceFolder(), attachConfiguration, { | |
ebbd64f1RedMickey6 years ago | 263 | parentSession: this.session, |
| 264 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
34472878RedMickey5 years ago | 265 | }) |
| 266 | .then( | |
| 267 | (childDebugSessionStarted: boolean) => { | |
| 268 | if (childDebugSessionStarted) { | |
| 269 | if (resolve) { | |
| 270 | resolve(); | |
| 271 | } | |
| 272 | } else { | |
| 273 | throw new Error( | |
| 274 | localize( | |
| 275 | "CouldNotStartChildDebugSession", | |
| 276 | "Couldn't start child debug session", | |
| 277 | ), | |
| 278 | ); | |
259c018fYuri Skorokhodov5 years ago | 279 | } |
34472878RedMickey5 years ago | 280 | }, |
259c018fYuri Skorokhodov5 years ago | 281 | err => { |
| 282 | throw err; | |
34472878RedMickey5 years ago | 283 | }, |
| 284 | ); | |
b7451aefRedMickey6 years ago | 285 | } |
ebbd64f1RedMickey6 years ago | 286 | |
| 287 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { | |
| 288 | if ( | |
34472878RedMickey5 years ago | 289 | debugSession.configuration.rnDebugSessionId === this.session.id && |
| 290 | debugSession.type === this.pwaNodeSessionName | |
ebbd64f1RedMickey6 years ago | 291 | ) { |
a2ddbba5RedMickey5 years ago | 292 | vscode.commands.executeCommand(this.stopCommand, this.session); |
ebbd64f1RedMickey6 years ago | 293 | } |
| 294 | } | |
6f9a0779JiglioNero5 years ago | 295 | |
| 296 | protected async initializeSettings(args: any): Promise<any> { | |
| 297 | await super.initializeSettings(args); | |
| 298 | if (args.useHermesEngine === undefined) { | |
| 299 | args.useHermesEngine = true; | |
| 300 | } | |
| 301 | } | |
2c19da7fRedMickey6 years ago | 302 | } |