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