microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/rnDebugSession.ts
242lines · 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 vscode from "vscode"; | |
| 5 | import * as path from "path"; | |
| 6 | import * as mkdirp from "mkdirp"; | |
2c19da7fRedMickey6 years ago | 7 | import { logger } from "vscode-debugadapter"; |
6e1bcd36RedMickey6 years ago | 8 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 9 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; | |
| 10 | import { TelemetryHelper } from "../common/telemetryHelper"; | |
| 11 | import { MultipleLifetimesAppWorker } from "./appWorker"; | |
a6562589RedMickey6 years ago | 12 | import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler"; |
2c19da7fRedMickey6 years ago | 13 | import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase"; |
1bdccb66RedMickey6 years ago | 14 | import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter"; |
5514e287RedMickey6 years ago | 15 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 16 | import { InternalErrorCode } from "../common/error/internalErrorCode"; | |
6e1bcd36RedMickey6 years ago | 17 | import * as nls from "vscode-nls"; |
2d8af448Yuri Skorokhodov6 years ago | 18 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
6e1bcd36RedMickey6 years ago | 19 | const localize = nls.loadMessageBundle(); |
| 20 | | |
2c19da7fRedMickey6 years ago | 21 | export class RNDebugSession extends DebugSessionBase { |
6e1bcd36RedMickey6 years ago | 22 | |
4c757eebRedMickey6 years ago | 23 | private readonly terminateCommand: string; |
f872f4d5RedMickey6 years ago | 24 | |
e23d1841RedMickey6 years ago | 25 | private appWorker: MultipleLifetimesAppWorker | null; |
4c757eebRedMickey6 years ago | 26 | private nodeSession: vscode.DebugSession | null; |
6e491635RedMickey6 years ago | 27 | private onDidStartDebugSessionHandler: vscode.Disposable; |
| 28 | private onDidTerminateDebugSessionHandler: vscode.Disposable; | |
6e1bcd36RedMickey6 years ago | 29 | |
2c19da7fRedMickey6 years ago | 30 | constructor(session: vscode.DebugSession) { |
| 31 | super(session); | |
4c757eebRedMickey6 years ago | 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 | |
e23d1841RedMickey6 years ago | 37 | this.appWorker = null; |
| 38 | | |
6e491635RedMickey6 years ago | 39 | this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession( |
4c757eebRedMickey6 years ago | 40 | this.handleStartDebugSession.bind(this) |
| 41 | ); | |
| 42 | | |
6e491635RedMickey6 years ago | 43 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( |
4c757eebRedMickey6 years ago | 44 | this.handleTerminateDebugSession.bind(this) |
| 45 | ); | |
6e1bcd36RedMickey6 years ago | 46 | } |
| 47 | | |
984ca036RedMickey6 years ago | 48 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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(() => { | |
5514e287RedMickey6 years ago | 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); | |
6e1bcd36RedMickey6 years ago | 63 | resolve(); |
5514e287RedMickey6 years ago | 64 | } |
6e1bcd36RedMickey6 years ago | 65 | }) |
| 66 | .catch((err) => { | |
5514e287RedMickey6 years ago | 67 | reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err)); |
6e1bcd36RedMickey6 years ago | 68 | }); |
5514e287RedMickey6 years ago | 69 | }) |
| 70 | .catch((err) => { | |
| 71 | reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err)); | |
| 72 | }) | |
| 73 | ) | |
| 74 | .catch(err => this.showError(err, response)); | |
6e1bcd36RedMickey6 years ago | 75 | } |
| 76 | | |
984ca036RedMickey6 years ago | 77 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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)}`); | |
5514e287RedMickey6 years ago | 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)) | |
9fc07913JiglioNero5 years ago | 101 | .then(() => this.appLauncher.getPackager().start()) |
5514e287RedMickey6 years ago | 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) => { | |
67ffa5b4RedMickey5 years ago | 120 | if (this.cancellationTokenSource.token.isCancellationRequested) { |
| 121 | return this.appWorker?.stop(); | |
| 122 | } | |
| 123 | | |
5514e287RedMickey6 years ago | 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 | }); | |
67ffa5b4RedMickey5 years ago | 142 | if (this.cancellationTokenSource.token.isCancellationRequested) { |
| 143 | return this.appWorker.stop(); | |
| 144 | } | |
5514e287RedMickey6 years ago | 145 | return this.appWorker.start(); |
6e1bcd36RedMickey6 years ago | 146 | }); |
5514e287RedMickey6 years ago | 147 | }); |
| 148 | }) | |
| 149 | .catch((err) => { | |
| 150 | reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err)); | |
| 151 | }) | |
| 152 | ) | |
e23d1841RedMickey6 years ago | 153 | .catch(err => this.showError(err, response)); |
6e1bcd36RedMickey6 years ago | 154 | } |
| 155 | | |
984ca036RedMickey6 years ago | 156 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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 | | |
6e491635RedMickey6 years ago | 162 | this.onDidStartDebugSessionHandler.dispose(); |
| 163 | this.onDidTerminateDebugSessionHandler.dispose(); | |
| 164 | | |
6e1bcd36RedMickey6 years ago | 165 | super.disconnectRequest(response, args, request); |
4c757eebRedMickey6 years ago | 166 | } |
| 167 | | |
5d47053fRedMickey6 years ago | 168 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
1bdccb66RedMickey6 years ago | 169 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN( |
| 170 | attachArgs, | |
| 171 | this.appLauncher.getCdpProxyPort(), | |
| 172 | this.session.id | |
| 173 | ); | |
a6562589RedMickey6 years ago | 174 | |
| 175 | vscode.debug.startDebugging( | |
| 176 | this.appLauncher.getWorkspaceFolder(), | |
1bdccb66RedMickey6 years ago | 177 | attachConfiguration, |
ebbd64f1RedMickey6 years ago | 178 | { |
| 179 | parentSession: this.session, | |
| 180 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
| 181 | } | |
a6562589RedMickey6 years ago | 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(); | |
4c757eebRedMickey6 years ago | 190 | } |
a6562589RedMickey6 years ago | 191 | } else { |
4c757eebRedMickey6 years ago | 192 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; |
| 193 | this.setConnectionAllowedIfPossible(); | |
| 194 | this.resetFirstConnectionStatus(); | |
a6562589RedMickey6 years ago | 195 | throw new Error("Cannot start child debug session"); |
| 196 | } | |
| 197 | }, | |
| 198 | err => { | |
| 199 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; | |
| 200 | this.setConnectionAllowedIfPossible(); | |
4c757eebRedMickey6 years ago | 201 | this.resetFirstConnectionStatus(); |
a6562589RedMickey6 years ago | 202 | throw err; |
| 203 | }); | |
4c757eebRedMickey6 years ago | 204 | } |
6e1bcd36RedMickey6 years ago | 205 | |
4c757eebRedMickey6 years ago | 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 | ) { | |
ebbd64f1RedMickey6 years ago | 220 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { |
5d47053fRedMickey6 years ago | 221 | this.establishDebugSession(this.previousAttachArgs); |
ebbd64f1RedMickey6 years ago | 222 | } else { |
| 223 | this.session.customRequest(this.disconnectCommand, {forcedStop: true}); | |
| 224 | } | |
4c757eebRedMickey6 years ago | 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 | } | |
6e1bcd36RedMickey6 years ago | 242 | } |