Skip to content

Commit 87e530d

Browse files
committed
serverReady: try to listen only on the correct terminal
1 parent b1300a0 commit 87e530d

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

extensions/debug-server-ready/src/extension.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
//import * as nls from 'vscode-nls';
87
import * as util from 'util';
98

10-
119
const PATTERN = 'listening on.* (https?://\\S+|[0-9]+)'; // matches "listening on port 3000" or "Now listening on: https://localhost:5001"
1210
const URI_FORMAT = 'http://localhost:%s';
1311
const WEB_ROOT = '${workspaceFolder}';
@@ -23,6 +21,7 @@ class ServerReadyDetector extends vscode.Disposable {
2321
static detectors = new Map<vscode.DebugSession, ServerReadyDetector>();
2422

2523
private hasFired = false;
24+
private shellPid?: number;
2625
private regexp: RegExp;
2726
private disposables: vscode.Disposable[] = [];
2827

@@ -46,6 +45,13 @@ class ServerReadyDetector extends vscode.Disposable {
4645
}
4746
}
4847

48+
static rememberShellPid(session: vscode.DebugSession, pid: number) {
49+
let detector = ServerReadyDetector.detectors.get(session);
50+
if (detector) {
51+
detector.shellPid = pid;
52+
}
53+
}
54+
4955
private constructor(private session: vscode.DebugSession) {
5056
super(() => this.internalDispose());
5157

@@ -57,9 +63,19 @@ class ServerReadyDetector extends vscode.Disposable {
5763
this.disposables = [];
5864
}
5965

60-
trackTerminals() {
61-
// TODO: listen only on the Terminal associated with the debug session
62-
vscode.window.terminals.forEach(terminal => {
66+
async trackTerminals() {
67+
68+
let terminals: vscode.Terminal[] = [];
69+
70+
// either find the terminal where the debug is started with "runInTerminal" or use all terminals
71+
for (let terminal of vscode.window.terminals) {
72+
if (!this.shellPid || await terminal.processId === this.shellPid) {
73+
terminals.push(terminal);
74+
}
75+
}
76+
this.shellPid = undefined;
77+
78+
terminals.forEach(terminal => {
6379
this.disposables.push(terminal.onDidWriteData(s => {
6480
this.detectPattern(s);
6581
}));
@@ -149,11 +165,23 @@ function startTrackerForType(context: vscode.ExtensionContext, type: string) {
149165
createDebugAdapterTracker(session: vscode.DebugSession) {
150166
const detector = ServerReadyDetector.start(session);
151167
if (detector) {
168+
let runInTerminalRequestSeq: number | undefined;
152169
return {
153170
onDidSendMessage: m => {
154171
if (m.type === 'event' && m.event === 'output' && m.body.output) {
155172
detector.detectPattern(m.body.output);
156173
}
174+
if (m.type === 'request' && m.command === 'runInTerminal' && m.arguments) {
175+
if (m.arguments.kind === 'integrated') {
176+
runInTerminalRequestSeq = m.seq; // remember this to find matching response
177+
}
178+
}
179+
},
180+
onWillReceiveMessage: m => {
181+
if (runInTerminalRequestSeq && m.type === 'response' && m.command === 'runInTerminal' && m.body && runInTerminalRequestSeq === m.request_seq) {
182+
runInTerminalRequestSeq = undefined;
183+
ServerReadyDetector.rememberShellPid(session, m.body.shellProcessId);
184+
}
157185
}
158186
};
159187
}

0 commit comments

Comments
 (0)