microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
259c018fa4baf0ea6b728e4dc1f0cfe07b55904c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/rnDebugSession.ts

242lines · 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))
9fc07913JiglioNero5 years ago101.then(() => this.appLauncher.getPackager().start())
5514e287RedMickey6 years ago102.then(() => {
103logger.log(localize("StartingDebuggerAppWorker", "Starting debugger app worker."));
104
105const sourcesStoragePath = path.join(this.projectRootPath, ".vscode", ".react");
106// Create folder if not exist to avoid problems if
107// RN project root is not a ${workspaceFolder}
108mkdirp.sync(sourcesStoragePath);
109
110// If launch is invoked first time, appWorker is undefined, so create it here
111this.appWorker = new MultipleLifetimesAppWorker(
112attachArgs,
113sourcesStoragePath,
114this.projectRootPath,
115undefined
116);
117this.appLauncher.setAppWorker(this.appWorker);
118
119this.appWorker.on("connected", (port: number) => {
67ffa5b4RedMickey5 years ago120if (this.cancellationTokenSource.token.isCancellationRequested) {
121return this.appWorker?.stop();
122}
123
5514e287RedMickey6 years ago124logger.log(localize("DebuggerWorkerLoadedRuntimeOnPort", "Debugger worker loaded runtime on port {0}", port));
125
126this.appLauncher.getRnCdpProxy().setApplicationTargetPort(port);
127
128if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
129return;
130}
131
132if (this.debugSessionStatus === DebugSessionStatus.FirstConnection) {
133this.debugSessionStatus = DebugSessionStatus.FirstConnectionPending;
134this.establishDebugSession(attachArgs, resolve);
135} else if (this.debugSessionStatus === DebugSessionStatus.ConnectionAllowed) {
136if (this.nodeSession) {
137this.debugSessionStatus = DebugSessionStatus.ConnectionPending;
138this.nodeSession.customRequest(this.terminateCommand);
139}
140}
141});
67ffa5b4RedMickey5 years ago142if (this.cancellationTokenSource.token.isCancellationRequested) {
143return this.appWorker.stop();
144}
5514e287RedMickey6 years ago145return this.appWorker.start();
6e1bcd36RedMickey6 years ago146});
5514e287RedMickey6 years ago147});
148})
149.catch((err) => {
150reject(ErrorHelper.getInternalError(InternalErrorCode.CouldNotAttachToDebugger, err.message || err));
151})
152)
e23d1841RedMickey6 years ago153.catch(err => this.showError(err, response));
6e1bcd36RedMickey6 years ago154}
155
984ca036RedMickey6 years ago156protected async disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments, request?: DebugProtocol.Request): Promise<void> {
6e1bcd36RedMickey6 years ago157// The client is about to disconnect so first we need to stop app worker
158if (this.appWorker) {
159this.appWorker.stop();
160}
161
6e491635RedMickey6 years ago162this.onDidStartDebugSessionHandler.dispose();
163this.onDidTerminateDebugSessionHandler.dispose();
164
6e1bcd36RedMickey6 years ago165super.disconnectRequest(response, args, request);
4c757eebRedMickey6 years ago166}
167
5d47053fRedMickey6 years ago168protected establishDebugSession(attachArgs: IAttachRequestArgs, resolve?: (value?: void | PromiseLike<void> | undefined) => void): void {
1bdccb66RedMickey6 years ago169const attachConfiguration = JsDebugConfigAdapter.createDebuggingConfigForPureRN(
170attachArgs,
171this.appLauncher.getCdpProxyPort(),
172this.session.id
173);
a6562589RedMickey6 years ago174
175vscode.debug.startDebugging(
176this.appLauncher.getWorkspaceFolder(),
1bdccb66RedMickey6 years ago177attachConfiguration,
ebbd64f1RedMickey6 years ago178{
179parentSession: this.session,
180consoleMode: vscode.DebugConsoleMode.MergeWithParent,
181}
a6562589RedMickey6 years ago182)
183.then((childDebugSessionStarted: boolean) => {
184if (childDebugSessionStarted) {
185this.debugSessionStatus = DebugSessionStatus.ConnectionDone;
186this.setConnectionAllowedIfPossible();
187if (resolve) {
188this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
189resolve();
4c757eebRedMickey6 years ago190}
a6562589RedMickey6 years ago191} else {
4c757eebRedMickey6 years ago192this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
193this.setConnectionAllowedIfPossible();
194this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago195throw new Error("Cannot start child debug session");
196}
197},
198err => {
199this.debugSessionStatus = DebugSessionStatus.ConnectionFailed;
200this.setConnectionAllowedIfPossible();
4c757eebRedMickey6 years ago201this.resetFirstConnectionStatus();
a6562589RedMickey6 years ago202throw err;
203});
4c757eebRedMickey6 years ago204}
6e1bcd36RedMickey6 years ago205
4c757eebRedMickey6 years ago206private handleStartDebugSession(debugSession: vscode.DebugSession) {
207if (
208debugSession.configuration.rnDebugSessionId === this.session.id
209&& debugSession.type === this.pwaNodeSessionName
210) {
211this.nodeSession = debugSession;
212}
213}
214
215private handleTerminateDebugSession(debugSession: vscode.DebugSession) {
216if (
217debugSession.configuration.rnDebugSessionId === this.session.id
218&& debugSession.type === this.pwaNodeSessionName
219) {
ebbd64f1RedMickey6 years ago220if (this.debugSessionStatus === DebugSessionStatus.ConnectionPending) {
5d47053fRedMickey6 years ago221this.establishDebugSession(this.previousAttachArgs);
ebbd64f1RedMickey6 years ago222} else {
223this.session.customRequest(this.disconnectCommand, {forcedStop: true});
224}
4c757eebRedMickey6 years ago225}
226}
227
228private setConnectionAllowedIfPossible(): void {
229if (
230this.debugSessionStatus === DebugSessionStatus.ConnectionDone
231|| this.debugSessionStatus === DebugSessionStatus.ConnectionFailed
232) {
233this.debugSessionStatus = DebugSessionStatus.ConnectionAllowed;
234}
235}
236
237private resetFirstConnectionStatus(): void {
238if (this.debugSessionStatus === DebugSessionStatus.FirstConnectionPending) {
239this.debugSessionStatus = DebugSessionStatus.FirstConnection;
240}
241}
6e1bcd36RedMickey6 years ago242}