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,