Skip to content

Commit 2c3f829

Browse files
Copiloteleanorjboyd
andcommitted
Add PowerShell version logging for activation debugging
Co-authored-by: eleanorjboyd <[email protected]>
1 parent 4f0d430 commit 2c3f829

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/features/terminal/shells/pwsh/pwshStartup.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ async function isPowerShellInstalled(shell: string): Promise<boolean> {
5757
}
5858
}
5959

60+
/**
61+
* Detects the major version of PowerShell by executing a version query command.
62+
* This helps with debugging activation issues since PowerShell 5.x and 7+ have different behaviors.
63+
* @param shell The PowerShell executable name ('powershell' for Windows PowerShell or 'pwsh' for PowerShell Core/7+)
64+
* @returns Promise resolving to the major version number as a string, or undefined if detection fails
65+
*/
66+
async function getPowerShellVersion(shell: 'powershell' | 'pwsh'): Promise<string | undefined> {
67+
try {
68+
const command = `${shell} -Command "\$PSVersionTable.PSVersion.Major"`;
69+
const versionOutput = await runCommand(command);
70+
if (versionOutput && !isNaN(Number(versionOutput))) {
71+
return versionOutput;
72+
}
73+
traceVerbose(`Failed to parse PowerShell version from output: ${versionOutput}`);
74+
return undefined;
75+
} catch (err) {
76+
traceVerbose(`Failed to get PowerShell version for ${shell}`, err);
77+
return undefined;
78+
}
79+
}
80+
6081
async function getProfileForShell(shell: 'powershell' | 'pwsh'): Promise<string> {
6182
const cachedPath = getProfilePathCache(shell);
6283
if (cachedPath) {
@@ -124,7 +145,8 @@ function getActivationContent(): string {
124145
' try {',
125146
` Invoke-Expression $env:${POWERSHELL_ENV_KEY}`,
126147
' } catch {',
127-
` Write-Error "Failed to activate Python environment: $_" -ErrorAction Continue`,
148+
` $psVersion = $PSVersionTable.PSVersion.Major`,
149+
` Write-Error "Failed to activate Python environment (PowerShell $psVersion): $_" -ErrorAction Continue`,
128150
' }',
129151
' }',
130152
'}',
@@ -219,6 +241,12 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
219241
results.set(shell, this._isPs5Installed);
220242
} else {
221243
const isInstalled = await isPowerShellInstalled(shell);
244+
if (isInstalled) {
245+
// Log PowerShell version for debugging activation issues
246+
const version = await getPowerShellVersion(shell);
247+
const versionText = version ? ` (version ${version})` : ' (version unknown)';
248+
traceInfo(`SHELL: ${shell} is installed${versionText}`);
249+
}
222250
if (shell === 'pwsh') {
223251
this._isPwshInstalled = isInstalled;
224252
} else {

0 commit comments

Comments
 (0)