Closed caparkaya closed 1 year ago
At the end of the first pass, It looks like it sets the global to null
$Global:__LastHistoryId = $LastHistoryEntry.Id # which is null
pass 1:
$Global:__LastHistoryId = -1
if ( $Global:__LastHistoryId -ne -1 ) { }
-1 -ne -1
$Global:__LastHistoryId = $null
pass 2:
if ( $Global:__LastHistoryId -ne -1 ) { }
$null -ne -1
LastHistoryEntry
has a value$LastHistoryEntry.Id -eq $Global:__LastHistoryId
<any int> -eq $null
$Global:__LastHistoryId = $LastHistoryEntry.Id
$Global:__LastHistoryId == <any int>
pass 3
My annotations are the 🦓
-comments. The rest is the truncated original.
# Prevent installing more than once per session
if (Test-Path variable:global:__VSCodeOriginalPrompt) {
return;
}
# Disable shell integration when the language mode is restricted
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
return;
}
$Global:__VSCodeOriginalPrompt = $function:Prompt
$Global:__LastHistoryId = -1
function Global:Prompt() {
$FakeCode = [int]!$global:?
$LastHistoryEntry = Get-History -Count 1
# 🦓 $LastHistoryEntry: is true null
# Skip finishing the command if the first command has not yet started
if ($Global:__LastHistoryId -ne -1) {
# 🦓 -1 -ne -1 == false
if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
# if entered 🦓 $null.Id -eq -1 == false
}
else {
# if entered then 🦓 that means: $Global:__LastHistoryId != -1
$Result = '..'
if ($LastHistoryEntry.CommandLine) {
#🦓 if $null , therefore: false
$CommandLine = $LastHistoryEntry.CommandLine
}
else {
$CommandLine = ""
}
}
}
$Result += $Global:__VSCodeOriginalPrompt.Invoke()
$Global:__LastHistoryId = $LastHistoryEntry.Id
# 🦓 $global:__lastHistoryId = $null
return $Result
}
Thank you @ninmonkey , I'm not much familiar with this process but I understand that you were able to repro this issue. Should I propose a code-based solution & open a pull-request myself or will someone from the dev-team do this within a time-frame upon availability? I may not be able to see how my proposed solution will affect in the big picture, that's the reason of my hesitation.
This should be resolved. The problem was actually the use of Set-StrictMode -Version Latest
being incompatible with how the VS Code team wrote their shell integration script. We worked around it by adding:
# NOTE: We disable strict mode for the scope of this function because it unhelpfully throws an
# error when $LastHistoryEntry is null, and is not otherwise useful.
Set-StrictMode -Off
to the prompt wrapper in that script.
This issue has been marked as answered and has not had any activity in a day. It has been automatically closed for housekeeping purposes.
Prerequisites
Steps to reproduce
Hello,
With the latest update of VS Code (v1.74.1) Powershell Extension (v2022.12.1), with Terminal > Integrated > Shell Integration: Enabled (Checked), Powershell Editor Services throws exception at startup if the powershell profile script file (Microsoft.VSCode_profile.ps1) contains
It fails in file "src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs", at line 567 (function Global:Prompt()):
because $LastHistoryEntry is null. Looking at the code fragment, I understand that at line 529 the assignment statement might result in $null value (or the resulting object might not contain Id property) which is not handled properly.
Expected behavior
Setting
in powershell profile script should not cause any startup errors inside Powershell Editor Services.
Actual behavior
At editor services startup (with Powershell profile loaded in terminal):
Error details
Environment data
Version
Powershell Extension Version: v2022.12.1, VSCode Version: 1.74.1
Visuals
No response