Skip to content

Commit b0f85f3

Browse files
committed
Fix handling the Extension Terminal failing to start
Now we dispose on the exited event (so we don't have to do it in the catch around `start`). We subscribe the `onDidCloseTerminal` event right before creating the terminal so it fires even if the terminal immediately closes. We check if the terminal is still defined (not crashed) when waiting for the session file.
1 parent 686b4c9 commit b0f85f3

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/process.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,28 @@ export class PowerShellProcess {
111111
hideFromUser: this.sessionSettings.integratedConsole.startInBackground,
112112
};
113113

114+
// Subscribe a log event for when the terminal closes (this fires for
115+
// all terminals and the event itself checks if it's our terminal). This
116+
// subscription should happen before we create the terminal so if it
117+
// fails immediately, the event fires.
118+
this.consoleCloseSubscription = vscode.window.onDidCloseTerminal((terminal) => this.onTerminalClose(terminal));
119+
114120
this.consoleTerminal = vscode.window.createTerminal(terminalOptions);
115121

116122
const pwshName = path.basename(this.exePath);
117123
this.logger.write(`${pwshName} started.`);
118124

125+
// Log that the PowerShell terminal process has been started
126+
const pid = await this.getPid();
127+
this.logTerminalPid(pid ?? 0, pwshName);
128+
119129
if (this.sessionSettings.integratedConsole.showOnStartup
120130
&& !this.sessionSettings.integratedConsole.startInBackground) {
121131
// We still need to run this to set the active terminal to the extension terminal.
122132
this.consoleTerminal.show(true);
123133
}
124134

125-
// Start the language client
126-
const sessionDetails = await this.waitForSessionFile();
127-
128-
// Subscribe a log event for when the terminal closes
129-
this.consoleCloseSubscription = vscode.window.onDidCloseTerminal((terminal) => this.onTerminalClose(terminal));
130-
131-
// Log that the PowerShell terminal process has been started
132-
const pid = await this.getPid();
133-
this.logTerminalPid(pid ?? 0, pwshName);
134-
135-
return sessionDetails;
135+
return await this.waitForSessionFile();
136136
}
137137

138138
// This function should only be used after a failure has occurred because it is slow!
@@ -154,7 +154,7 @@ export class PowerShellProcess {
154154

155155
public async dispose(): Promise<void> {
156156
// Clean up the session file
157-
this.logger.write("Terminating PowerShell process...");
157+
this.logger.write("Disposing PowerShell Extension Terminal...");
158158

159159
this.consoleTerminal?.dispose();
160160
this.consoleTerminal = undefined;
@@ -199,8 +199,8 @@ export class PowerShellProcess {
199199
private static async deleteSessionFile(sessionFilePath: vscode.Uri): Promise<void> {
200200
try {
201201
await vscode.workspace.fs.delete(sessionFilePath);
202-
} catch (e) {
203-
// TODO: Be more specific about what we're catching
202+
} catch {
203+
// We don't care about any reasons for it to fail.
204204
}
205205
}
206206

@@ -212,6 +212,12 @@ export class PowerShellProcess {
212212
// Check every 2 seconds
213213
this.logger.write("Waiting for session file...");
214214
for (let i = numOfTries; i > 0; i--) {
215+
if (this.consoleTerminal === undefined) {
216+
const err = "PowerShell Extension Terminal didn't start!";
217+
this.logger.write(err);
218+
throw new Error(err);
219+
}
220+
215221
if (await utils.checkIfFileExists(this.sessionFilePath)) {
216222
this.logger.write("Session file found!");
217223
const sessionDetails = await PowerShellProcess.readSessionFile(this.sessionFilePath);
@@ -237,7 +243,8 @@ export class PowerShellProcess {
237243
return;
238244
}
239245

240-
this.logger.write("powershell.exe terminated or terminal UI was closed");
246+
this.logger.write("PowerShell process terminated or Extension Terminal was closed!");
241247
this.onExitedEmitter.fire();
248+
void this.dispose();
242249
}
243250
}

src/session.ts

-3
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,6 @@ export class SessionManager implements Middleware {
457457
try {
458458
this.sessionDetails = await languageServerProcess.start("EditorServices");
459459
} catch (err) {
460-
// We should kill the process in case it's stuck.
461-
void languageServerProcess.dispose();
462-
463460
// PowerShell never started, probably a bad version!
464461
const version = await languageServerProcess.getVersionCli();
465462
let shouldUpdate = true;

0 commit comments

Comments
 (0)