Skip to content

Commit 509c9a3

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 a56206b commit 509c9a3

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/session.ts

+23-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 = false;
9293
private startCancellationTokenSource: vscode.CancellationTokenSource | undefined;
9394
private suppressRestartPrompt = false;
9495
private versionDetails: IPowerShellVersionDetails | undefined;
@@ -109,6 +110,7 @@ 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+
112114
this.platformDetails = getPlatformDetails();
113115
this.HostName = hostName;
114116
this.DisplayName = displayName;
@@ -189,6 +191,9 @@ export class SessionManager implements Middleware {
189191
// Migrate things.
190192
await this.migrateWhitespaceAroundPipeSetting();
191193

194+
// Update non-PowerShell settings.
195+
this.shellIntegrationEnabled = vscode.workspace.getConfiguration("terminal.integrated.shellIntegration").get<boolean>("enabled") ?? false;
196+
192197
// Find the PowerShell executable to use for the server.
193198
this.PowerShellExeDetails = await this.findPowerShell();
194199

@@ -447,19 +452,23 @@ export class SessionManager implements Middleware {
447452

448453
private async onConfigurationUpdated(): Promise<void> {
449454
const settings = getSettings();
455+
const shellIntegrationEnabled = vscode.workspace.getConfiguration("terminal.integrated.shellIntegration").get<boolean>("enabled");
450456
this.logger.updateLogLevel(settings.developer.editorServicesLogLevel);
451457

452458
// 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)) {
459+
if (!this.suppressRestartPrompt
460+
&& this.sessionStatus === SessionStatus.Running
461+
&& ((shellIntegrationEnabled !== this.shellIntegrationEnabled
462+
&& !settings.integratedConsole.startInBackground)
463+
|| settings.cwd !== this.sessionSettings.cwd
464+
|| settings.powerShellDefaultVersion !== this.sessionSettings.powerShellDefaultVersion
465+
|| settings.developer.editorServicesLogLevel !== this.sessionSettings.developer.editorServicesLogLevel
466+
|| settings.developer.bundledModulesPath !== this.sessionSettings.developer.bundledModulesPath
467+
|| settings.developer.editorServicesWaitForDebugger !== this.sessionSettings.developer.editorServicesWaitForDebugger
468+
|| settings.developer.setExecutionPolicy !== this.sessionSettings.developer.setExecutionPolicy
469+
|| settings.integratedConsole.useLegacyReadLine !== this.sessionSettings.integratedConsole.useLegacyReadLine
470+
|| settings.integratedConsole.startInBackground !== this.sessionSettings.integratedConsole.startInBackground
471+
|| settings.integratedConsole.startLocation !== this.sessionSettings.integratedConsole.startLocation)) {
463472

464473
this.logger.writeVerbose("Settings changed, prompting to restart...");
465474
const response = await vscode.window.showInformationMessage(
@@ -610,10 +619,6 @@ export class SessionManager implements Middleware {
610619
});
611620
};
612621

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");
617622

618623
const clientOptions: LanguageClientOptions = {
619624
documentSelector: this.documentSelector,
@@ -624,10 +629,13 @@ export class SessionManager implements Middleware {
624629
// TODO: fileEvents: vscode.workspace.createFileSystemWatcher('**/.eslintrc')
625630
},
626631
// NOTE: Some settings are only applicable on startup, so we send them during initialization.
632+
// When Terminal Shell Integration is enabled, we pass the path to the script that the server should execute.
633+
// Passing an empty string implies integration is disabled.
627634
initializationOptions: {
628635
enableProfileLoading: this.sessionSettings.enableProfileLoading,
629636
initialWorkingDirectory: await validateCwdSetting(this.logger),
630-
shellIntegrationScript: shellIntegrationEnabled ? shellIntegrationScript : "",
637+
shellIntegrationScript: this.shellIntegrationEnabled
638+
? utils.ShellIntegrationScript : "",
631639
},
632640
errorHandler: {
633641
// 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)