microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c036b0d343245c82886cd5906e37ff50b8b30d34

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

241lines · modeblame

6e1bcd36RedMickey6 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as vscode from "vscode";
5import * as path from "path";
6import * as mkdirp from "mkdirp";
2c19da7fRedMickey6 years ago7import { logger } from "vscode-debugadapter";
6e1bcd36RedMickey6 years ago8import { DebugProtocol } from "vscode-debugprotocol";
9import { ProjectVersionHelper } from "../common/projectVersionHelper";
10import { TelemetryHelper } from "../common/telemetryHelper";
11import { MultipleLifetimesAppWorker } from "./appWorker";
a6562589RedMickey6 years ago12import { RnCDPMessageHandler } from "../cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler";
2c19da7fRedMickey6 years ago13import { DebugSessionBase, DebugSessionStatus, IAttachRequestArgs, ILaunchRequestArgs } from "./debugSessionBase";
1bdccb66RedMickey6 years ago14import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
5514e287RedMickey6 years ago15import { ErrorHelper } from "../common/error/errorHelper";
16import { InternalErrorCode } from "../common/error/internalErrorCode";
6e1bcd36RedMickey6 years ago17import * as nls from "vscode-nls";
2d8af448Yuri Skorokhodov6 years ago18nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
6e1bcd36RedMickey6 years ago19const localize = nls.loadMessageBundle();
20
2c19da7fRedMickey6 years ago21export class RNDebugSession extends DebugSessionBase {
6e1bcd36RedMickey6 years ago22
4c757eebRedMickey6 years ago23private readonly terminateCommand: string;
f872f4d5RedMickey6 years ago24
e23d1841RedMickey6 years ago25private appWorker: MultipleLifetimesAppWorker | null;
4c757eebRedMickey6 years ago26private nodeSession: vscode.DebugSession | null;
6e491635RedMickey6 years ago27private onDidStartDebugSessionHandler: vscode.Disposable;
28private onDidTerminateDebugSessionHandler: vscode.Disposable;
6e1bcd36RedMickey6 years ago29
2c19da7fRedMickey6 years ago30constructor(session: vscode.DebugSession) {
31super(session);
4c757eebRedMickey6 years ago32
33// constants definition
34this.terminateCommand = "terminate"; // the "terminate" command is sent from the client to the debug adapter in order to give the debuggee a chance for terminating itself
35
36// variables definition
e23d1841RedMickey6 years ago37this.appWorker = null;
38
6e491635RedMickey6 years ago39this.onDidStartDebugSessionHandler = vscode.debug.onDidStartDebugSession(
4c757eebRedMickey6 years ago40this.handleStartDebugSession.bind(this)
41);
42
6e491635RedMickey6 years ago43this.onDidTerminateDebugSessionHandler = vscode.debug.onDidTerminateDebugSession(
4c757eebRedMickey6 years ago44this.handleTerminateDebugSession.bind(this)
45);
6e1bcd36RedMickey6 years ago46}
47
984ca036RedMickey6 years ago48protected async launchRequest(response: DebugProtocol.LaunchResponse, launchArgs: ILaunchRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago49return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
50.then(() => {
51logger.log("Launching the application");
52logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
53
54this.appLauncher.launch(launchArgs)
55.then(() => {
5514e287RedMickey6 years ago56if (launchArgs.enableDebug) {
57launchArgs.port = launchArgs.port || this.appLauncher.getPackagerPort(launchArgs.cwd);
58this.attachRequest(response, launchArgs).then(() => {
59resolve();
60}).catch((e) => reject(e));
61} else {
62this.sendResponse(response);
6e1bcd36RedMickey6 years ago63resolve();
5514e287RedMickey6 years ago64}
6e1bcd36RedMickey6 years ago65})
66.catch((err) => {
5514e287RedMickey6 years ago67reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err));
6e1bcd36RedMickey6 years ago68});
5514e287RedMickey6 years ago69})
70.catch((err) => {
71reject(ErrorHelper.getInternalError(InternalErrorCode.ApplicationLaunchFailed, err.message || err));
72})
73)
74.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago75}
76
984ca036RedMickey6 years ago77protected async attachRequest(response: DebugProtocol.AttachResponse, attachArgs: IAttachRequestArgs, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago78let extProps = {
79platform: {
80value: attachArgs.platform,
81isPii: false,
82},
83};
84
85this.previousAttachArgs = attachArgs;
86return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
87.then(() => {
88logger.log("Attaching to the application");
89logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
5514e287RedMickey6 years ago90return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true);
91})
92.then(versions => {
93extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
94if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
95extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
96}
97return TelemetryHelper.generate("attach", extProps, (generator) => {
98attachArgs.port = attachArgs.port || this.appLauncher.getPackagerPort(attachArgs.cwd);
99return this.appLauncher.getRnCdpProxy().stopServer()
100.then(() => this.appLauncher.getRnCdpProxy().initializeServer(new RnCDPMessageHandler(), this.cdpProxyLogLevel))
101.then(() => {
102logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
103
104const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
105// Create folder if not exist to avoid problems if
106// RN project root is not a ${workspaceFolder}
107mkdirp.sync(sourcesStoragePath);
108
109// If launch is invoked first time, appWorker is undefined, so create it here
110this.appWorker = new MultipleLifetimesAppWorker(
111attachArgs,
112sourcesStoragePath,
113this.projectRootPath,
114undefined
115);
116this.appLauncher.setAppWorker(this.appWorker);
117
118this.appWorker.on("connected", (port: number) => {
67ffa5b4RedMickey6 years ago119if (this.cancellationTokenSource.token.isCancellationRequested) {
120return this.appWorker?.stop();
121}
122
5514e287RedMickey6 years ago123logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
124
125this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
126
127if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
128return;
129}
130
131if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
132this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
133this.establishDebugSession(attachArgs, resolve);
134} else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
135if (this.nodeSession) {
136this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
137this.nodeSession.customRequest(this.terminateCommand);
138}
139}
140});
67ffa5b4RedMickey6 years ago141if (this.cancellationTokenSource.token.isCancellationRequested) {
142return this.appWorker.stop();
143}
5514e287RedMickey6 years ago144return this.appWorker.start();
6e1bcd36RedMickey6 years ago145});
5514e287RedMickey6 years ago146});
147})
148.catch((err) => {
149reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err));
150})
151)
e23d1841RedMickey6 years ago152.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago153}
154
984ca036RedMickey6 years ago155protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago156// The client is about to disconnect so first we need to stop app worker
157if (this.appWorker) {
158this.appWorker.stop();
159}
160
6e491635RedMickey6 years ago161this.onDidStartDebugSessionHandler.dispose();
162this.onDidTerminateDebugSessionHandler.dispose();
163
6e1bcd36RedMickey6 years ago164super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago165}
166
5d47053fRedMickey6 years ago167protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
1bdccb66RedMickey6 years ago168const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
169attachArgs,
170this.appLauncher.getCdpProxyPort(),
171this.session.id
172);
a6562589RedMickey6 years ago173
174vscode.debug.startDebugging(
175this.appLauncher.getWorkspaceFolder(),
1bdccb66RedMickey6 years ago176attachConfiguration,
ebbd64f1RedMickey6 years ago177{
178parentSession: this.session,
179consoleMode: vscode.DebugConsoleMode.MergeWithParent,
180}
a6562589RedMickey6 years ago181)
182.then((childDebugSessionStarted: boolean) => {
183if (childDebugSessionStarted) {
184this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
185this.setConnectionAllowedIfPossible();
186if (resolve) {
187this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
188resolve();
4c757eebRedMickey6 years ago189}
a6562589RedMickey6 years ago190} else {
4c757eebRedMickey6 years ago191this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
192this.setConnectionAllowedIfPossible();
193this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago194throw new Error("Cannot start child debug session");
195}
196},
197err => {
198this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
199this.setConnectionAllowedIfPossible();
4c757eebRedMickey6 years ago200this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago201throw err;
202});
4c757eebRedMickey6 years ago203}
6e1bcd36RedMickey6 years ago204
4c757eebRedMickey6 years ago205private handleStartDebugSession(debugSession: vscode.DebugSession) {
206if (
207debugSession.configuration.rnDebugSessionId === this.session.id
208&& debugSession.type === this.pwaNodeSessionName
209) {
210this.nodeSession = debugSession;
211}
212}
213
214private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
215if (
216debugSession.configuration.rnDebugSessionId === this.session.id
217&& debugSession.type === this.pwaNodeSessionName
218) {
ebbd64f1RedMickey6 years ago219if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago220this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago221} else {
222this.session.customRequest(this.disconnectCommand, {forcedStop: true});
223}
4c757eebRedMickey6 years ago224}
225}
226
227private setConnectionAllowedIfPossible(): void {
228if (
229this.debugSessionStatus === DebugSessionStatus.ConnectionDone
230|| this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
231) {
232this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
233}
234}
235
236private resetFirstConnectionStatus(): void {
237if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
238this.debugSessionStatus = DebugSessionStatus.FirstConnection;
239}
240}
6e1bcd36RedMickey6 years ago241}