microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
257lines · modeblame
e45838cbVladimir Kotikov9 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 Q from "q"; | |
| 5 | import * as path from "path"; | |
| 6 | import * as fs from "fs"; | |
| 7 | import stripJsonComments = require("strip-json-comments"); | |
| 8 | | |
b8999098Dmitry Zinovyev9 years ago | 9 | import { Telemetry } from "../common/telemetry"; |
| 10 | import { TelemetryHelper } from "../common/telemetryHelper"; | |
| 11 | import { RemoteExtension } from "../common/remoteExtension"; | |
3a155214Artem Egorov8 years ago | 12 | import { RemoteTelemetryReporter, ReassignableTelemetryReporter } from "../common/telemetryReporters"; |
2d876061Ruslan Bikkinin8 years ago | 13 | import { ChromeDebugSession, IChromeDebugSessionOpts, ChromeDebugAdapter, logger } from "vscode-chrome-debug-core"; |
e3b0fb3bChance An8 years ago | 14 | import { ContinuedEvent, TerminatedEvent, Logger, Response } from "vscode-debugadapter"; |
0a68f8dbArtem Egorov8 years ago | 15 | import { DebugProtocol } from "vscode-debugprotocol"; |
e45838cbVladimir Kotikov9 years ago | 16 | |
| 17 | import { MultipleLifetimesAppWorker } from "./appWorker"; | |
| 18 | | |
2d876061Ruslan Bikkinin8 years ago | 19 | import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper"; |
| 20 | | |
0a68f8dbArtem Egorov8 years ago | 21 | |
b8999098Dmitry Zinovyev9 years ago | 22 | export function makeSession( |
0a68f8dbArtem Egorov8 years ago | 23 | debugSessionClass: typeof ChromeDebugSession, |
| 24 | debugSessionOpts: IChromeDebugSessionOpts, | |
b8999098Dmitry Zinovyev9 years ago | 25 | telemetryReporter: ReassignableTelemetryReporter, |
0a68f8dbArtem Egorov8 years ago | 26 | appName: string, version: string): typeof ChromeDebugSession { |
e45838cbVladimir Kotikov9 years ago | 27 | |
| 28 | return class extends debugSessionClass { | |
| 29 | | |
| 30 | private projectRootPath: string; | |
| 31 | private remoteExtension: RemoteExtension; | |
5c8365a6Artem Egorov8 years ago | 32 | private appWorker: MultipleLifetimesAppWorker | null = null; |
e45838cbVladimir Kotikov9 years ago | 33 | |
| 34 | constructor(debuggerLinesAndColumnsStartAt1?: boolean, isServer?: boolean) { | |
| 35 | super(debuggerLinesAndColumnsStartAt1, isServer, debugSessionOpts); | |
| 36 | } | |
| 37 | | |
| 38 | // Override ChromeDebugSession's sendEvent to control what we will send to client | |
0a68f8dbArtem Egorov8 years ago | 39 | public sendEvent(event: DebugProtocol.Event): void { |
e45838cbVladimir Kotikov9 years ago | 40 | // Do not send "terminated" events signaling about session's restart to client as it would cause it |
| 41 | // to restart adapter's process, while we want to stay alive and don't want to interrupt connection | |
6c75098eVladimir9 years ago | 42 | // to packager. |
b8999098Dmitry Zinovyev9 years ago | 43 | |
720e992dArtem Egorov8 years ago | 44 | if (event.event === "terminated" && event.body && event.body.restart) { |
b8999098Dmitry Zinovyev9 years ago | 45 | |
| 46 | // Worker has been reloaded and switched to "continue" state | |
| 47 | // So we have to send "continued" event to client instead of "terminated" | |
| 48 | // Otherwise client might mistakenly show "stopped" state | |
0a68f8dbArtem Egorov8 years ago | 49 | let continuedEvent: ContinuedEvent = { |
b8999098Dmitry Zinovyev9 years ago | 50 | event: "continued", |
| 51 | type: "event", | |
| 52 | seq: event["seq"], // tslint:disable-line | |
| 53 | body: { threadId: event.body.threadId }, | |
| 54 | }; | |
| 55 | | |
| 56 | super.sendEvent(continuedEvent); | |
e45838cbVladimir Kotikov9 years ago | 57 | return; |
| 58 | } | |
| 59 | | |
| 60 | super.sendEvent(event); | |
| 61 | } | |
| 62 | | |
0a68f8dbArtem Egorov8 years ago | 63 | protected dispatchRequest(request: DebugProtocol.Request): void { |
e45838cbVladimir Kotikov9 years ago | 64 | if (request.command === "disconnect") |
| 65 | return this.disconnect(request); | |
| 66 | | |
| 67 | if (request.command === "attach") | |
| 68 | return this.attach(request); | |
| 69 | | |
| 70 | if (request.command === "launch") | |
| 71 | return this.launch(request); | |
| 72 | | |
| 73 | return super.dispatchRequest(request); | |
| 74 | } | |
| 75 | | |
0a68f8dbArtem Egorov8 years ago | 76 | private launch(request: DebugProtocol.Request): void { |
2d876061Ruslan Bikkinin8 years ago | 77 | this.requestSetup(request.arguments) |
| 78 | .then(() => { | |
| 79 | return this.remoteExtension.launch(request); | |
| 80 | }) | |
cbb0e869Artem Egorov8 years ago | 81 | .then(() => { |
2e432a9eArtem Egorov8 years ago | 82 | return this.remoteExtension.getPackagerPort(request.arguments.program); |
0a68f8dbArtem Egorov8 years ago | 83 | }) |
| 84 | .then((packagerPort: number) => { | |
6eeec3c0Serge Svekolnikov8 years ago | 85 | this.attachRequest({ |
| 86 | ...request, | |
| 87 | arguments: { | |
| 88 | ...request.arguments, | |
| 89 | port: packagerPort, | |
| 90 | }, | |
| 91 | }); | |
0a68f8dbArtem Egorov8 years ago | 92 | }) |
| 93 | .catch(error => { | |
748105d9Artem Egorov8 years ago | 94 | this.bailOut(error.data || error.message); |
cbb0e869Artem Egorov8 years ago | 95 | }); |
e45838cbVladimir Kotikov9 years ago | 96 | } |
| 97 | | |
0a68f8dbArtem Egorov8 years ago | 98 | private attach(request: DebugProtocol.Request): void { |
2d876061Ruslan Bikkinin8 years ago | 99 | this.requestSetup(request.arguments) |
| 100 | .then(() => { | |
| 101 | return this.remoteExtension.getPackagerPort(request.arguments.program); | |
| 102 | }) | |
0a68f8dbArtem Egorov8 years ago | 103 | .then((packagerPort: number) => { |
6eeec3c0Serge Svekolnikov8 years ago | 104 | this.attachRequest({ |
| 105 | ...request, | |
| 106 | arguments: { | |
| 107 | ...request.arguments, | |
e92278b5Serge Svekolnikov8 years ago | 108 | port: request.arguments.port || packagerPort, |
6eeec3c0Serge Svekolnikov8 years ago | 109 | }, |
| 110 | }); | |
2d876061Ruslan Bikkinin8 years ago | 111 | }) |
| 112 | .catch(error => { | |
| 113 | this.bailOut(error.data || error.message); | |
cbb0e869Artem Egorov8 years ago | 114 | }); |
e45838cbVladimir Kotikov9 years ago | 115 | } |
| 116 | | |
0a68f8dbArtem Egorov8 years ago | 117 | private disconnect(request: DebugProtocol.Request): void { |
e45838cbVladimir Kotikov9 years ago | 118 | // The client is about to disconnect so first we need to stop app worker |
f920e582Vladimir Kotikov9 years ago | 119 | if (this.appWorker) { |
| 120 | this.appWorker.stop(); | |
| 121 | } | |
e45838cbVladimir Kotikov9 years ago | 122 | |
| 123 | // Then we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session | |
0a68f8dbArtem Egorov8 years ago | 124 | if (request.arguments.platform === "android") { |
e45838cbVladimir Kotikov9 years ago | 125 | this.remoteExtension.stopMonitoringLogcat() |
0a68f8dbArtem Egorov8 years ago | 126 | .catch(reason => logger.warn(`Couldn't stop monitoring logcat: ${reason.message || reason}`)) |
b8999098Dmitry Zinovyev9 years ago | 127 | .finally(() => super.dispatchRequest(request)); |
e45838cbVladimir Kotikov9 years ago | 128 | } else { |
| 129 | super.dispatchRequest(request); | |
| 130 | } | |
| 131 | } | |
| 132 | | |
2d876061Ruslan Bikkinin8 years ago | 133 | private requestSetup(args: any): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 134 | let logLevel: string = args.trace; |
| 135 | if (logLevel) { | |
| 136 | logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase()); | |
| 137 | logger.setup(Logger.LogLevel[logLevel], false); | |
| 138 | } else { | |
| 139 | logger.setup(Logger.LogLevel.Log, false); | |
| 140 | } | |
| 141 | | |
2d876061Ruslan Bikkinin8 years ago | 142 | const projectRootPath = getProjectRoot(args); |
| 143 | return ReactNativeProjectHelper.isReactNativeProject(projectRootPath) | |
| 144 | .then((result) => { | |
| 145 | if (!result) { | |
| 146 | throw new Error(`Seems to be that you are trying to debug from within directory that is not a React Native project root. | |
| 147 | If so, please, follow these instructions: https://github.com/Microsoft/vscode-react-native/blob/master/doc/customization.md#project-structure.`); | |
| 148 | } | |
| 149 | this.projectRootPath = projectRootPath; | |
| 150 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); | |
| 151 | | |
| 152 | // Start to send telemetry | |
| 153 | telemetryReporter.reassignTo(new RemoteTelemetryReporter( | |
| 154 | appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, this.projectRootPath)); | |
| 155 | return void 0; | |
| 156 | }); | |
e45838cbVladimir Kotikov9 years ago | 157 | } |
| 158 | | |
| 159 | /** | |
| 160 | * Runs logic needed to attach. | |
| 161 | * Attach should: | |
| 162 | * - Enable js debugging | |
| 163 | */ | |
0a68f8dbArtem Egorov8 years ago | 164 | // tslint:disable-next-line:member-ordering |
031832ffArtem Egorov8 years ago | 165 | protected attachRequest(request: DebugProtocol.Request): Q.Promise<void> { |
| 166 | const extProps = { | |
| 167 | platform: { | |
| 168 | value: request.arguments.platform, | |
| 169 | isPii: false, | |
| 170 | }, | |
| 171 | }; | |
| 172 | | |
| 173 | return TelemetryHelper.generate("attach", extProps, (generator) => { | |
e45838cbVladimir Kotikov9 years ago | 174 | return Q({}) |
| 175 | .then(() => { | |
0a68f8dbArtem Egorov8 years ago | 176 | logger.log("Starting debugger app worker."); |
e45838cbVladimir Kotikov9 years ago | 177 | // TODO: remove dependency on args.program - "program" property is technically |
| 178 | // no more required in launch configuration and could be removed | |
| 179 | const workspaceRootPath = path.resolve(path.dirname(request.arguments.program), ".."); | |
| 180 | const sourcesStoragePath = path.join(workspaceRootPath, ".vscode", ".react"); | |
| 181 | | |
| 182 | // If launch is invoked first time, appWorker is undefined, so create it here | |
6eeec3c0Serge Svekolnikov8 years ago | 183 | this.appWorker = new MultipleLifetimesAppWorker( |
| 184 | request.arguments, | |
| 185 | sourcesStoragePath, | |
| 186 | this.projectRootPath, | |
| 187 | undefined); | |
e45838cbVladimir Kotikov9 years ago | 188 | this.appWorker.on("connected", (port: number) => { |
0a68f8dbArtem Egorov8 years ago | 189 | logger.log("Debugger worker loaded runtime on port " + port); |
e45838cbVladimir Kotikov9 years ago | 190 | // Don't mutate original request to avoid side effects |
6eeec3c0Serge Svekolnikov8 years ago | 191 | let attachArguments = Object.assign({}, request.arguments, { |
| 192 | address: "localhost", | |
| 193 | port, | |
| 194 | restart: true, | |
| 195 | request: "attach", | |
| 196 | remoteRoot: undefined, | |
| 197 | localRoot: undefined, | |
| 198 | }); | |
6c75098eVladimir9 years ago | 199 | // Reinstantiate debug adapter, as the current implementation of ChromeDebugAdapter |
| 200 | // doesn't allow us to reattach to another debug target easily. As of now it's easier | |
| 201 | // to throw previous instance out and create a new one. | |
0a68f8dbArtem Egorov8 years ago | 202 | (this as any)._debugAdapter = new (<any>debugSessionOpts.adapter)(debugSessionOpts, this); |
e3b0fb3bChance An8 years ago | 203 | |
| 204 | // Explicity call _debugAdapter.attach() to prevent directly calling dispatchRequest() | |
| 205 | // yield a response as "attach" even for "launch" request. Because dispatchRequest() will | |
| 206 | // decide to do a sendResponse() aligning with the request parameter passed in. | |
| 207 | Q((this as any)._debugAdapter.attach(attachArguments, request.seq)) | |
2d876061Ruslan Bikkinin8 years ago | 208 | .then((responseBody) => { |
| 209 | const response: DebugProtocol.Response = new Response(request); | |
| 210 | response.body = responseBody; | |
| 211 | this.sendResponse(response); | |
| 212 | }); | |
e45838cbVladimir Kotikov9 years ago | 213 | }); |
| 214 | | |
| 215 | return this.appWorker.start(); | |
| 216 | }) | |
| 217 | .catch(error => this.bailOut(error.message)); | |
| 218 | }); | |
| 219 | } | |
| 220 | | |
| 221 | /** | |
| 222 | * Logs error to user and finishes the debugging process. | |
| 223 | */ | |
| 224 | private bailOut(message: string): void { | |
0a68f8dbArtem Egorov8 years ago | 225 | logger.error(`Could not debug. ${message}`); |
| 226 | this.sendEvent(new TerminatedEvent()); | |
27710197Vladimir Kotikov8 years ago | 227 | } |
e45838cbVladimir Kotikov9 years ago | 228 | }; |
| 229 | } | |
| 230 | | |
0a68f8dbArtem Egorov8 years ago | 231 | export function makeAdapter(debugAdapterClass: typeof ChromeDebugAdapter): typeof ChromeDebugAdapter { |
e45838cbVladimir Kotikov9 years ago | 232 | return class extends debugAdapterClass { |
4f7b3bc0Anna Kocheshkova8 years ago | 233 | public doAttach(port: number, targetUrl?: string, address?: string, timeout?: number): Promise<void> { |
4b86d595Vladimir Kotikov9 years ago | 234 | // We need to overwrite ChromeDebug's _attachMode to let Node2 adapter |
e45838cbVladimir Kotikov9 years ago | 235 | // to set up breakpoints on initial pause event |
0a68f8dbArtem Egorov8 years ago | 236 | (this as any)._attachMode = false; |
e45838cbVladimir Kotikov9 years ago | 237 | return super.doAttach(port, targetUrl, address, timeout); |
| 238 | } | |
| 239 | }; | |
| 240 | } | |
| 241 | | |
| 242 | /** | |
| 243 | * Parses settings.json file for workspace root property | |
| 244 | */ | |
| 245 | function getProjectRoot(args: any): string { | |
| 246 | try { | |
| 247 | let vsCodeRoot = path.resolve(args.program, "../.."); | |
| 248 | let settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json"); | |
| 249 | let settingsContent = fs.readFileSync(settingsPath, "utf8"); | |
| 250 | settingsContent = stripJsonComments(settingsContent); | |
| 251 | let parsedSettings = JSON.parse(settingsContent); | |
9a364375Serge Svekolnikov8 years ago | 252 | let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot; |
e45838cbVladimir Kotikov9 years ago | 253 | return path.resolve(vsCodeRoot, projectRootPath); |
| 254 | } catch (e) { | |
| 255 | return path.resolve(args.program, "../.."); | |
| 256 | } | |
| 257 | } |