microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugSession.ts
171lines · 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 vscode from "vscode"; |
| 5 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; |
| 6 | import { logger } from "vscode-debugadapter"; |
| 7 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 8 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 9 | import { DirectCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/directCDPMessageHandler"; |
| 10 | import { DebugSessionBase, IAttachRequestArgs, ILaunchRequestArgs } from "../debugSessionBase"; |
| 11 | import { JsDebugConfigAdapter } from "../jsDebugConfigAdapter"; |
| 12 | import { DebuggerEndpointHelper } from "../../cdp-proxy/debuggerEndpointHelper"; |
| 13 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 14 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 15 | import * as nls from "vscode-nls"; |
| 16 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 17 | const localize = nls.loadMessageBundle(); |
| 18 | |
| 19 | export class DirectDebugSession extends DebugSessionBase { |
| 20 | |
| 21 | private debuggerEndpointHelper: DebuggerEndpointHelper; |
| 22 | private onDidTerminateDebugSessionHandler: vscode.Disposable; |
| 23 | |
| 24 | constructor(session: vscode.DebugSession) { |
| 25 | super(session); |
| 26 | this.debuggerEndpointHelper = new DebuggerEndpointHelper(); |
| 27 | |
| 28 | this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession( |
| 29 | this.handleTerminateDebugSession.bind(this) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
| 34 | let extProps = { |
| 35 | platform: { |
| 36 | value: launchArgs.platform, |
| 37 | isPii: false, |
| 38 | }, |
| 39 | isDirect: { |
| 40 | value: true, |
| 41 | isPii: false, |
| 42 | }, |
| 43 | }; |
| 44 | |
| 45 | return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs) |
| 46 | .then(() => { |
| 47 | logger.log("Launching the application"); |
| 48 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`); |
| 49 | return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows"); |
| 50 | }) |
| 51 | .then(versions => { |
| 52 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 53 | if (launchArgs.platform === "windows") { |
| 54 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 55 | } |
| 56 | return TelemetryHelper.generate("launch", extProps, (generator) => { |
| 57 | return this.appLauncher.launch(launchArgs) |
| 58 | .then(() => { |
| 59 | if (launchArgs.enableDebug) { |
| 60 | launchArgs.port = launchArgs.port || this.appLauncher.getPackagerPort(launchArgs.cwd); |
| 61 | this.attachRequest(response, launchArgs).then(() => { |
| 62 | resolve(); |
| 63 | }).catch((e) => reject(e)); |
| 64 | } else { |
| 65 | this.sendResponse(response); |
| 66 | resolve(); |
| 67 | } |
| 68 | }); |
| 69 | }); |
| 70 | }) |
| 71 | .catch((err) => { |
| 72 | reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err)); |
| 73 | }) |
| 74 | ) |
| 75 | .catch(err => this.showError(err, response)); |
| 76 | } |
| 77 | |
| 78 | protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> { |
| 79 | let extProps = { |
| 80 | platform: { |
| 81 | value: attachArgs.platform, |
| 82 | isPii: false, |
| 83 | }, |
| 84 | isDirect: { |
| 85 | value: true, |
| 86 | isPii: false, |
| 87 | }, |
| 88 | }; |
| 89 | |
| 90 | this.previousAttachArgs = attachArgs; |
| 91 | |
| 92 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) |
| 93 | .then(() => { |
| 94 | logger.log("Attaching to the application"); |
| 95 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`); |
| 96 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true); |
| 97 | }) |
| 98 | .then(versions => { |
| 99 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 100 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { |
| 101 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 102 | } |
| 103 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 104 | attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd); |
| 105 | logger.log(`Connecting to ${attachArgs.port} port`); |
| 106 | return this.appLauncher.getRnCdpProxy().stopServer() |
| 107 | .then(() => this.appLauncher.getRnCdpProxy().initializeServer(new DirectCDPMessageHandler(), this.cdpProxyLogLevel)) |
| 108 | .then(() => this.appLauncher.getPackager().start()) |
| 109 | .then(() => this.debuggerEndpointHelper.retryGetWSEndpoint( |
| 110 | `http://localhost:${attachArgs.port}`, |
| 111 | 90, |
| 112 | this.cancellationTokenSource.token |
| 113 | )) |
| 114 | .then((browserInspectUri) => { |
| 115 | this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri); |
| 116 | this.establishDebugSession(attachArgs, resolve); |
| 117 | }) |
| 118 | .catch(e => reject(e)); |
| 119 | }); |
| 120 | }) |
| 121 | .catch((err) => { |
| 122 | reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err)); |
| 123 | }) |
| 124 | ) |
| 125 | .catch(err => this.showError(err, response)); |
| 126 | } |
| 127 | |
| 128 | protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> { |
| 129 | this.onDidTerminateDebugSessionHandler.dispose(); |
| 130 | |
| 131 | super.disconnectRequest(response, args, request); |
| 132 | } |
| 133 | |
| 134 | protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void { |
| 135 | const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForRNHermes( |
| 136 | attachArgs, |
| 137 | this.appLauncher.getCdpProxyPort(), |
| 138 | this.session.id |
| 139 | ); |
| 140 | |
| 141 | vscode.debug.startDebugging( |
| 142 | this.appLauncher.getWorkspaceFolder(), |
| 143 | attachConfiguration, |
| 144 | { |
| 145 | parentSession: this.session, |
| 146 | consoleMode: vscode.DebugConsoleMode.MergeWithParent, |
| 147 | } |
| 148 | ) |
| 149 | .then((childDebugSessionStarted: boolean) => { |
| 150 | if (childDebugSessionStarted) { |
| 151 | if (resolve) { |
| 152 | resolve(); |
| 153 | } |
| 154 | } else { |
| 155 | throw new Error(localize("CouldNotStartChildDebugSession", "Couldn't start child debug session")); |
| 156 | } |
| 157 | }, |
| 158 | err => { |
| 159 | throw err; |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | private handleTerminateDebugSession(debugSession: vscode.DebugSession) { |
| 164 | if ( |
| 165 | debugSession.configuration.rnDebugSessionId === this.session.id |
| 166 | && debugSession.type === this.pwaNodeSessionName |
| 167 | ) { |
| 168 | this.session.customRequest(this.disconnectCommand, {forcedStop: true}); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |