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