Skip to content

Commit 9e46c55

Browse files
Capture and report Extension Terminal exit code on startup failure (#5526)
When the PowerShell process exits during startup -- as in #5031, where pwsh terminated ~3 seconds after spawn -- we threw away the terminal's exit status and surfaced a generic "PowerShell Language Server process didn't start!" message. That's unactionable: the logs show a *fast process exit*, not a slow start, and we had no way to know *why* pwsh died. We now capture `terminal.exitStatus` in `onTerminalClose` before disposing the process, expose it via `getExitStatus()`, and include a non-zero exit code in both the log warning and the failure notification ("...exited with code: N before connecting!"). When the code is `undefined` (the user or VS Code closed the terminal, or it simply timed out) we fall back to the original wording. I don't claim this fixes the underlying exit -- that's almost certainly environmental (AV/EDR, profile, module load) -- but it turns an undiagnosable report into a diagnosable one. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e301252 commit 9e46c55

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

‎src/process.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class PowerShellProcess {
2222
private pid?: number;
2323
private pidUpdateEmitter?: vscode.EventEmitter<number | undefined>;
2424

25+
// Captured when the terminal closes so callers can report why the process
26+
// exited (e.g. a non-zero exit code) instead of a generic failure message.
27+
private exitStatus?: vscode.TerminalExitStatus;
28+
2529
constructor(
2630
public exePath: string,
2731
private bundledModulesPath: string,
@@ -223,6 +227,13 @@ export class PowerShellProcess {
223227
return await this.consoleTerminal?.processId;
224228
}
225229

230+
// Returns the exit status of the terminal, if it has closed. This is
231+
// captured before the terminal is disposed so callers can report a non-zero
232+
// exit code when the process terminates before connecting.
233+
public getExitStatus(): vscode.TerminalExitStatus | undefined {
234+
return this.exitStatus;
235+
}
236+
226237
public showTerminal(preserveFocus?: boolean): void {
227238
this.consoleTerminal?.show(preserveFocus);
228239
}
@@ -343,8 +354,16 @@ export class PowerShellProcess {
343354
return;
344355
}
345356

357+
// Capture the exit status before disposing so it can be reported. A
358+
// non-zero code means the process failed to start; `undefined` means
359+
// the user (or VS Code) closed the terminal.
360+
this.exitStatus = terminal.exitStatus;
361+
const code = this.exitStatus?.code;
362+
346363
this.logger.writeWarning(
347-
`PowerShell process terminated or Extension Terminal was closed, PID: ${this.pid}`,
364+
`PowerShell process terminated or Extension Terminal was closed${
365+
code !== undefined ? ` with exit code: ${code}` : ""
366+
}, PID: ${this.pid}`,
348367
);
349368
this.dispose();
350369
}

‎src/session.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,15 @@ export class SessionManager implements Middleware {
798798
);
799799
} else {
800800
shouldUpdate = false;
801+
// If the process terminated before connecting, VS Code gives us the
802+
// terminal's exit code, which is far more actionable than a generic
803+
// failure. A `undefined` code means it timed out or was closed
804+
// without reporting one.
805+
const exitCode = powerShellProcess.getExitStatus()?.code;
801806
void this.setSessionFailedOpenBug(
802-
"PowerShell Language Server process didn't start!",
807+
exitCode !== undefined && exitCode !== 0
808+
? `PowerShell Language Server process exited with code: ${exitCode} before connecting! Check the PowerShell output for errors.`
809+
: "PowerShell Language Server process didn't start!",
803810
);
804811
}
805812

‎test/core/process.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import * as assert from "assert";
5+
import Sinon from "sinon";
6+
import * as vscode from "vscode";
7+
import { PowerShellProcess } from "../../src/process";
8+
import { stubInterface, testLogger } from "../utils";
9+
10+
describe("PowerShellProcess", () => {
11+
afterEach(() => {
12+
Sinon.restore();
13+
});
14+
15+
function createProcess(): PowerShellProcess {
16+
return new PowerShellProcess(
17+
"pwsh",
18+
"modules",
19+
false,
20+
false,
21+
testLogger,
22+
vscode.Uri.file("C:/tmp"),
23+
"",
24+
vscode.Uri.file("C:/tmp/session.json"),
25+
);
26+
}
27+
28+
// These pokes mirror the `manager as unknown as {...}` style used in
29+
// `session.test.ts` to exercise otherwise private startup behavior.
30+
function internalsOf(process: PowerShellProcess): {
31+
consoleTerminal: vscode.Terminal | undefined;
32+
onTerminalClose: (terminal: vscode.Terminal) => void;
33+
} {
34+
return process as unknown as {
35+
consoleTerminal: vscode.Terminal | undefined;
36+
onTerminalClose: (terminal: vscode.Terminal) => void;
37+
};
38+
}
39+
40+
it("has no exit status before its terminal closes", () => {
41+
const process = createProcess();
42+
assert.equal(process.getExitStatus(), undefined);
43+
});
44+
45+
it("captures and logs the exit status when its terminal closes", () => {
46+
const warnSpy = Sinon.spy(testLogger, "writeWarning");
47+
const process = createProcess();
48+
const exitStatus: vscode.TerminalExitStatus = {
49+
code: 1,
50+
reason: vscode.TerminalExitReason.Process,
51+
};
52+
const terminal = stubInterface<vscode.Terminal>({
53+
exitStatus,
54+
dispose: () => {
55+
return;
56+
},
57+
});
58+
59+
// Track the terminal, then simulate VS Code firing its close event.
60+
const internals = internalsOf(process);
61+
internals.consoleTerminal = terminal;
62+
internals.onTerminalClose(terminal);
63+
64+
assert.deepEqual(process.getExitStatus(), exitStatus);
65+
assert.ok(
66+
warnSpy.calledWithMatch("exit code: 1"),
67+
"should log the non-zero exit code",
68+
);
69+
});
70+
71+
it("ignores close events from unrelated terminals", () => {
72+
const process = createProcess();
73+
const internals = internalsOf(process);
74+
internals.consoleTerminal = stubInterface<vscode.Terminal>({
75+
dispose: () => {
76+
return;
77+
},
78+
});
79+
80+
const otherTerminal = stubInterface<vscode.Terminal>({
81+
exitStatus: {
82+
code: 1,
83+
reason: vscode.TerminalExitReason.Process,
84+
},
85+
});
86+
internals.onTerminalClose(otherTerminal);
87+
88+
assert.equal(process.getExitStatus(), undefined);
89+
});
90+
});

0 commit comments

Comments
 (0)