microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/rnDebugSession.ts
336lines · modeblame
6e1bcd36RedMickey6 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 * as path from "path"; | |
| 6 | import * as mkdirp from "mkdirp"; | |
2c19da7fRedMickey6 years ago | 7 | import { logger } from "vscode-debugadapter"; |
6e1bcd36RedMickey6 years ago | 8 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 9 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; | |
| 10 | import { TelemetryHelper } from "../common/telemetryHelper"; | |
| 11 | import { MultipleLifetimesAppWorker } from "./appWorker"; | |
a6562589RedMickey6 years ago | 12 | import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler"; |
34472878RedMickey5 years ago | 13 | import { |
| 14 | DebugSessionBase, | |
| 15 | DebugSessionStatus, | |
| 16 | IAttachRequestArgs, | |
| 17 | ILaunchRequestArgs, | |
| 18 | } from "./debugSessionBase"; | |
1bdccb66RedMickey6 years ago | 19 | import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter"; |
5514e287RedMickey6 years ago | 20 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 21 | import { InternalErrorCode } from "../common/error/internalErrorCode"; | |
6e1bcd36RedMickey6 years ago | 22 | import * as nls from "vscode-nls"; |
34472878RedMickey5 years ago | 23 | nls.config({ |
| 24 | messageFormat: nls.MessageFormat.bundle, | |
| 25 | bundleFormat: nls.BundleFormat.standalone, | |
| 26 | })(); | |
6e1bcd36RedMickey6 years ago | 27 | const localize = nls.loadMessageBundle(); |
| 28 | | |
2c19da7fRedMickey6 years ago | 29 | export class RNDebugSession extends DebugSessionBase { |
4c757eebRedMickey6 years ago | 30 | private readonly terminateCommand: string; |
f872f4d5RedMickey6 years ago | 31 | |
e23d1841RedMickey6 years ago | 32 | private appWorker: MultipleLifetimesAppWorker | null; |
4c757eebRedMickey6 years ago | 33 | private nodeSession: vscode.DebugSession | null; |
6e491635RedMickey6 years ago | 34 | private onDidStartDebugSessionHandler: vscode.Disposable; |
| 35 | private onDidTerminateDebugSessionHandler: vscode.Disposable; | |
6e1bcd36RedMickey6 years ago | 36 | |
2c19da7fRedMickey6 years ago | 37 | constructor(session: vscode.DebugSession) { |
| 38 | super(session); | |
4c757eebRedMickey6 years ago | 39 | |
| 40 | // constants definition | |
| 41 | this.terminateCommand = "terminate"; // the "terminate" command is sent from the client to the debug adapter in order to give the debuggee a chance for terminating itself | |
| 42 | | |
| 43 | // variables definition | |
e23d1841RedMickey6 years ago | 44 | this.appWorker = null; |
| 45 | | |
6e491635RedMickey6 years ago | 46 | this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession( |
34472878RedMickey5 years ago | 47 | this.handleStartDebugSession.bind(this), |
4c757eebRedMickey6 years ago | 48 | ); |
| 49 | | |
6e491635RedMickey6 years ago | 50 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( |
34472878RedMickey5 years ago | 51 | this.handleTerminateDebugSession.bind(this), |
4c757eebRedMickey6 years ago | 52 | ); |
6e1bcd36RedMickey6 years ago | 53 | } |
| 54 | | |
34472878RedMickey5 years ago | 55 | protected async launchRequest( |
| 56 | response: DebugProtocol.LaunchResponse, | |
| 57 | launchArgs: ILaunchRequestArgs, | |
| 58 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 59 | request?: DebugProtocol.Request, | |
| 60 | ): Promise<void> { | |
| 61 | return new Promise<void>((resolve, reject) => | |
| 62 | this.initializeSettings(launchArgs) | |
| 63 | .then(() => { | |
| 64 | logger.log("Launching the application"); | |
| 65 | logger.verbose( | |
| 66 | `Launching the application: ${JSON.stringify(launchArgs, null, 2)}`, | |
| 67 | ); | |
| 68 | | |
| 69 | this.appLauncher | |
| 70 | .launch(launchArgs) | |
| 71 | .then(() => { | |
| 72 | if (launchArgs.enableDebug) { | |
| 73 | launchArgs.port = | |
| 74 | launchArgs.port || | |
| 75 | this.appLauncher.getPackagerPort(launchArgs.cwd); | |
| 76 | this.attachRequest(response, launchArgs) | |
| 77 | .then(() => { | |
| 78 | resolve(); | |
| 79 | }) | |
| 80 | .catch(e => reject(e)); | |
| 81 | } else { | |
| 82 | this.sendResponse(response); | |
5514e287RedMickey6 years ago | 83 | resolve(); |
34472878RedMickey5 years ago | 84 | } |
| 85 | }) | |
| 86 | .catch(err => { | |
| 87 | reject( | |
| 88 | ErrorHelper.getInternalError( | |
| 89 | InternalErrorCode.ApplicationLaunchFailed, | |
| 90 | err.message || err, | |
| 91 | ), | |
| 92 | ); | |
| 93 | }); | |
| 94 | }) | |
| 95 | .catch(err => { | |
| 96 | reject( | |
| 97 | ErrorHelper.getInternalError( | |
| 98 | InternalErrorCode.ApplicationLaunchFailed, | |
| 99 | err.message || err, | |
| 100 | ), | |
| 101 | ); | |
| 102 | }), | |
| 103 | ).catch(err => this.showError(err, response)); | |
6e1bcd36RedMickey6 years ago | 104 | } |
| 105 | | |
34472878RedMickey5 years ago | 106 | protected async attachRequest( |
| 107 | response: DebugProtocol.AttachResponse, | |
| 108 | attachArgs: IAttachRequestArgs, | |
| 109 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 110 | request?: DebugProtocol.Request, | |
| 111 | ): Promise<void> { | |
6e1bcd36RedMickey6 years ago | 112 | let extProps = { |
| 113 | platform: { | |
| 114 | value: attachArgs.platform, | |
| 115 | isPii: false, | |
| 116 | }, | |
| 117 | }; | |
| 118 | | |
| 119 | this.previousAttachArgs = attachArgs; | |
34472878RedMickey5 years ago | 120 | return new Promise<void>((resolve, reject) => |
| 121 | this.initializeSettings(attachArgs) | |
| 122 | .then(() => { | |
| 123 | logger.log("Attaching to the application"); | |
| 124 | logger.verbose( | |
| 125 | `Attaching to the application: ${JSON.stringify(attachArgs, null, 2)}`, | |
| 126 | ); | |
| 127 | return ProjectVersionHelper.getReactNativeVersions( | |
5884987eRedMickey5 years ago | 128 | this.projectRootPath, |
34472878RedMickey5 years ago | 129 | ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform( |
| 130 | attachArgs, | |
| 131 | ), | |
| 132 | ); | |
| 133 | }) | |
| 134 | .then(versions => { | |
| 135 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( | |
| 136 | attachArgs, | |
| 137 | versions, | |
| 138 | extProps, | |
| 139 | ); | |
| 140 | | |
| 141 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| 142 | return TelemetryHelper.generate("attach", extProps, generator => { | |
| 143 | attachArgs.port = | |
| 144 | attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); | |
| 145 | return this.appLauncher | |
| 146 | .getRnCdpProxy() | |
| 147 | .stopServer() | |
| 148 | .then(() => | |
| 149 | this.appLauncher | |
| 150 | .getRnCdpProxy() | |
| 151 | .initializeServer( | |
| 152 | new RnCDPMessageHandler(), | |
| 153 | this.cdpProxyLogLevel, | |
| 154 | ), | |
| 155 | ) | |
| 156 | .then(() => this.appLauncher.getPackager().start()) | |
| 157 | .then(() => { | |
| 158 | logger.log( | |
| 159 | localize( | |
| 160 | "StartingDebuggerAppWorker", | |
| 161 | "Starting debugger app worker.", | |
| 162 | ), | |
5514e287RedMickey6 years ago | 163 | ); |
| 164 | | |
34472878RedMickey5 years ago | 165 | const sourcesStoragePath = path.join( |
| 166 | this.projectRootPath, | |
| 167 | ".vscode", | |
| 168 | ".react", | |
| 169 | ); | |
| 170 | // Create folder if not exist to avoid problems if | |
| 171 | // RN project root is not a ${workspaceFolder} | |
| 172 | mkdirp.sync(sourcesStoragePath); | |
| 173 | | |
| 174 | // If launch is invoked first time, appWorker is undefined, so create it here | |
| 175 | this.appWorker = new MultipleLifetimesAppWorker( | |
| 176 | attachArgs, | |
| 177 | sourcesStoragePath, | |
| 178 | this.projectRootPath, | |
5940f996RedMickey5 years ago | 179 | this.cancellationTokenSource.token, |
34472878RedMickey5 years ago | 180 | undefined, |
| 181 | ); | |
| 182 | this.appLauncher.setAppWorker(this.appWorker); | |
67ffa5b4RedMickey5 years ago | 183 | |
34472878RedMickey5 years ago | 184 | this.appWorker.on("connected", (port: number) => { |
| 185 | if ( | |
| 186 | this.cancellationTokenSource.token.isCancellationRequested | |
| 187 | ) { | |
| 188 | return this.appWorker?.stop(); | |
| 189 | } | |
5514e287RedMickey6 years ago | 190 | |
34472878RedMickey5 years ago | 191 | logger.log( |
| 192 | localize( | |
| 193 | "DebuggerWorkerLoadedRuntimeOnPort", | |
| 194 | "Debugger worker loaded runtime on port {0}", | |
| 195 | port, | |
| 196 | ), | |
| 197 | ); | |
5514e287RedMickey6 years ago | 198 | |
34472878RedMickey5 years ago | 199 | this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port); |
| 200 | | |
| 201 | if ( | |
| 202 | this.debugSessionStatus === | |
| 203 | DebugSessionStatus.ConnectionPending | |
| 204 | ) { | |
| 205 | return; | |
| 206 | } | |
5514e287RedMickey6 years ago | 207 | |
34472878RedMickey5 years ago | 208 | if ( |
| 209 | this.debugSessionStatus === | |
| 210 | DebugSessionStatus.FirstConnection | |
| 211 | ) { | |
| 212 | this.debugSessionStatus = | |
| 213 | DebugSessionStatus.FirstConnectionPending; | |
| 214 | this.establishDebugSession(attachArgs, resolve); | |
| 215 | } else if ( | |
| 216 | this.debugSessionStatus === | |
| 217 | DebugSessionStatus.ConnectionAllowed | |
| 218 | ) { | |
| 219 | if (this.nodeSession) { | |
| 220 | this.debugSessionStatus = | |
| 221 | DebugSessionStatus.ConnectionPending; | |
| 222 | this.nodeSession.customRequest(this.terminateCommand); | |
| 223 | } | |
5514e287RedMickey6 years ago | 224 | } |
34472878RedMickey5 years ago | 225 | }); |
| 226 | if (this.cancellationTokenSource.token.isCancellationRequested) { | |
| 227 | return this.appWorker.stop(); | |
5514e287RedMickey6 years ago | 228 | } |
34472878RedMickey5 years ago | 229 | return this.appWorker.start(); |
5514e287RedMickey6 years ago | 230 | }); |
34472878RedMickey5 years ago | 231 | }); |
| 232 | }) | |
| 233 | .catch(err => { | |
| 234 | reject( | |
| 235 | ErrorHelper.getInternalError( | |
| 236 | InternalErrorCode.CouldNotAttachToDebugger, | |
| 237 | err.message || err, | |
| 238 | ), | |
| 239 | ); | |
| 240 | }), | |
| 241 | ).catch(err => this.showError(err, response)); | |
6e1bcd36RedMickey6 years ago | 242 | } |
| 243 | | |
34472878RedMickey5 years ago | 244 | protected async disconnectRequest( |
| 245 | response: DebugProtocol.DisconnectResponse, | |
| 246 | args: DebugProtocol.DisconnectArguments, | |
| 247 | request?: DebugProtocol.Request, | |
| 248 | ): Promise<void> { | |
6e1bcd36RedMickey6 years ago | 249 | // The client is about to disconnect so first we need to stop app worker |
| 250 | if (this.appWorker) { | |
| 251 | this.appWorker.stop(); | |
| 252 | } | |
| 253 | | |
6e491635RedMickey6 years ago | 254 | this.onDidStartDebugSessionHandler.dispose(); |
| 255 | this.onDidTerminateDebugSessionHandler.dispose(); | |
| 256 | | |
6e1bcd36RedMickey6 years ago | 257 | super.disconnectRequest(response, args, request); |
4c757eebRedMickey6 years ago | 258 | } |
| 259 | | |
34472878RedMickey5 years ago | 260 | protected establishDebugSession( |
| 261 | attachArgs: IAttachRequestArgs, | |
| 262 | resolve?: (value?: void | PromiseLike<void> | undefined) => void, | |
| 263 | ): void { | |
1bdccb66RedMickey6 years ago | 264 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN( |
| 265 | attachArgs, | |
| 266 | this.appLauncher.getCdpProxyPort(), | |
34472878RedMickey5 years ago | 267 | this.session.id, |
1bdccb66RedMickey6 years ago | 268 | ); |
a6562589RedMickey6 years ago | 269 | |
34472878RedMickey5 years ago | 270 | vscode.debug |
| 271 | .startDebugging(this.appLauncher.getWorkspaceFolder(), attachConfiguration, { | |
ebbd64f1RedMickey6 years ago | 272 | parentSession: this.session, |
| 273 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
34472878RedMickey5 years ago | 274 | }) |
| 275 | .then( | |
| 276 | (childDebugSessionStarted: boolean) => { | |
| 277 | if (childDebugSessionStarted) { | |
| 278 | this.debugSessionStatus = DebugSessionStatus.ConnectionDone; | |
| 279 | this.setConnectionAllowedIfPossible(); | |
| 280 | if (resolve) { | |
| 281 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; | |
| 282 | resolve(); | |
| 283 | } | |
| 284 | } else { | |
| 285 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; | |
| 286 | this.setConnectionAllowedIfPossible(); | |
| 287 | this.resetFirstConnectionStatus(); | |
| 288 | throw new Error("Cannot start child debug session"); | |
| 289 | } | |
| 290 | }, | |
| 291 | err => { | |
| 292 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; | |
| 293 | this.setConnectionAllowedIfPossible(); | |
| 294 | this.resetFirstConnectionStatus(); | |
| 295 | throw err; | |
| 296 | }, | |
| 297 | ); | |
4c757eebRedMickey6 years ago | 298 | } |
6e1bcd36RedMickey6 years ago | 299 | |
4c757eebRedMickey6 years ago | 300 | private handleStartDebugSession(debugSession: vscode.DebugSession) { |
| 301 | if ( | |
34472878RedMickey5 years ago | 302 | debugSession.configuration.rnDebugSessionId === this.session.id && |
| 303 | debugSession.type === this.pwaNodeSessionName | |
4c757eebRedMickey6 years ago | 304 | ) { |
| 305 | this.nodeSession = debugSession; | |
| 306 | } | |
| 307 | } | |
| 308 | | |
| 309 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { | |
| 310 | if ( | |
34472878RedMickey5 years ago | 311 | debugSession.configuration.rnDebugSessionId === this.session.id && |
| 312 | debugSession.type === this.pwaNodeSessionName | |
4c757eebRedMickey6 years ago | 313 | ) { |
ebbd64f1RedMickey6 years ago | 314 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { |
5d47053fRedMickey6 years ago | 315 | this.establishDebugSession(this.previousAttachArgs); |
ebbd64f1RedMickey6 years ago | 316 | } else { |
a2ddbba5RedMickey5 years ago | 317 | vscode.commands.executeCommand(this.stopCommand, this.session); |
ebbd64f1RedMickey6 years ago | 318 | } |
4c757eebRedMickey6 years ago | 319 | } |
| 320 | } | |
| 321 | | |
| 322 | private setConnectionAllowedIfPossible(): void { | |
| 323 | if ( | |
34472878RedMickey5 years ago | 324 | this.debugSessionStatus === DebugSessionStatus.ConnectionDone || |
| 325 | this.debugSessionStatus === DebugSessionStatus.ConnectionFailed | |
4c757eebRedMickey6 years ago | 326 | ) { |
| 327 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; | |
| 328 | } | |
| 329 | } | |
| 330 | | |
| 331 | private resetFirstConnectionStatus(): void { | |
| 332 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) { | |
| 333 | this.debugSessionStatus = DebugSessionStatus.FirstConnection; | |
| 334 | } | |
| 335 | } | |
6e1bcd36RedMickey6 years ago | 336 | } |