microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/rnDebugSession.ts
240lines · 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)}`); | |
341dba36Yuri Skorokhodov5 years ago | 90 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, ProjectVersionHelper.generateAdditionalPackagesToCheckByPlatform(attachArgs)); |
5514e287RedMickey6 years ago | 91 | }) |
| 92 | .then(versions => { | |
341dba36Yuri Skorokhodov5 years ago | 93 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(attachArgs, versions, extProps); |
| 94 | | |
5514e287RedMickey6 years ago | 95 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 96 | attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); | |
| 97 | return this.appLauncher.getRnCdpProxy().stopServer() | |
| 98 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel)) | |
9fc07913JiglioNero5 years ago | 99 | .then(() => this.appLauncher.getPackager().start()) |
5514e287RedMickey6 years ago | 100 | .then(() => { |
| 101 | logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker.")); | |
| 102 | | |
| 103 | const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react"); | |
| 104 | // Create folder if not exist to avoid problems if | |
| 105 | // RN project root is not a ${workspaceFolder} | |
| 106 | mkdirp.sync(sourcesStoragePath); | |
| 107 | | |
| 108 | // If launch is invoked first time, appWorker is undefined, so create it here | |
| 109 | this.appWorker = new MultipleLifetimesAppWorker( | |
| 110 | attachArgs, | |
| 111 | sourcesStoragePath, | |
| 112 | this.projectRootPath, | |
| 113 | undefined | |
| 114 | ); | |
| 115 | this.appLauncher.setAppWorker(this.appWorker); | |
| 116 | | |
| 117 | this.appWorker.on("connected", (port: number) => { | |
67ffa5b4RedMickey6 years ago | 118 | if (this.cancellationTokenSource.token.isCancellationRequested) { |
| 119 | return this.appWorker?.stop(); | |
| 120 | } | |
| 121 | | |
5514e287RedMickey6 years ago | 122 | logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port)); |
| 123 | | |
| 124 | this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port); | |
| 125 | | |
| 126 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { | |
| 127 | return; | |
| 128 | } | |
| 129 | | |
| 130 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) { | |
| 131 | this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending; | |
| 132 | this.establishDebugSession(attachArgs, resolve); | |
| 133 | } else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) { | |
| 134 | if (this.nodeSession) { | |
| 135 | this.debugSessionStatus = DebugSessionStatus.ConnectionPending; | |
| 136 | this.nodeSession.customRequest(this.terminateCommand); | |
| 137 | } | |
| 138 | } | |
| 139 | }); | |
67ffa5b4RedMickey6 years ago | 140 | if (this.cancellationTokenSource.token.isCancellationRequested) { |
| 141 | return this.appWorker.stop(); | |
| 142 | } | |
5514e287RedMickey6 years ago | 143 | return this.appWorker.start(); |
6e1bcd36RedMickey6 years ago | 144 | }); |
5514e287RedMickey6 years ago | 145 | }); |
| 146 | }) | |
| 147 | .catch((err) => { | |
| 148 | reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err)); | |
| 149 | }) | |
| 150 | ) | |
e23d1841RedMickey6 years ago | 151 | .catch(err => this.showError(err, response)); |
6e1bcd36RedMickey6 years ago | 152 | } |
| 153 | | |
984ca036RedMickey6 years ago | 154 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
6e1bcd36RedMickey6 years ago | 155 | // The client is about to disconnect so first we need to stop app worker |
| 156 | if (this.appWorker) { | |
| 157 | this.appWorker.stop(); | |
| 158 | } | |
| 159 | | |
6e491635RedMickey6 years ago | 160 | this.onDidStartDebugSessionHandler.dispose(); |
| 161 | this.onDidTerminateDebugSessionHandler.dispose(); | |
| 162 | | |
6e1bcd36RedMickey6 years ago | 163 | super.disconnectRequest(response, args, request); |
4c757eebRedMickey6 years ago | 164 | } |
| 165 | | |
5d47053fRedMickey6 years ago | 166 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
1bdccb66RedMickey6 years ago | 167 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN( |
| 168 | attachArgs, | |
| 169 | this.appLauncher.getCdpProxyPort(), | |
| 170 | this.session.id | |
| 171 | ); | |
a6562589RedMickey6 years ago | 172 | |
| 173 | vscode.debug.startDebugging( | |
| 174 | this.appLauncher.getWorkspaceFolder(), | |
1bdccb66RedMickey6 years ago | 175 | attachConfiguration, |
ebbd64f1RedMickey6 years ago | 176 | { |
| 177 | parentSession: this.session, | |
| 178 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, | |
| 179 | } | |
a6562589RedMickey6 years ago | 180 | ) |
| 181 | .then((childDebugSessionStarted: boolean) => { | |
| 182 | if (childDebugSessionStarted) { | |
| 183 | this.debugSessionStatus = DebugSessionStatus.ConnectionDone; | |
| 184 | this.setConnectionAllowedIfPossible(); | |
| 185 | if (resolve) { | |
| 186 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; | |
| 187 | resolve(); | |
4c757eebRedMickey6 years ago | 188 | } |
a6562589RedMickey6 years ago | 189 | } else { |
4c757eebRedMickey6 years ago | 190 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; |
| 191 | this.setConnectionAllowedIfPossible(); | |
| 192 | this.resetFirstConnectionStatus(); | |
a6562589RedMickey6 years ago | 193 | throw new Error("Cannot start child debug session"); |
| 194 | } | |
| 195 | }, | |
| 196 | err => { | |
| 197 | this.debugSessionStatus = DebugSessionStatus.ConnectionFailed; | |
| 198 | this.setConnectionAllowedIfPossible(); | |
4c757eebRedMickey6 years ago | 199 | this.resetFirstConnectionStatus(); |
a6562589RedMickey6 years ago | 200 | throw err; |
| 201 | }); | |
4c757eebRedMickey6 years ago | 202 | } |
6e1bcd36RedMickey6 years ago | 203 | |
4c757eebRedMickey6 years ago | 204 | private handleStartDebugSession(debugSession: vscode.DebugSession) { |
| 205 | if ( | |
| 206 | debugSession.configuration.rnDebugSessionId === this.session.id | |
| 207 | && debugSession.type === this.pwaNodeSessionName | |
| 208 | ) { | |
| 209 | this.nodeSession = debugSession; | |
| 210 | } | |
| 211 | } | |
| 212 | | |
| 213 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { | |
| 214 | if ( | |
| 215 | debugSession.configuration.rnDebugSessionId === this.session.id | |
| 216 | && debugSession.type === this.pwaNodeSessionName | |
| 217 | ) { | |
ebbd64f1RedMickey6 years ago | 218 | if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) { |
5d47053fRedMickey6 years ago | 219 | this.establishDebugSession(this.previousAttachArgs); |
ebbd64f1RedMickey6 years ago | 220 | } else { |
a2ddbba5RedMickey5 years ago | 221 | vscode.commands.executeCommand(this.stopCommand, this.session); |
ebbd64f1RedMickey6 years ago | 222 | } |
4c757eebRedMickey6 years ago | 223 | } |
| 224 | } | |
| 225 | | |
| 226 | private setConnectionAllowedIfPossible(): void { | |
| 227 | if ( | |
| 228 | this.debugSessionStatus === DebugSessionStatus.ConnectionDone | |
| 229 | || this.debugSessionStatus === DebugSessionStatus.ConnectionFailed | |
| 230 | ) { | |
| 231 | this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed; | |
| 232 | } | |
| 233 | } | |
| 234 | | |
| 235 | private resetFirstConnectionStatus(): void { | |
| 236 | if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) { | |
| 237 | this.debugSessionStatus = DebugSessionStatus.FirstConnection; | |
| 238 | } | |
| 239 | } | |
6e1bcd36RedMickey6 years ago | 240 | } |