From c06ad1ecbbc60b38bebd256500efbd9a1de3863f Mon Sep 17 00:00:00 2001 From: Dmitry Dementiev Date: Sun, 22 Mar 2026 17:41:37 +0300 Subject: [PATCH] =?UTF-8?q?fix(windows):=20WT-=D1=81=D0=BA=D1=80=D0=B8?= =?UTF-8?q?=D0=BF=D1=82=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83?= =?UTF-8?q?=D0=B5=D1=82=20source=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20?= =?UTF-8?q?commandline=20=D0=B4=D0=BB=D1=8F=20PS7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Зачем: - на машине без pwsh в стандартном PATH профиль с `commandline = 'pwsh.exe'` ломался, потому что WT игнорировал динамический `source` в пользу явного `commandline`. - Что: - PS7 профиль теперь управляется через `source = Windows.Terminal.PowershellCore`, `commandline` удаляется. - добавлена проверка `Test-PowerShell7Available`: если pwsh не найден, профиль не создаётся. - добавлена `Remove-JsonPropertyValue` для идемпотентного удаления свойств. - magic string вынесен в константу `$PowerShellSource`. - Проверка: - `chezmoi -S "$HOME\dotfiles\chezmoi" apply --dry-run --verbose`. - `pwsh -NoProfile -Command "(Get-Content (Join-Path $env:LOCALAPPDATA 'Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json') -Raw | ConvertFrom-Json).profiles.list | Where-Object {$_.source -eq 'Windows.Terminal.PowershellCore'} | ConvertTo-Json"`. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../run_20-configure-windows-terminal.ps1 | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/chezmoi/.chezmoiscripts/run_20-configure-windows-terminal.ps1 b/chezmoi/.chezmoiscripts/run_20-configure-windows-terminal.ps1 index f717e11..4de0cdb 100644 --- a/chezmoi/.chezmoiscripts/run_20-configure-windows-terminal.ps1 +++ b/chezmoi/.chezmoiscripts/run_20-configure-windows-terminal.ps1 @@ -9,6 +9,7 @@ if (-not (Test-Path -LiteralPath $settingsPath)) { } $PowerShellGuid = '{574e775e-4f2a-5b96-ac1e-a2962a402336}' +$PowerShellSource = 'Windows.Terminal.PowershellCore' $LegacyPowerShellGuid = '{61c54bbd-c2c6-5271-96e7-009a87ff44bf}' $changed = $false @@ -74,6 +75,30 @@ function Set-JsonPropertyValue { } } +function Remove-JsonPropertyValue { + param( + [Parameter(Mandatory = $true)] + $Object, + [Parameter(Mandatory = $true)] + [string]$Name + ) + + $Property = $Object.PSObject.Properties[$Name] + if ($null -ne $Property) { + [void]$Object.PSObject.Properties.Remove($Name) + $script:changed = $true + } +} + +function Test-PowerShell7Available { + try { + return $null -ne (Get-Command pwsh -ErrorAction SilentlyContinue | Select-Object -First 1) + } + catch { + return $false + } +} + function Test-IsPowerShell7Profile { param( [Parameter(Mandatory = $true)] @@ -84,7 +109,7 @@ function Test-IsPowerShell7Profile { $Name = [string](Get-JsonPropertyValue -Object $Profile -Name name) $CommandLine = [string](Get-JsonPropertyValue -Object $Profile -Name commandline) - if ($Source -eq 'Windows.Terminal.PowershellCore') { + if ($Source -eq $PowerShellSource) { return $true } @@ -108,7 +133,7 @@ function Test-IsDisposablePowerShell7Profile { return ( $Name -match '^(PowerShell|PowerShell 7)$' -and ( - $Source -eq 'Windows.Terminal.PowershellCore' -or + $Source -eq $PowerShellSource -or $CommandLine -match '(?i)(^|[\\/ ])pwsh(?:\.exe)?(?:\s|$)' ) ) @@ -166,7 +191,7 @@ for ($i = 0; $i -lt $profiles.Count; $i++) { $powerShell7Index = $null foreach ($candidateIndex in $powerShell7CandidateIndices) { $candidate = $profiles[$candidateIndex] - if ([string](Get-JsonPropertyValue -Object $candidate -Name source) -eq 'Windows.Terminal.PowershellCore') { + if ([string](Get-JsonPropertyValue -Object $candidate -Name source) -eq $PowerShellSource) { $powerShell7Index = $candidateIndex break } @@ -177,28 +202,33 @@ if ($null -eq $powerShell7Index -and $powerShell7CandidateIndices.Count -gt 0) { } if ($null -eq $powerShell7Index) { - $profiles += [pscustomobject]@{ - commandline = 'pwsh.exe' - guid = $PowerShellGuid - hidden = $false - name = 'PowerShell 7' - startingDirectory = '%USERPROFILE%' + if (Test-PowerShell7Available) { + # Create a dynamic-profile override instead of a manual pwsh.exe launcher. + $profiles += [pscustomobject]@{ + guid = $PowerShellGuid + hidden = $false + name = 'PowerShell 7' + source = $PowerShellSource + startingDirectory = '%USERPROFILE%' + } + $powerShell7Index = $profiles.Count - 1 + $changed = $true + } + else { + Write-Warning 'PowerShell 7 was not detected; leaving Windows Terminal default profile unchanged.' } - $powerShell7Index = $profiles.Count - 1 - $changed = $true } -$powerShell7Profile = $profiles[$powerShell7Index] -if ([string]::IsNullOrWhiteSpace([string](Get-JsonPropertyValue -Object $powerShell7Profile -Name guid))) { +if ($null -ne $powerShell7Index) { + $powerShell7Profile = $profiles[$powerShell7Index] Set-JsonPropertyValue -Object $powerShell7Profile -Name guid -Value $PowerShellGuid + Set-JsonPropertyValue -Object $powerShell7Profile -Name source -Value $PowerShellSource + Remove-JsonPropertyValue -Object $powerShell7Profile -Name commandline + Set-JsonPropertyValue -Object $powerShell7Profile -Name name -Value 'PowerShell 7' + Set-JsonPropertyValue -Object $powerShell7Profile -Name hidden -Value $false + Set-JsonPropertyValue -Object $powerShell7Profile -Name startingDirectory -Value '%USERPROFILE%' + Set-JsonPropertyValue -Object $settings -Name defaultProfile -Value ([string](Get-JsonPropertyValue -Object $powerShell7Profile -Name guid)) } -if ($null -eq (Get-JsonPropertyValue -Object $powerShell7Profile -Name commandline) -and [string](Get-JsonPropertyValue -Object $powerShell7Profile -Name source) -ne 'Windows.Terminal.PowershellCore') { - Set-JsonPropertyValue -Object $powerShell7Profile -Name commandline -Value 'pwsh.exe' -} -Set-JsonPropertyValue -Object $powerShell7Profile -Name name -Value 'PowerShell 7' -Set-JsonPropertyValue -Object $powerShell7Profile -Name hidden -Value $false -Set-JsonPropertyValue -Object $powerShell7Profile -Name startingDirectory -Value '%USERPROFILE%' -Set-JsonPropertyValue -Object $settings -Name defaultProfile -Value ([string](Get-JsonPropertyValue -Object $powerShell7Profile -Name guid)) if ($null -ne $legacyPowerShellIndex) { $legacyPowerShellProfile = $profiles[$legacyPowerShellIndex]