Skip to content

Commit fc0885e

Browse files
authored
Save started component debug sessions in map URI->DebugSession (#1586)
Do not allow to start debug session twice for the same component. Signed-off-by: Denis Golovin <dgolovin@redhat.com>
1 parent 367b297 commit fc0885e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ async function verifyBundledBinaries(): Promise<any> {
5151
export async function activate(extensionContext: ExtensionContext): Promise<any> {
5252
migrateFromOdo018();
5353
Cluster.extensionContext = extensionContext;
54-
Component.extensionContext = extensionContext;
5554
TokenStore.extensionContext = extensionContext;
5655
const disposable = [
5756
...(await registerCommands(
@@ -69,6 +68,7 @@ export async function activate(extensionContext: ExtensionContext): Promise<any>
6968
commands.executeCommand('extension.vsKubernetesUseNamespace', context),
7069
),
7170
OpenShiftExplorer.getInstance(),
71+
...Component.init(extensionContext)
7272
];
7373
disposable.forEach((value) => extensionContext.subscriptions.push(value));
7474

src/openshift/component.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/* eslint-disable @typescript-eslint/no-var-requires */
77

8-
import { window, commands, QuickPickItem, Uri, workspace, ExtensionContext, debug, DebugConfiguration, extensions, ProgressLocation } from 'vscode';
8+
import { window, commands, QuickPickItem, Uri, workspace, ExtensionContext, debug, DebugConfiguration, extensions, ProgressLocation, DebugSession, Disposable } from 'vscode';
99
import { ChildProcess , exec } from 'child_process';
1010
import { isURL } from 'validator';
1111
import OpenShiftItem, { selectTargetApplication, selectTargetComponent } from './openshiftItem';
@@ -29,6 +29,23 @@ const waitPort = require('wait-port');
2929

3030
export class Component extends OpenShiftItem {
3131
public static extensionContext: ExtensionContext;
32+
public static debugSessions: Map<string, DebugSession> = new Map();
33+
34+
public static init(context: ExtensionContext): Disposable[] {
35+
Component.extensionContext = context;
36+
return [
37+
debug.onDidStartDebugSession((session) => {
38+
if (session.configuration.contextPath) {
39+
Component.debugSessions.set(session.configuration.contextPath.fsPath, session);
40+
}
41+
}),
42+
debug.onDidTerminateDebugSession((session) => {
43+
if (session.configuration?.contextPath) {
44+
Component.debugSessions.delete(session.configuration.contextPath.fsPath);
45+
}
46+
})
47+
];
48+
}
3249

3350
static async getOpenshiftData(context: OpenShiftObject): Promise<OpenShiftObject> {
3451
return Component.getOpenShiftCmdData(context,
@@ -630,6 +647,13 @@ export class Component extends OpenShiftItem {
630647
}
631648

632649
static async startDebugger(component: OpenShiftObject): Promise<string | undefined> {
650+
if (Component.debugSessions.get(component.contextPath.fsPath)) {
651+
const choice = await window.showWarningMessage(`Debugger session is already running for ${component.getName()}.`, 'Show \'Run and Debug\' view');
652+
if (choice) {
653+
commands.executeCommand('workbench.view.debug');
654+
}
655+
return null;
656+
}
633657
const components = await Component.odo.getComponentTypesJson();
634658
const componentBuilder = components.find((builder) => builder.metadata.name === component.builderImage.name);
635659
const imageStreamRef = await Component.odo.getImageStreamRef(componentBuilder.metadata.name, componentBuilder.metadata.namespace);
@@ -704,11 +728,12 @@ export class Component extends OpenShiftItem {
704728
}
705729
});
706730
cp.stderr.on('data', (data: string) => {
707-
if (!`${data}`.includes('address already in use')) {
731+
if (!`${data}`.includes('the local debug port 5858 is not free')) {
708732
reject(data);
709733
}
710734
});
711735
}).then((result) => {
736+
config.contextPath = component.contextPath;
712737
config.port = result;
713738
config.odoPid = cp.pid;
714739
return debug.startDebugging(workspace.getWorkspaceFolder(component.contextPath), config);

0 commit comments

Comments
 (0)