From 6083495611a0ec99ee31cb97317f96901f2fc1d1 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Mon, 9 Nov 2026 10:05:22 +0300
Subject: [PATCH v16 1/3] ci: Locate Visual Studio dynamically on Windows

The Visual Studio version and edition vary between runner images, so
hardcoding the path prevents using runners with a different
installation.

Use vswhere to locate the latest installation with the required C++
tools and save its vcvarsall.bat path in GITHUB_ENV.

Suggested-by: Andres Freund <andres@anarazel.de>
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CAN55FZ1BcWBsWgCCk2GQkYN6tzDxCw2hVK2m3qeiwaNXUnjLVg%40mail.gmail.com
---
 .github/workflows/pg-ci.yml | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/pg-ci.yml b/.github/workflows/pg-ci.yml
index 9e55f0fbc45..baf9ba3ca22 100644
--- a/.github/workflows/pg-ci.yml
+++ b/.github/workflows/pg-ci.yml
@@ -1036,9 +1036,18 @@ jobs:
         shell: pwsh
         run: src/tools/ci/gha_setup_windows_debugger.ps1
 
+      # The Visual Studio version and edition depend on the runner image.
+      - name: Locate Visual Studio
+        shell: pwsh
+        run: |
+          $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
+          $vs = & $vswhere -latest -products '*' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
+          if (!$? -or !$vs) { throw 'Could not locate Visual Studio C++ tools' }
+          Add-Content $env:GITHUB_ENV "VCVARSALL=$vs\VC\Auxiliary\Build\vcvarsall.bat"
+
       - name: Configure
         run: |
-          call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
+          call "%VCVARSALL%" x64
           meson setup ^
             --backend ninja ^
             ${{env.MESON_COMMON_PG_CONFIG_ARGS}} ^
@@ -1050,7 +1059,7 @@ jobs:
 
       - name: Build
         run: |
-          call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
+          call "%VCVARSALL%" x64
           ninja -C build ${{env.MBUILD_TARGET}} || exit 1
           ninja -C build -t missingdeps
 
@@ -1062,7 +1071,7 @@ jobs:
           # doesn't seem worth it.
           MTEST_TARGET: --slice ${{ matrix.slice}}/${{ matrix.num_slices}} ${{env.MTEST_TARGET}}
           ADDITIONAL_SETUP: |
-            call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
+            call "%VCVARSALL%" x64
         run: *meson_test_world_cmd
 
       - *upload_logs_step
-- 
2.47.3

