Skip to content

Commit 1c9601c

Browse files
Add PowerShell version logging for activation debugging (#851)
Implementing better PowerShell version logging for activation debugging as requested in issue #706. The issue stems from the PR review comment (#693 (comment)) suggesting that logging PowerShell version information would be valuable for debugging activation failures, especially given the differences between Windows PowerShell 5.x and PowerShell 7+. ## Changes Made: - [x] Analyze current PowerShell activation flow in pwshStartup.ts and pwshEnvs.ts - [x] Examine existing logging patterns and utilities - [x] Test current build/compile state of project - [x] Create utility function to detect PowerShell version via `$PSVersionTable.PSVersion.Major` - [x] Add PowerShell version logging to relevant activation code paths - [x] Add version logging for both conda and non-conda activation scenarios - [x] Ensure logging follows existing patterns (traceInfo/traceVerbose) - [x] Test changes and verify proper logging output - [x] Use shorter -c flag instead of -Command for PowerShell commands ## Implementation Details: 1. **Added `getPowerShellVersion()` function** - Detects PowerShell major version using `$PSVersionTable.PSVersion.Major` command with `-c` flag 2. **Enhanced installation logging** - Now logs PowerShell version when shells are detected (e.g., "SHELL: pwsh is installed (version 7)") 3. **Enhanced activation script error messages** - PowerShell errors now include version info (e.g., "Failed to activate Python environment (PowerShell 7): error details") ## Testing: - All existing unit tests pass (128 passing) - Code compiles successfully with no lint errors - Manual testing confirms PowerShell version detection works correctly These changes provide better diagnostic information for debugging PowerShell activation issues without affecting existing functionality. Fixes #706. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eleanorjboyd <[email protected]>
1 parent 47206e3 commit 1c9601c

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
@@ -58,6 +58,27 @@ async function isPowerShellInstalled(shell: string): Promise<boolean> {
5858
}
5959
}
6060

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

0 commit comments

Comments
 (0)