microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
320lines · 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"); | |
b8999098Dmitry Zinovyev9 years ago | 8 | import { Telemetry } from "../common/telemetry"; |
| 9 | import { TelemetryHelper } from "../common/telemetryHelper"; | |
| 10 | import { RemoteExtension } from "../common/remoteExtension"; | |
3a155214Artem Egorov8 years ago | 11 | import { RemoteTelemetryReporter, ReassignableTelemetryReporter } from "../common/telemetryReporters"; |
e67ace8aYuri Skorokhodov6 years ago | 12 | import { ChromeDebugSession, IChromeDebugSessionOpts, ChromeDebugAdapter, logger, Crdp, stoppedEvent, IOnPausedResult } from "vscode-chrome-debug-core"; |
e3b0fb3bChance An8 years ago | 13 | import { ContinuedEvent, TerminatedEvent, Logger, Response } from "vscode-debugadapter"; |
0a68f8dbArtem Egorov8 years ago | 14 | import { DebugProtocol } from "vscode-debugprotocol"; |
e45838cbVladimir Kotikov9 years ago | 15 | import { MultipleLifetimesAppWorker } from "./appWorker"; |
2d876061Ruslan Bikkinin8 years ago | 16 | import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper"; |
e3706a1cRedMickey6 years ago | 17 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; |
d124bf0eYuri Skorokhodov7 years ago | 18 | import * as nls from "vscode-nls"; |
| 19 | import { ErrorHelper } from "../common/error/errorHelper"; | |
| 20 | import { InternalErrorCode } from "../common/error/internalErrorCode"; | |
08722261Yuri Skorokhodov7 years ago | 21 | import { getLoggingDirectory } from "../extension/log/LogHelper"; |
3955d20aYuri Skorokhodov6 years ago | 22 | import * as mkdirp from "mkdirp"; |
d124bf0eYuri Skorokhodov7 years ago | 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(() => { |
18ea7d15Yuri Skorokhodov6 years ago | 86 | return this.remoteExtension.getPackagerPort(request.arguments.cwd || 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(() => { | |
dbfbebd9Yuri Skorokhodov7 years ago | 105 | logger.verbose(`Handle attach request: ${JSON.stringify(request.arguments, null , 2)}`); |
18ea7d15Yuri Skorokhodov6 years ago | 106 | return this.remoteExtension.getPackagerPort(request.arguments.cwd || request.arguments.program); |
2d876061Ruslan Bikkinin8 years ago | 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> { |
08722261Yuri Skorokhodov7 years ago | 139 | // If special env variables are defined, then write process outputs to file |
| 140 | let chromeDebugCoreLogs = getLoggingDirectory(); | |
| 141 | if (chromeDebugCoreLogs) { | |
| 142 | chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "ChromeDebugCoreLogs.txt"); | |
| 143 | } | |
0a68f8dbArtem Egorov8 years ago | 144 | let logLevel: string = args.trace; |
| 145 | if (logLevel) { | |
| 146 | logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase()); | |
08722261Yuri Skorokhodov7 years ago | 147 | logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false); |
0a68f8dbArtem Egorov8 years ago | 148 | } else { |
08722261Yuri Skorokhodov7 years ago | 149 | logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false); |
0a68f8dbArtem Egorov8 years ago | 150 | } |
| 151 | | |
18ea7d15Yuri Skorokhodov6 years ago | 152 | |
135c493eYuri Skorokhodov7 years ago | 153 | if (!args.sourceMaps) { |
| 154 | args.sourceMaps = true; | |
| 155 | } | |
2d876061Ruslan Bikkinin8 years ago | 156 | const projectRootPath = getProjectRoot(args); |
| 157 | return ReactNativeProjectHelper.isReactNativeProject(projectRootPath) | |
| 158 | .then((result) => { | |
| 159 | if (!result) { | |
d124bf0eYuri Skorokhodov7 years ago | 160 | throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError); |
2d876061Ruslan Bikkinin8 years ago | 161 | } |
| 162 | this.projectRootPath = projectRootPath; | |
| 163 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); | |
| 164 | | |
| 165 | // Start to send telemetry | |
| 166 | telemetryReporter.reassignTo(new RemoteTelemetryReporter( | |
| 167 | appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, this.projectRootPath)); | |
23b5fa80Yuri Skorokhodov6 years ago | 168 | |
| 169 | if (args.program) { | |
| 170 | // TODO: Remove this warning when program property will be completely removed | |
| 171 | logger.warn(localize("ProgramPropertyDeprecationWarning", "Launched debug configuration contains 'program' property which is deprecated and will be removed soon. Please replace it with: \"cwd\": \"${workspaceFolder}\"")); | |
| 172 | const useProgramEvent = TelemetryHelper.createTelemetryEvent("useProgramProperty"); | |
| 173 | Telemetry.send(useProgramEvent); | |
| 174 | } | |
| 175 | if (args.cwd) { | |
| 176 | // To match count of 'cwd' users with 'program' users. TODO: Remove when program property will be removed | |
| 177 | const useCwdEvent = TelemetryHelper.createTelemetryEvent("useCwdProperty"); | |
| 178 | Telemetry.send(useCwdEvent); | |
| 179 | } | |
2d876061Ruslan Bikkinin8 years ago | 180 | return void 0; |
| 181 | }); | |
e45838cbVladimir Kotikov9 years ago | 182 | } |
| 183 | | |
| 184 | /** | |
| 185 | * Runs logic needed to attach. | |
| 186 | * Attach should: | |
| 187 | * - Enable js debugging | |
| 188 | */ | |
0a68f8dbArtem Egorov8 years ago | 189 | // tslint:disable-next-line:member-ordering |
031832ffArtem Egorov7 years ago | 190 | protected attachRequest(request: DebugProtocol.Request): Q.Promise<void> { |
ba953e9fRedMickey6 years ago | 191 | let extProps = { |
031832ffArtem Egorov7 years ago | 192 | platform: { |
| 193 | value: request.arguments.platform, | |
| 194 | isPii: false, | |
| 195 | }, | |
| 196 | }; | |
| 197 | | |
e3706a1cRedMickey6 years ago | 198 | return ProjectVersionHelper.getReactNativeVersions(request.arguments.cwd, true) |
7fa90b3bRedMickey6 years ago | 199 | .then(versions => { |
| 200 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); | |
e3706a1cRedMickey6 years ago | 201 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { |
7fa90b3bRedMickey6 years ago | 202 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 203 | } | |
ba953e9fRedMickey6 years ago | 204 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 205 | return Q({}) | |
| 206 | .then(() => { | |
| 207 | logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker.")); | |
| 208 | // TODO: remove dependency on args.program - "program" property is technically | |
| 209 | // no more required in launch configuration and could be removed | |
| 210 | const workspaceRootPath = request.arguments.cwd ? path.resolve(request.arguments.cwd) : path.resolve(path.dirname(request.arguments.program), ".."); | |
| 211 | const sourcesStoragePath = path.join(workspaceRootPath, ".vscode", ".react"); | |
| 212 | // Create folder if not exist to avoid problems if | |
| 213 | // RN project root is not a ${workspaceFolder} | |
| 214 | mkdirp.sync(sourcesStoragePath); | |
| 215 | | |
| 216 | // If launch is invoked first time, appWorker is undefined, so create it here | |
| 217 | this.appWorker = new MultipleLifetimesAppWorker( | |
| 218 | request.arguments, | |
| 219 | sourcesStoragePath, | |
| 220 | this.projectRootPath, | |
| 221 | undefined); | |
| 222 | this.appWorker.on("connected", (port: number) => { | |
| 223 | logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port)); | |
| 224 | // Don't mutate original request to avoid side effects | |
| 225 | let attachArguments = Object.assign({}, request.arguments, { | |
| 226 | address: "localhost", | |
| 227 | port, | |
| 228 | restart: true, | |
| 229 | request: "attach", | |
| 230 | remoteRoot: undefined, | |
| 231 | localRoot: undefined, | |
| 232 | }); | |
| 233 | // Reinstantiate debug adapter, as the current implementation of ChromeDebugAdapter | |
| 234 | // doesn't allow us to reattach to another debug target easily. As of now it's easier | |
| 235 | // to throw previous instance out and create a new one. | |
| 236 | (this as any)._debugAdapter = new (<any>debugSessionOpts.adapter)(debugSessionOpts, this); | |
| 237 | | |
| 238 | // Explicity call _debugAdapter.attach() to prevent directly calling dispatchRequest() | |
| 239 | // yield a response as "attach" even for "launch" request. Because dispatchRequest() will | |
| 240 | // decide to do a sendResponse() aligning with the request parameter passed in. | |
| 241 | Q((this as any)._debugAdapter.attach(attachArguments, request.seq)) | |
| 242 | .then((responseBody) => { | |
| 243 | const response: DebugProtocol.Response = new Response(request); | |
| 244 | response.body = responseBody; | |
| 245 | this.sendResponse(response); | |
| 246 | }); | |
2d876061Ruslan Bikkinin8 years ago | 247 | }); |
e45838cbVladimir Kotikov9 years ago | 248 | |
ba953e9fRedMickey6 years ago | 249 | return this.appWorker.start(); |
| 250 | }) | |
| 251 | .catch(error => this.bailOut(error.message)); | |
| 252 | }); | |
| 253 | }); | |
e45838cbVladimir Kotikov9 years ago | 254 | } |
| 255 | | |
| 256 | /** | |
| 257 | * Logs error to user and finishes the debugging process. | |
| 258 | */ | |
| 259 | private bailOut(message: string): void { | |
d124bf0eYuri Skorokhodov7 years ago | 260 | logger.error(localize("CouldNotDebug", "Could not debug. {0}" , message)); |
0a68f8dbArtem Egorov8 years ago | 261 | this.sendEvent(new TerminatedEvent()); |
27710197Vladimir Kotikov8 years ago | 262 | } |
e45838cbVladimir Kotikov9 years ago | 263 | }; |
| 264 | } | |
| 265 | | |
0a68f8dbArtem Egorov8 years ago | 266 | export function makeAdapter(debugAdapterClass: typeof ChromeDebugAdapter): typeof ChromeDebugAdapter { |
e45838cbVladimir Kotikov9 years ago | 267 | return class extends debugAdapterClass { |
e67ace8aYuri Skorokhodov6 years ago | 268 | private firstStop: boolean = true; |
4f7b3bc0Anna Kocheshkova8 years ago | 269 | public doAttach(port: number, targetUrl?: string, address?: string, timeout?: number): Promise<void> { |
4b86d595Vladimir Kotikov9 years ago | 270 | // We need to overwrite ChromeDebug's _attachMode to let Node2 adapter |
e45838cbVladimir Kotikov9 years ago | 271 | // to set up breakpoints on initial pause event |
0a68f8dbArtem Egorov8 years ago | 272 | (this as any)._attachMode = false; |
e45838cbVladimir Kotikov9 years ago | 273 | return super.doAttach(port, targetUrl, address, timeout); |
| 274 | } | |
639a73d7Artem Egorov7 years ago | 275 | |
e67ace8aYuri Skorokhodov6 years ago | 276 | // Since the bundle runs inside the Node.js VM in debuggerWorker.js in runtime |
| 277 | // Node debug adapter need time to parse new added code source maps | |
| 278 | // So we added 'debugger;' statement at the start of the bundle code | |
| 279 | // and wait for the adapter to receive a signal to stop on that statement | |
| 280 | // and then wait for code bundle to be processed and then send continue request to skip the code execution stop in VS Code UI | |
| 281 | public onPaused(notification: Crdp.Debugger.PausedEvent, expectingStopReason?: stoppedEvent.ReasonType): Promise<IOnPausedResult> { | |
| 282 | // When pause on 'debugger;' statement, notification contains reason with value "other" instead of "breakpoint" | |
| 283 | if (this.firstStop && notification.reason === "other") { | |
| 284 | return new Promise<IOnPausedResult>((resolve) => { | |
| 285 | setTimeout(() => { | |
| 286 | this.firstStop = false; | |
| 287 | this.continue(); | |
| 288 | resolve({didPause: false}); | |
| 289 | }, 50); | |
| 290 | }); | |
| 291 | } else { | |
| 292 | return super.onPaused(notification, expectingStopReason); | |
| 293 | } | |
| 294 | } | |
| 295 | | |
639a73d7Artem Egorov7 years ago | 296 | public async terminate(args: DebugProtocol.TerminatedEvent) { |
| 297 | return this.disconnect({ | |
| 298 | terminateDebuggee: true, | |
| 299 | }); | |
| 300 | } | |
e45838cbVladimir Kotikov9 years ago | 301 | }; |
| 302 | } | |
| 303 | | |
| 304 | /** | |
| 305 | * Parses settings.json file for workspace root property | |
| 306 | */ | |
549baae2RedMickey6 years ago | 307 | export function getProjectRoot(args: any): string { |
31796719RedMickey6 years ago | 308 | const vsCodeRoot = args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../.."); |
| 309 | const settingsPath = path.resolve(vsCodeRoot, ".vscode/settings.json"); | |
e45838cbVladimir Kotikov9 years ago | 310 | try { |
| 311 | let settingsContent = fs.readFileSync(settingsPath, "utf8"); | |
| 312 | settingsContent = stripJsonComments(settingsContent); | |
| 313 | let parsedSettings = JSON.parse(settingsContent); | |
9a364375Serge Svekolnikov8 years ago | 314 | let projectRootPath = parsedSettings["react-native-tools.projectRoot"] || parsedSettings["react-native-tools"].projectRoot; |
e45838cbVladimir Kotikov9 years ago | 315 | return path.resolve(vsCodeRoot, projectRootPath); |
| 316 | } catch (e) { | |
52f2485aYuri Skorokhodov6 years ago | 317 | logger.verbose(`'projectRoot' setting is not found: ${settingsPath} file doesn't exist or its content is incorrect.`); |
18ea7d15Yuri Skorokhodov6 years ago | 318 | return args.cwd ? path.resolve(args.cwd) : path.resolve(args.program, "../.."); |
e45838cbVladimir Kotikov9 years ago | 319 | } |
| 320 | } |