|
| 1 | +<# |
| 2 | +BuildBuddy cache keys include the action and test environment, so Bazel should |
| 3 | +not inherit the full hosted-runner PATH on Windows. That PATH includes volatile |
| 4 | +tool entries, such as Maven, that can change independently of this repo and |
| 5 | +cause avoidable cache misses. |
| 6 | +
|
| 7 | +This script derives a smaller, cache-stable PATH that keeps the Windows |
| 8 | +toolchain entries Bazel-backed CI tasks need: MSVC and Windows SDK paths, Git, |
| 9 | +PowerShell, Node, Python, DotSlash, and the standard Windows system |
| 10 | +directories. |
| 11 | +`setup-bazel-ci` runs this after exporting the MSVC environment, and the script |
| 12 | +publishes the result via `GITHUB_ENV` as `CODEX_BAZEL_WINDOWS_PATH` so later |
| 13 | +steps can pass that explicit PATH to Bazel. |
| 14 | +#> |
| 15 | + |
| 16 | +$stablePathEntries = New-Object System.Collections.Generic.List[string] |
| 17 | +$seenEntries = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) |
| 18 | +$windowsAppsPath = if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { |
| 19 | + $null |
| 20 | +} else { |
| 21 | + "$($env:LOCALAPPDATA)\Microsoft\WindowsApps" |
| 22 | +} |
| 23 | +$windowsDir = if ($env:WINDIR) { |
| 24 | + $env:WINDIR |
| 25 | +} elseif ($env:SystemRoot) { |
| 26 | + $env:SystemRoot |
| 27 | +} else { |
| 28 | + $null |
| 29 | +} |
| 30 | + |
| 31 | +function Add-StablePathEntry { |
| 32 | + param([string]$PathEntry) |
| 33 | + |
| 34 | + if ([string]::IsNullOrWhiteSpace($PathEntry)) { |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if ($seenEntries.Add($PathEntry)) { |
| 39 | + [void]$stablePathEntries.Add($PathEntry) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +foreach ($pathEntry in ($env:PATH -split ';')) { |
| 44 | + if ([string]::IsNullOrWhiteSpace($pathEntry)) { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if ( |
| 49 | + $pathEntry -like '*Microsoft Visual Studio*' -or |
| 50 | + $pathEntry -like '*Windows Kits*' -or |
| 51 | + $pathEntry -like '*Microsoft SDKs*' -or |
| 52 | + $pathEntry -like 'C:\Program Files\Git\*' -or |
| 53 | + $pathEntry -like 'C:\Program Files\PowerShell\*' -or |
| 54 | + $pathEntry -like 'C:\hostedtoolcache\windows\node\*' -or |
| 55 | + $pathEntry -like 'C:\hostedtoolcache\windows\Python\*' -or |
| 56 | + $pathEntry -eq 'D:\a\_temp\install-dotslash\bin' -or |
| 57 | + ($windowsDir -and ($pathEntry -eq $windowsDir -or $pathEntry -like "${windowsDir}\*")) |
| 58 | + ) { |
| 59 | + Add-StablePathEntry $pathEntry |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +$gitCommand = Get-Command git -ErrorAction SilentlyContinue |
| 64 | +if ($gitCommand) { |
| 65 | + Add-StablePathEntry (Split-Path $gitCommand.Source -Parent) |
| 66 | +} |
| 67 | + |
| 68 | +$nodeCommand = Get-Command node -ErrorAction SilentlyContinue |
| 69 | +if ($nodeCommand) { |
| 70 | + Add-StablePathEntry (Split-Path $nodeCommand.Source -Parent) |
| 71 | +} |
| 72 | + |
| 73 | +$python3Command = Get-Command python3 -ErrorAction SilentlyContinue |
| 74 | +if ($python3Command) { |
| 75 | + Add-StablePathEntry (Split-Path $python3Command.Source -Parent) |
| 76 | +} |
| 77 | + |
| 78 | +$pythonCommand = Get-Command python -ErrorAction SilentlyContinue |
| 79 | +if ($pythonCommand) { |
| 80 | + Add-StablePathEntry (Split-Path $pythonCommand.Source -Parent) |
| 81 | +} |
| 82 | + |
| 83 | +$pwshCommand = Get-Command pwsh -ErrorAction SilentlyContinue |
| 84 | +if ($pwshCommand) { |
| 85 | + Add-StablePathEntry (Split-Path $pwshCommand.Source -Parent) |
| 86 | +} |
| 87 | + |
| 88 | +if ($windowsAppsPath) { |
| 89 | + Add-StablePathEntry $windowsAppsPath |
| 90 | +} |
| 91 | + |
| 92 | +if ($stablePathEntries.Count -eq 0) { |
| 93 | + throw 'Failed to derive cache-stable Windows PATH.' |
| 94 | +} |
| 95 | + |
| 96 | +if ([string]::IsNullOrWhiteSpace($env:GITHUB_ENV)) { |
| 97 | + throw 'GITHUB_ENV must be set.' |
| 98 | +} |
| 99 | + |
| 100 | +$stablePath = $stablePathEntries -join ';' |
| 101 | +Write-Host 'Derived CODEX_BAZEL_WINDOWS_PATH entries:' |
| 102 | +foreach ($pathEntry in $stablePathEntries) { |
| 103 | + Write-Host " $pathEntry" |
| 104 | +} |
| 105 | +"CODEX_BAZEL_WINDOWS_PATH=$stablePath" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append |
0 commit comments