Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/features/terminal/shells/pwsh/pwshStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ async function isPowerShellInstalled(shell: string): Promise<boolean> {
}
}

/**
* Detects the major version of PowerShell by executing a version query command.
* This helps with debugging activation issues since PowerShell 5.x and 7+ have different behaviors.
* @param shell The PowerShell executable name ('powershell' for Windows PowerShell or 'pwsh' for PowerShell Core/7+)
* @returns Promise resolving to the major version number as a string, or undefined if detection fails
*/
async function getPowerShellVersion(shell: 'powershell' | 'pwsh'): Promise<string | undefined> {
try {
const command = `${shell} -Command "\$PSVersionTable.PSVersion.Major"`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, I think you can even make it pwsh -c '$PSVersionTable.PSVersion.Major'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot follow through on comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed to use -c flag in commit 3f8a2b4.

const versionOutput = await runCommand(command);
if (versionOutput && !isNaN(Number(versionOutput))) {
return versionOutput;
}
traceVerbose(`Failed to parse PowerShell version from output: ${versionOutput}`);
return undefined;
} catch (err) {
traceVerbose(`Failed to get PowerShell version for ${shell}`, err);
return undefined;
}
}

async function getProfileForShell(shell: 'powershell' | 'pwsh'): Promise<string> {
const cachedPath = getProfilePathCache(shell);
if (cachedPath) {
Expand Down Expand Up @@ -124,7 +145,8 @@ function getActivationContent(): string {
' try {',
` Invoke-Expression $env:${POWERSHELL_ENV_KEY}`,
' } catch {',
` Write-Error "Failed to activate Python environment: $_" -ErrorAction Continue`,
` $psVersion = $PSVersionTable.PSVersion.Major`,
` Write-Error "Failed to activate Python environment (PowerShell $psVersion): $_" -ErrorAction Continue`,
' }',
' }',
'}',
Expand Down Expand Up @@ -219,6 +241,12 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
results.set(shell, this._isPs5Installed);
} else {
const isInstalled = await isPowerShellInstalled(shell);
if (isInstalled) {
// Log PowerShell version for debugging activation issues
const version = await getPowerShellVersion(shell);
const versionText = version ? ` (version ${version})` : ' (version unknown)';
traceInfo(`SHELL: ${shell} is installed${versionText}`);
}
if (shell === 'pwsh') {
this._isPwshInstalled = isInstalled;
} else {
Expand Down
Loading