microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
196lines · 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 fs from "fs"; |
| 5 | import * as path from "path"; |
| 6 | import * as http from "http"; |
| 7 | |
| 8 | import {Telemetry} from "../common/telemetry"; |
| 9 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 10 | import {RemoteExtension} from "../common/remoteExtension"; |
| 11 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
| 12 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 13 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 14 | import {ExtensionTelemetryReporter, NullTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters"; |
| 15 | |
| 16 | // These typings do not reflect the typings as intended to be used |
| 17 | // but rather as they exist in truth, so we can reach into the internals |
| 18 | // and access what we need. |
| 19 | declare module VSCodeDebugAdapter { |
| 20 | class DebugSession { |
| 21 | public static run: Function; |
| 22 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 23 | public start(input: any, output: any): void; |
| 24 | public launchRequest(response: any, args: any): void; |
| 25 | public disconnectRequest(response: any, args: any): void; |
| 26 | } |
| 27 | class InitializedEvent { |
| 28 | constructor(); |
| 29 | } |
| 30 | class OutputEvent { |
| 31 | constructor(message: string, destination?: string); |
| 32 | } |
| 33 | class TerminatedEvent { |
| 34 | constructor(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | declare class SourceMaps { |
| 39 | public _sourceToGeneratedMaps: {}; |
| 40 | public _generatedToSourceMaps: {}; |
| 41 | public _allSourceMaps: {}; |
| 42 | } |
| 43 | |
| 44 | declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession { |
| 45 | public _sourceMaps: SourceMaps; |
| 46 | } |
| 47 | |
| 48 | interface ILaunchArgs { |
| 49 | platform: string; |
| 50 | target?: string; |
| 51 | internalDebuggerPort?: any; |
| 52 | iosProjectPath?: string; |
| 53 | args: string[]; |
| 54 | logCatArguments: any; |
| 55 | program: string; |
| 56 | } |
| 57 | |
| 58 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
| 59 | |
| 60 | function bailOut(reason: string): void { |
| 61 | // Things have gone wrong in initialization: Report the error to telemetry and exit |
| 62 | TelemetryHelper.sendSimpleEvent(reason); |
| 63 | process.exit(1); |
| 64 | } |
| 65 | |
| 66 | function parseLogCatArguments(userProvidedLogCatArguments: any) { |
| 67 | return Array.isArray(userProvidedLogCatArguments) |
| 68 | ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments |
| 69 | : userProvidedLogCatArguments; // If not, we leave it as-is |
| 70 | } |
| 71 | |
| 72 | function isNullOrUndefined(value: any): boolean { |
| 73 | return typeof value === "undefined" || value === null; |
| 74 | } |
| 75 | |
| 76 | let projectRootPath: string = null; |
| 77 | |
| 78 | // Enable telemetry |
| 79 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); |
| 80 | const appName = "react-native-debug-adapter"; |
| 81 | new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version, |
| 82 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), telemetryReporter, () => { |
| 83 | let nodeDebugFolder: string; |
| 84 | let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; |
| 85 | |
| 86 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 87 | // If it fails, we must not have been in a react native project |
| 88 | try { |
| 89 | /* tslint:disable:no-var-requires */ |
| 90 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; |
| 91 | vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); |
| 92 | /* tslint:enable:no-var-requires */ |
| 93 | } catch (e) { |
| 94 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 95 | bailOut("cannotFindDebugAdapter"); |
| 96 | } |
| 97 | |
| 98 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 99 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; |
| 100 | vscodeDebugAdapterPackage.DebugSession.run = function() { }; |
| 101 | |
| 102 | let nodeDebug: { NodeDebugSession: typeof NodeDebugSession }; |
| 103 | |
| 104 | try { |
| 105 | /* tslint:disable:no-var-requires */ |
| 106 | nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug")); |
| 107 | /* tslint:enable:no-var-requires */ |
| 108 | } catch (e) { |
| 109 | // Unable to find nodeDebug, but we can make our own communication channel now |
| 110 | const debugSession = new vscodeDebugAdapterPackage.DebugSession(); |
| 111 | // Note: this will not work in the context of debugging the debug adapter and communicating over a socket, |
| 112 | // but in that case we have much better ways to investigate errors. |
| 113 | debugSession.start(process.stdin, process.stdout); |
| 114 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); |
| 115 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); |
| 116 | |
| 117 | bailOut("cannotFindNodeDebugAdapter"); |
| 118 | } |
| 119 | |
| 120 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; |
| 121 | |
| 122 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments |
| 123 | const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest; |
| 124 | nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) { |
| 125 | projectRootPath = path.resolve(args.program, "../.."); |
| 126 | telemetryReporter.reassignTo(new ExtensionTelemetryReporter( // We start to send telemetry |
| 127 | appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, projectRootPath)); |
| 128 | |
| 129 | // Create a server waiting for messages to re-initialize the debug session; |
| 130 | const reinitializeServer = http.createServer((req, res) => { |
| 131 | res.statusCode = 404; |
| 132 | if (req.url === "/refreshBreakpoints") { |
| 133 | res.statusCode = 200; |
| 134 | if (this) { |
| 135 | const sourceMaps = this._sourceMaps; |
| 136 | if (sourceMaps) { |
| 137 | // Flush any cached source maps |
| 138 | sourceMaps._allSourceMaps = {}; |
| 139 | sourceMaps._generatedToSourceMaps = {}; |
| 140 | sourceMaps._sourceToGeneratedMaps = {}; |
| 141 | } |
| 142 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 143 | this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); |
| 144 | } |
| 145 | } |
| 146 | res.end(); |
| 147 | }); |
| 148 | const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090; |
| 149 | |
| 150 | |
| 151 | reinitializeServer.listen(debugServerListeningPort); |
| 152 | reinitializeServer.on("error", (err: Error) => { |
| 153 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); |
| 154 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr")); |
| 155 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json")); |
| 156 | }); |
| 157 | |
| 158 | // We do not permit arbitrary args to be passed to our process |
| 159 | args.args = [ |
| 160 | args.platform, |
| 161 | debugServerListeningPort.toString(), |
| 162 | args.target || "simulator", |
| 163 | ]; |
| 164 | |
| 165 | if (args.iosProjectPath) { |
| 166 | args.args.push(args.iosProjectPath); |
| 167 | } |
| 168 | |
| 169 | if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise) |
| 170 | args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]); |
| 171 | } |
| 172 | |
| 173 | originalNodeDebugSessionLaunchRequest.call(this, request, args); |
| 174 | }; |
| 175 | |
| 176 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments |
| 177 | const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest; |
| 178 | function customDisconnectRequest(response: any, args: any): void { |
| 179 | try { |
| 180 | // First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session |
| 181 | const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath); |
| 182 | remoteExtension.stopMonitoringLogcat() |
| 183 | .finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args)) |
| 184 | .done(() => { }, reason => // We just print a warning if something fails |
| 185 | process.stderr.write(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`)); |
| 186 | } catch (exception) { |
| 187 | // This is a "nice to have" feature, so we just fire the message and forget. We don't event handle |
| 188 | // errors in the response promise |
| 189 | process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`); |
| 190 | originalNodeDebugSessionDisconnectRequest.call(this, response, args); |
| 191 | } |
| 192 | } |
| 193 | nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest; |
| 194 | |
| 195 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |
| 196 | }); |