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