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