microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/rnDebugSession.ts
233lines · 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"; |
6e1bcd36RedMickey6 years ago | 14 | import * as nls from "vscode-nls"; |
| 15 | const localize = nls.loadMessageBundle(); | |
| 16 | | |
2c19da7fRedMickey6 years ago | 17 | export class RNDebugSession extends DebugSessionBase { |
6e1bcd36RedMickey6 years ago | 18 | |
4c757eebRedMickey6 years ago | 19 | private readonly terminateCommand: string; |
f872f4d5RedMickey6 years ago | 20 | |
e23d1841RedMickey6 years ago | 21 | private appWorker: MultipleLifetimesAppWorker | null; |
4c757eebRedMickey6 years ago | 22 | private nodeSession: vscode.DebugSession | null; |
6e491635RedMickey6 years ago | 23 | private onDidStartDebugSessionHandler: vscode.Disposable; |
| 24 | private onDidTerminateDebugSessionHandler: vscode.Disposable; | |
6e1bcd36RedMickey6 years ago | 25 | |
2c19da7fRedMickey6 years ago | 26 | constructor(session: vscode.DebugSession) { |
| 27 | super(session); | |
4c757eebRedMickey6 years ago | 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 | |
e23d1841RedMickey6 years ago | 33 | this.appWorker = null; |
| 34 | | |
6e491635RedMickey6 years ago | 35 | this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession( |
4c757eebRedMickey6 years ago | 36 | this.handleStartDebugSession.bind(this) |
| 37 | ); | |
| 38 | | |
6e491635RedMickey6 years ago | 39 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( |
4c757eebRedMickey6 years ago | 40 | this.handleTerminateDebugSession.bind(this) |
| 41 | ); | |
6e1bcd36RedMickey6 years ago | 42 | } |
| 43 | | |
984ca036RedMickey6 years ago | 44 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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) => { | |
2c19da7fRedMickey6 years ago | 61 | logger.error("An error occurred while launching the application. " + err.message || err); |
6e1bcd36RedMickey6 years ago | 62 | reject(err); |
| 63 | }); | |
984ca036RedMickey6 years ago | 64 | })) |
e23d1841RedMickey6 years ago | 65 | .catch(err => this.showError(err, response)); |
6e1bcd36RedMickey6 years ago | 66 | } |
| 67 | | |
984ca036RedMickey6 years ago | 68 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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) => { | |
a324603aRedMickey6 years ago | 88 | attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); |
984ca036RedMickey6 years ago | 89 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 90 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel)) | |
f872f4d5RedMickey6 years ago | 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, | |
a324603aRedMickey6 years ago | 104 | undefined |
| 105 | ); | |
7e74daf7Yuri Skorokhodov6 years ago | 106 | this.appLauncher.setAppWorker(this.appWorker); |
f872f4d5RedMickey6 years ago | 107 | |
| 108 | this.appWorker.on("connected", (port: number) => { | |
| 109 | logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port)); | |
| 110 | | |
a6562589RedMickey6 years ago | 111 | this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port); |
4c757eebRedMickey6 years ago | 112 | |
| 113 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { | |
| 114 | return; | |
| 115 | } | |
d9c9ddcbRedMickey6 years ago | 116 | |
4c757eebRedMickey6 years ago | 117 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) { |
| 118 | this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending; | |
5d47053fRedMickey6 years ago | 119 | this.establishDebugSession(attachArgs, resolve); |
4c757eebRedMickey6 years ago | 120 | } else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) { |
| 121 | if (this.nodeSession) { | |
| 122 | this.debugSessionStatus = DebugSessionStatus.ConnectionPending; | |
| 123 | this.nodeSession.customRequest(this.terminateCommand); | |
| 124 | } | |
f872f4d5RedMickey6 years ago | 125 | } |
| 126 | }); | |
| 127 | return this.appWorker.start(); | |
6e1bcd36RedMickey6 years ago | 128 | }); |
| 129 | }) | |
| 130 | .catch((err) => { | |
| 131 | logger.error("An error occurred while attaching to the debugger. " + err.message || err); | |
| 132 | reject(err); | |
| 133 | }); | |
| 134 | }); | |
984ca036RedMickey6 years ago | 135 | })) |
e23d1841RedMickey6 years ago | 136 | .catch(err => this.showError(err, response)); |
6e1bcd36RedMickey6 years ago | 137 | } |
| 138 | | |
984ca036RedMickey6 years ago | 139 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 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 | | |
6e491635RedMickey6 years ago | 145 | this.onDidStartDebugSessionHandler.dispose(); |
| 146 | this.onDidTerminateDebugSessionHandler.dispose(); | |
| 147 | | |
6e1bcd36RedMickey6 years ago | 148 | super.disconnectRequest(response, args, request); |
4c757eebRedMickey6 years ago | 149 | } |
| 150 | | |
5d47053fRedMickey6 years ago | 151 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
a6562589RedMickey6 years ago | 152 | const attachArguments = { |
| 153 | type: "pwa-node", | |
| 154 | request: "attach", | |
| 155 | name: "Attach", | |
| 156 | continueOnAttach: true, | |
| 157 | port: this.appLauncher.getCdpProxyPort(), | |
| 158 | smartStep: false, | |
5d47053fRedMickey6 years ago | 159 | skipFiles: attachArgs.skipFiles || [], |
a6562589RedMickey6 years ago | 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, | |
ebbd64f1RedMickey6 years ago | 169 | { |
| 170 | parentSession: this.session, | |
| 171 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
| 172 | } | |
a6562589RedMickey6 years ago | 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(); | |
4c757eebRedMickey6 years ago | 181 | } |
a6562589RedMickey6 years ago | 182 | } else { |
4c757eebRedMickey6 years ago | 183 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; |
| 184 | this.setConnectionAllowedIfPossible(); | |
| 185 | this.resetFirstConnectionStatus(); | |
a6562589RedMickey6 years ago | 186 | throw new Error("Cannot start child debug session"); |
| 187 | } | |
| 188 | }, | |
| 189 | err => { | |
| 190 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; | |
| 191 | this.setConnectionAllowedIfPossible(); | |
4c757eebRedMickey6 years ago | 192 | this.resetFirstConnectionStatus(); |
a6562589RedMickey6 years ago | 193 | throw err; |
| 194 | }); | |
4c757eebRedMickey6 years ago | 195 | } |
6e1bcd36RedMickey6 years ago | 196 | |
4c757eebRedMickey6 years ago | 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 | ) { | |
ebbd64f1RedMickey6 years ago | 211 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { |
5d47053fRedMickey6 years ago | 212 | this.establishDebugSession(this.previousAttachArgs); |
ebbd64f1RedMickey6 years ago | 213 | } else { |
| 214 | this.session.customRequest(this.disconnectCommand, {forcedStop: true}); | |
| 215 | } | |
4c757eebRedMickey6 years ago | 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 | } | |
6e1bcd36RedMickey6 years ago | 233 | } |