microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/rnDebugSession.ts
228lines · modecode
| 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"; |
| 7 | import { logger } from "vscode-debugadapter"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 9 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; |
| 10 | import { TelemetryHelper } from "../common/telemetryHelper"; |
| 11 | import { MultipleLifetimesAppWorker } from "./appWorker"; |
| 12 | import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler"; |
| 13 | import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase"; |
| 14 | import * as nls from "vscode-nls"; |
| 15 | const localize = nls.loadMessageBundle(); |
| 16 | |
| 17 | export class RNDebugSession extends DebugSessionBase { |
| 18 | |
| 19 | private readonly terminateCommand: string; |
| 20 | private readonly pwaNodeSessionName: string; |
| 21 | |
| 22 | private appWorker: MultipleLifetimesAppWorker | null; |
| 23 | private nodeSession: vscode.DebugSession | null; |
| 24 | private onDidStartDebugSessionHandler: vscode.Disposable; |
| 25 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
| 26 | |
| 27 | constructor(session: vscode.DebugSession) { |
| 28 | super(session); |
| 29 | |
| 30 | // constants definition |
| 31 | 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 |
| 32 | this.pwaNodeSessionName = "pwa-node"; // the name of node debug session created by js-debug extension |
| 33 | |
| 34 | // variables definition |
| 35 | this.appWorker = null; |
| 36 | |
| 37 | this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession( |
| 38 | this.handleStartDebugSession.bind(this) |
| 39 | ); |
| 40 | |
| 41 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( |
| 42 | this.handleTerminateDebugSession.bind(this) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
| 47 | return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs) |
| 48 | .then(() => { |
| 49 | logger.log("Launching the application"); |
| 50 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`); |
| 51 | |
| 52 | this.appLauncher.launch(launchArgs) |
| 53 | .then(() => { |
| 54 | return this.appLauncher.getPackagerPort(launchArgs.cwd); |
| 55 | }) |
| 56 | .then((packagerPort: number) => { |
| 57 | launchArgs.port = launchArgs.port || packagerPort; |
| 58 | this.attachRequest(response, launchArgs).then(() => { |
| 59 | resolve(); |
| 60 | }).catch((e) => reject(e)); |
| 61 | }) |
| 62 | .catch((err) => { |
| 63 | logger.error("An error occurred while launching the application. " + err.message || err); |
| 64 | reject(err); |
| 65 | }); |
| 66 | })) |
| 67 | .catch(err => this.showError(err, response)); |
| 68 | } |
| 69 | |
| 70 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
| 71 | let extProps = { |
| 72 | platform: { |
| 73 | value: attachArgs.platform, |
| 74 | isPii: false, |
| 75 | }, |
| 76 | }; |
| 77 | |
| 78 | this.previousAttachArgs = attachArgs; |
| 79 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) |
| 80 | .then(() => { |
| 81 | logger.log("Attaching to the application"); |
| 82 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`); |
| 83 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true) |
| 84 | .then(versions => { |
| 85 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 86 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { |
| 87 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 88 | } |
| 89 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 90 | attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); |
| 91 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 92 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel)) |
| 93 | .then(() => { |
| 94 | logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker.")); |
| 95 | |
| 96 | const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); |
| 97 | // Create folder if not exist to avoid problems if |
| 98 | // RN project root is not a ${workspaceFolder} |
| 99 | mkdirp.sync(sourcesStoragePath); |
| 100 | |
| 101 | // If launch is invoked first time, appWorker is undefined, so create it here |
| 102 | this.appWorker = new MultipleLifetimesAppWorker( |
| 103 | attachArgs, |
| 104 | sourcesStoragePath, |
| 105 | this.projectRootPath, |
| 106 | undefined |
| 107 | ); |
| 108 | this.appLauncher.setAppWorker(this.appWorker); |
| 109 | |
| 110 | this.appWorker.on("connected", (port: number) => { |
| 111 | logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port)); |
| 112 | |
| 113 | this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port); |
| 114 | |
| 115 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) { |
| 120 | this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending; |
| 121 | this.establishDebugSession(resolve); |
| 122 | } else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) { |
| 123 | if (this.nodeSession) { |
| 124 | this.debugSessionStatus = DebugSessionStatus.ConnectionPending; |
| 125 | this.nodeSession.customRequest(this.terminateCommand); |
| 126 | } |
| 127 | } |
| 128 | }); |
| 129 | return this.appWorker.start(); |
| 130 | }); |
| 131 | }) |
| 132 | .catch((err) => { |
| 133 | logger.error("An error occurred while attaching to the debugger. " + err.message || err); |
| 134 | reject(err); |
| 135 | }); |
| 136 | }); |
| 137 | })) |
| 138 | .catch(err => this.showError(err, response)); |
| 139 | } |
| 140 | |
| 141 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
| 142 | // The client is about to disconnect so first we need to stop app worker |
| 143 | if (this.appWorker) { |
| 144 | this.appWorker.stop(); |
| 145 | } |
| 146 | |
| 147 | this.onDidStartDebugSessionHandler.dispose(); |
| 148 | this.onDidTerminateDebugSessionHandler.dispose(); |
| 149 | |
| 150 | super.disconnectRequest(response, args, request); |
| 151 | } |
| 152 | |
| 153 | protected establishDebugSession(resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
| 154 | const attachArguments = { |
| 155 | type: "pwa-node", |
| 156 | request: "attach", |
| 157 | name: "Attach", |
| 158 | continueOnAttach: true, |
| 159 | port: this.appLauncher.getCdpProxyPort(), |
| 160 | smartStep: false, |
| 161 | // The unique identifier of the debug session. It is used to distinguish React Native extension's |
| 162 | // debug sessions from other ones. So we can save and process only the extension's debug sessions |
| 163 | // in vscode.debug API methods "onDidStartDebugSession" and "onDidTerminateDebugSession". |
| 164 | rnDebugSessionId: this.session.id, |
| 165 | }; |
| 166 | |
| 167 | vscode.debug.startDebugging( |
| 168 | this.appLauncher.getWorkspaceFolder(), |
| 169 | attachArguments, |
| 170 | this.session |
| 171 | ) |
| 172 | .then((childDebugSessionStarted: boolean) => { |
| 173 | if (childDebugSessionStarted) { |
| 174 | this.debugSessionStatus = DebugSessionStatus.ConnectionDone; |
| 175 | this.setConnectionAllowedIfPossible(); |
| 176 | if (resolve) { |
| 177 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; |
| 178 | resolve(); |
| 179 | } |
| 180 | } else { |
| 181 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; |
| 182 | this.setConnectionAllowedIfPossible(); |
| 183 | this.resetFirstConnectionStatus(); |
| 184 | throw new Error("Cannot start child debug session"); |
| 185 | } |
| 186 | }, |
| 187 | err => { |
| 188 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; |
| 189 | this.setConnectionAllowedIfPossible(); |
| 190 | this.resetFirstConnectionStatus(); |
| 191 | throw err; |
| 192 | }); |
| 193 | } |
| 194 | |
| 195 | private handleStartDebugSession(debugSession: vscode.DebugSession) { |
| 196 | if ( |
| 197 | debugSession.configuration.rnDebugSessionId === this.session.id |
| 198 | && debugSession.type === this.pwaNodeSessionName |
| 199 | ) { |
| 200 | this.nodeSession = debugSession; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { |
| 205 | if ( |
| 206 | debugSession.configuration.rnDebugSessionId === this.session.id |
| 207 | && this.debugSessionStatus === DebugSessionStatus.ConnectionPending |
| 208 | && debugSession.type === this.pwaNodeSessionName |
| 209 | ) { |
| 210 | this.establishDebugSession(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private setConnectionAllowedIfPossible(): void { |
| 215 | if ( |
| 216 | this.debugSessionStatus === DebugSessionStatus.ConnectionDone |
| 217 | || this.debugSessionStatus === DebugSessionStatus.ConnectionFailed |
| 218 | ) { |
| 219 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private resetFirstConnectionStatus(): void { |
| 224 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) { |
| 225 | this.debugSessionStatus = DebugSessionStatus.FirstConnection; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |