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