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