Skip to content

Commit 4eab13c

Browse files
committed
Check if the Terminal Shell Integration setting is changed
Because if it is, we'll need to restart the Extension Terminal (unless it was hidden at startup). Also add a test to ensure we can find VS Code's script (in case its location changes upstream).
1 parent e041019 commit 4eab13c

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/session.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class SessionManager implements Middleware {
8989
private sessionDetails: IEditorServicesSessionDetails | undefined;
9090
private sessionsFolder: vscode.Uri;
9191
private sessionStatus: SessionStatus = SessionStatus.NotStarted;
92+
private shellIntegrationEnabled: boolean;
9293
private startCancellationTokenSource: vscode.CancellationTokenSource | undefined;
9394
private suppressRestartPrompt = false;
9495
private versionDetails: IPowerShellVersionDetails | undefined;
@@ -109,6 +110,8 @@ export class SessionManager implements Middleware {
109110
// We have to override the scheme because it defaults to
110111
// 'vscode-userdata' which breaks UNC paths.
111112
this.sessionsFolder = vscode.Uri.joinPath(extensionContext.globalStorageUri.with({ scheme: "file" }), "sessions");
113+
this.shellIntegrationEnabled = vscode.workspace.getConfiguration("terminal.integrated.shellIntegration").get<boolean>("enabled") ?? false;
114+
112115
this.platformDetails = getPlatformDetails();
113116
this.HostName = hostName;
114117
this.DisplayName = displayName;
@@ -449,17 +452,22 @@ export class SessionManager implements Middleware {
449452
const settings = getSettings();
450453
this.logger.updateLogLevel(settings.developer.editorServicesLogLevel);
451454

455+
const shellIntegrationEnabled = vscode.workspace.getConfiguration("terminal.integrated.shellIntegration").get<boolean>("enabled");
456+
452457
// Detect any setting changes that would affect the session.
453-
if (!this.suppressRestartPrompt && this.sessionStatus === SessionStatus.Running &&
454-
(settings.cwd !== this.sessionSettings.cwd
455-
|| settings.powerShellDefaultVersion !== this.sessionSettings.powerShellDefaultVersion
456-
|| settings.developer.editorServicesLogLevel !== this.sessionSettings.developer.editorServicesLogLevel
457-
|| settings.developer.bundledModulesPath !== this.sessionSettings.developer.bundledModulesPath
458-
|| settings.developer.editorServicesWaitForDebugger !== this.sessionSettings.developer.editorServicesWaitForDebugger
459-
|| settings.developer.setExecutionPolicy !== this.sessionSettings.developer.setExecutionPolicy
460-
|| settings.integratedConsole.useLegacyReadLine !== this.sessionSettings.integratedConsole.useLegacyReadLine
461-
|| settings.integratedConsole.startInBackground !== this.sessionSettings.integratedConsole.startInBackground
462-
|| settings.integratedConsole.startLocation !== this.sessionSettings.integratedConsole.startLocation)) {
458+
if (!this.suppressRestartPrompt
459+
&& this.sessionStatus === SessionStatus.Running
460+
&& ((shellIntegrationEnabled !== this.shellIntegrationEnabled
461+
&& !settings.integratedConsole.startInBackground)
462+
|| settings.cwd !== this.sessionSettings.cwd
463+
|| settings.powerShellDefaultVersion !== this.sessionSettings.powerShellDefaultVersion
464+
|| settings.developer.editorServicesLogLevel !== this.sessionSettings.developer.editorServicesLogLevel
465+
|| settings.developer.bundledModulesPath !== this.sessionSettings.developer.bundledModulesPath
466+
|| settings.developer.editorServicesWaitForDebugger !== this.sessionSettings.developer.editorServicesWaitForDebugger
467+
|| settings.developer.setExecutionPolicy !== this.sessionSettings.developer.setExecutionPolicy
468+
|| settings.integratedConsole.useLegacyReadLine !== this.sessionSettings.integratedConsole.useLegacyReadLine
469+
|| settings.integratedConsole.startInBackground !== this.sessionSettings.integratedConsole.startInBackground
470+
|| settings.integratedConsole.startLocation !== this.sessionSettings.integratedConsole.startLocation)) {
463471

464472
this.logger.writeVerbose("Settings changed, prompting to restart...");
465473
const response = await vscode.window.showInformationMessage(
@@ -610,10 +618,6 @@ export class SessionManager implements Middleware {
610618
});
611619
};
612620

613-
// When Terminal Shell Integration is enabled, we pass the path to the script that the server should execute.
614-
// Passing an empty string implies integration is disabled.
615-
const shellIntegrationEnabled = vscode.workspace.getConfiguration("terminal.integrated.shellIntegration").get<boolean>("enabled");
616-
const shellIntegrationScript = path.join(vscode.env.appRoot, "out", "vs", "workbench", "contrib", "terminal", "browser", "media", "shellIntegration.ps1");
617621

618622
const clientOptions: LanguageClientOptions = {
619623
documentSelector: this.documentSelector,
@@ -624,10 +628,13 @@ export class SessionManager implements Middleware {
624628
// TODO: fileEvents: vscode.workspace.createFileSystemWatcher('**/.eslintrc')
625629
},
626630
// NOTE: Some settings are only applicable on startup, so we send them during initialization.
631+
// When Terminal Shell Integration is enabled, we pass the path to the script that the server should execute.
632+
// Passing an empty string implies integration is disabled.
627633
initializationOptions: {
628634
enableProfileLoading: this.sessionSettings.enableProfileLoading,
629635
initialWorkingDirectory: await validateCwdSetting(this.logger),
630-
shellIntegrationScript: shellIntegrationEnabled ? shellIntegrationScript : "",
636+
shellIntegrationScript: this.shellIntegrationEnabled
637+
? utils.ShellIntegrationScript : "",
631638
},
632639
errorHandler: {
633640
// Override the default error handler to prevent it from

src/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import vscode = require("vscode");
77

88
export const PowerShellLanguageId = "powershell";
99

10+
export const ShellIntegrationScript = path.join(vscode.env.appRoot, "out", "vs", "workbench", "contrib", "terminal", "browser", "media", "shellIntegration.ps1");
11+
1012
export function escapeSingleQuotes(p: string): string {
1113
return p.replace(new RegExp("'", "g"), "''");
1214
}

test/core/paths.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import assert from "assert";
55
import * as vscode from "vscode";
66
import { IPowerShellExtensionClient } from "../../src/features/ExternalApi";
77
import utils = require("../utils");
8-
import { checkIfDirectoryExists } from "../../src/utils";
8+
import { checkIfDirectoryExists, checkIfFileExists, ShellIntegrationScript } from "../../src/utils";
99

1010
describe("Path assumptions", function () {
1111
let globalStorageUri: vscode.Uri;
@@ -21,4 +21,9 @@ describe("Path assumptions", function () {
2121
it("Creates the log folder at the correct path", async function () {
2222
assert(await checkIfDirectoryExists(vscode.Uri.joinPath(globalStorageUri, "logs")));
2323
});
24+
25+
it("Finds the Terminal Shell Integration Script", async function () {
26+
// If VS Code changes the location of the script, we need to know ASAP (as it's not a public API).
27+
assert(await checkIfFileExists(ShellIntegrationScript));
28+
});
2429
});

0 commit comments

Comments
 (0)