From 4f96cc0f7c731ea6cb88a0cdf6c496faafa1683a Mon Sep 17 00:00:00 2001 From: lnu Date: Wed, 14 Apr 2021 00:15:32 +0200 Subject: [PATCH] fix: wrong stack count when init from Set-PoshPrompt --- packages/powershell/oh-my-posh/oh-my-posh.psm1 | 5 +++++ src/init/omp.ps1 | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1 index bf3be814..4a98de46 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psm1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1 @@ -56,6 +56,11 @@ function Set-PoshPrompt { $config = "$PSScriptRoot/themes/jandedobbeleer.omp.json" } + # Workaround for get-location/push-location/pop-location from within a module + # https://github.com/PowerShell/PowerShell/issues/12868 + # https://github.com/JanDeDobbeleer/oh-my-posh2/issues/113 + $global:omp_global_sessionstate = $PSCmdlet.SessionState + $poshCommand = Get-PoshCommand Invoke-Expression (& $poshCommand --init --shell=pwsh --config="$config") } diff --git a/src/init/omp.ps1 b/src/init/omp.ps1 index 49ef7ed2..18395a9e 100644 --- a/src/init/omp.ps1 +++ b/src/init/omp.ps1 @@ -65,7 +65,12 @@ function global:Initialize-ModuleSupport { } } - $stackCount = (Get-Location -Stack).Count + $stackCount = 0 + try { + $stackCount = ($global:omp_global_sessionstate).path.locationstack('').count + } + catch {} + $executionTime = -1 $history = Get-History -ErrorAction Ignore -Count 1 if ($null -ne $history -and $null -ne $history.EndExecutionTime -and $null -ne $history.StartExecutionTime -and $global:omp_lastHistoryId -ne $history.Id) {