From f29508e98658d6248d835863a9cd96e520ec19a9 Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 30 Nov 2026 14:56:46 +0900 Subject: [PATCH] =?UTF-8?q?fix(#293):=20param=20default=20=E3=81=AE=20$PSS?= =?UTF-8?q?criptRoot=20=E3=82=92=E4=BD=BF=E3=82=8F=E3=81=AA=E3=81=84=20(Wi?= =?UTF-8?q?ndows=20PowerShell=205.1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Windows PowerShell 5.1 は param block を評価する時点で $PSScriptRoot が空である。 `powershell.exe -File` で起動した script は param default の値が失敗する。 - `platforms/windows-11/install-apps.ps1` の InventoryPath と `platforms/windows-11/setup-tailscale.ps1` の ConfigPath を、param block の 評価後に解決する形にする。明示指定は引き続き優先される。 - 実機では 5.1 で起動した install-apps.ps1 が何も実行せず、verify が Linear / Cursor / Notion を missing として報告していた。 - CI に repository 全体の check を追加: param default で $PSScriptRoot を使う .ps1 があれば失敗する (AST で param block の default を判定する)。 Co-Authored-By: Claude Code --- .github/workflows/ci.yml | 29 ++++++++++++++++++++++++ platforms/windows-11/install-apps.ps1 | 13 ++++++++++- platforms/windows-11/setup-tailscale.ps1 | 11 ++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07241e15..e925ff80 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -523,6 +523,35 @@ jobs: Remove-Item -LiteralPath $probePath -Force -ErrorAction SilentlyContinue } + # Windows PowerShell 5.1 does not populate $PSScriptRoot while the param + # block is evaluated, so a default value that uses it fails whenever the + # script is started with `powershell.exe -File`. The repository's Windows + # scripts are started that way by the converge task. + - name: Validate that no param default uses $PSScriptRoot + shell: pwsh + run: | + Set-StrictMode -Version Latest + $ErrorActionPreference = 'Stop' + + $files = Get-ChildItem -Path . -Recurse -Filter '*.ps1' | + Where-Object { $_.FullName -notmatch '[\\/]\.git[\\/]' } + foreach ($file in $files) { + $errors = $null + $ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$null, [ref]$errors) + if (-not $ast -or -not $ast.ParamBlock) { continue } + foreach ($parameter in $ast.ParamBlock.Parameters) { + if (-not $parameter.DefaultValue) { continue } + $uses = $parameter.DefaultValue.FindAll({ + param($node) + $node -is [System.Management.Automation.Language.VariableExpressionAst] -and + $node.VariablePath.UserPath -eq 'PSScriptRoot' + }, $true) + if ($uses.Count -gt 0) { + throw "$($file.FullName): the param default of `$$($parameter.Name.VariablePath.UserPath) uses `$PSScriptRoot, which Windows PowerShell 5.1 does not populate while the param block is evaluated." + } + } + } + - name: Validate Notion MSIX boundary shell: pwsh run: | diff --git a/platforms/windows-11/install-apps.ps1 b/platforms/windows-11/install-apps.ps1 index 03c44a51..26462bc3 100644 --- a/platforms/windows-11/install-apps.ps1 +++ b/platforms/windows-11/install-apps.ps1 @@ -1,11 +1,22 @@ [CmdletBinding()] param( - [string]$InventoryPath = (Join-Path $PSScriptRoot 'apps.psd1') + [string]$InventoryPath = '' ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +# Windows PowerShell 5.1 does not populate $PSScriptRoot while the param block +# is evaluated, so a default value that uses it fails there with +# "Split-Path: cannot bind argument to parameter 'Path' because it is an empty +# string" when the script is started with `powershell.exe -File`. The inventory +# path is resolved after the param block instead, which works with both +# invocation styles. An explicit -InventoryPath still wins +# (rebuildup/pc-setup#293). +if (-not $InventoryPath) { + $InventoryPath = Join-Path $PSScriptRoot 'apps.psd1' +} + if (-not (Get-Command winget -ErrorAction SilentlyContinue)) { throw 'winget is required to install Windows desktop applications.' } diff --git a/platforms/windows-11/setup-tailscale.ps1 b/platforms/windows-11/setup-tailscale.ps1 index 910ae827..7f49bcc1 100644 --- a/platforms/windows-11/setup-tailscale.ps1 +++ b/platforms/windows-11/setup-tailscale.ps1 @@ -1,12 +1,21 @@ [CmdletBinding()] param( [switch]$Injected, - [string]$ConfigPath = (Join-Path $PSScriptRoot 'tailscale.psd1') + [string]$ConfigPath = '' ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +# Windows PowerShell 5.1 does not populate $PSScriptRoot while the param block +# is evaluated, so a default value that uses it fails there when the script is +# started with `powershell.exe -File`. The config path is resolved after the +# param block instead. An explicit -ConfigPath still wins +# (rebuildup/pc-setup#293). +if (-not $ConfigPath) { + $ConfigPath = Join-Path $PSScriptRoot 'tailscale.psd1' +} + function Resolve-Application { param( [Parameter(Mandatory)][string]$Name,