Resolve MSBuild with a single discovery and probing pass

Read installation path and version from one vswhere JSON query. Probe architecture candidates in priority order, retain the first matching path, and stop without repeating filesystem checks.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Chet HuskandCopilot App committed 2026-11-13 13:50:22 -06:00
1 parent 50a58bfea2
commit 0e355a7858
4 files changed
+244 -224

No files matched your search

+86 -108
View File
@@ -11,38 +11,32 @@ const VS_VERSION = core.getInput('vs-version') || 'latest'
const VSWHERE_PATH = core.getInput('vswhere-path')
const ALLOW_PRERELEASE = core.getInput('vs-prerelease') || 'false'
const MSBUILD_ARCH_INPUT = core.getInput('msbuild-architecture')
let MSBUILD_ARCH = MSBUILD_ARCH_INPUT
// The minimum Visual Studio version for the v4 automatic 64-bit default.
export const MIN_VS_VERSION_FOR_X64 = 17
const MIN_VS_VERSION_FOR_X64 = 17
export function resolveMSBuildArchitecture(
export function getMSBuildArchitectures(
explicitArch: string,
hostArch: string,
vsMajorVersion: number | undefined,
arm64Available: boolean,
x64Available: boolean
): string {
vsMajorVersion: number | undefined
): string[] {
if (explicitArch) {
return explicitArch
return [explicitArch]
}
if (vsMajorVersion === undefined || vsMajorVersion < MIN_VS_VERSION_FOR_X64) {
return 'x86'
return ['x86']
}
if (hostArch === 'arm64') {
if (arm64Available) {
return 'arm64'
}
return x64Available ? 'x64' : 'x86'
return ['arm64', 'x64', 'x86']
}
if (hostArch === 'x64' || hostArch === 'amd64') {
return 'x64'
return ['x64']
}
return 'x86'
return ['x86']
}
// Parses the major version number out of a vswhere `installationVersion` string
@@ -60,7 +54,7 @@ export function parseMajorVersion(
// if a specific version of VS is requested
let VSWHERE_EXEC =
'-products * -requires Microsoft.Component.MSBuild -property installationPath -latest '
'-products * -requires Microsoft.Component.MSBuild -format json -utf8 -latest '
if (ALLOW_PRERELEASE === 'true') {
VSWHERE_EXEC += ' -prerelease '
}
@@ -112,111 +106,82 @@ async function run(): Promise<void> {
core.debug(`Full tool exe: ${vswhereToolExe}`)
let vsMajorVersion: number | undefined
if (!MSBUILD_ARCH_INPUT) {
let installationVersion = ''
const versionOptions: ExecOptions = {}
versionOptions.listeners = {
stdout: (data: Buffer) => {
installationVersion += data.toString()
}
}
const versionExec = VSWHERE_EXEC.replace(
'-property installationPath',
'-property installationVersion'
)
await exec.exec(`"${vswhereToolExe}" ${versionExec}`, [], versionOptions)
core.debug(`Found installation version: ${installationVersion.trim()}`)
vsMajorVersion = parseMajorVersion(installationVersion)
}
let installationPath = ''
const output: Buffer[] = []
const options: ExecOptions = {}
options.listeners = {
stdout: (data: Buffer) => {
installationPath += data.toString()
output.push(data)
}
}
await exec.exec(`"${vswhereToolExe}" ${VSWHERE_EXEC}`, [], options)
installationPath = installationPath.trim()
core.debug(`Found installation path: ${installationPath}`)
if (!installationPath) {
const installations: unknown = JSON.parse(
Buffer.concat(output).toString('utf8')
)
if (!Array.isArray(installations)) {
throw new Error('Invalid Visual Studio installation data from vswhere.')
}
if (installations.length === 0) {
core.setFailed('Unable to find MSBuild.')
return
}
if (!MSBUILD_ARCH_INPUT) {
// os.arch() describes Node, which can run under emulation on Windows.
const hostArch = (
process.env['PROCESSOR_ARCHITEW6432'] ||
process.env['RUNNER_ARCH'] ||
os.arch()
).toLowerCase()
const arm64Available =
hostArch === 'arm64' &&
fs.existsSync(
path.join(
installationPath,
'MSBuild\\Current\\Bin\\arm64\\MSBuild.exe'
)
)
const x64Available =
hostArch === 'arm64' &&
!arm64Available &&
fs.existsSync(
path.join(
installationPath,
'MSBuild\\Current\\Bin\\amd64\\MSBuild.exe'
)
)
MSBUILD_ARCH = resolveMSBuildArchitecture(
MSBUILD_ARCH_INPUT,
hostArch,
vsMajorVersion,
arm64Available,
x64Available
)
if (
hostArch === 'arm64' &&
vsMajorVersion !== undefined &&
vsMajorVersion >= MIN_VS_VERSION_FOR_X64 &&
!arm64Available
) {
core.info(`ARM64 MSBuild not found; using ${MSBUILD_ARCH} instead`)
}
core.info(
`Auto-detected msbuild-architecture: ${MSBUILD_ARCH} (host architecture=${hostArch}, vsMajorVersion=${vsMajorVersion})`
)
const installation: unknown = installations[0]
if (
typeof installation !== 'object' ||
installation === null ||
!('installationPath' in installation)
) {
throw new Error('Invalid Visual Studio installation data from vswhere.')
}
const {installationPath, installationVersion} = installation as {
installationPath: unknown
installationVersion?: unknown
}
if (typeof installationPath !== 'string' || !installationPath) {
throw new Error('Invalid Visual Studio installation path from vswhere.')
}
core.debug(`Found installation path: ${installationPath}`)
const vsMajorVersion =
typeof installationVersion === 'string'
? parseMajorVersion(installationVersion)
: undefined
core.debug(`Found installation version: ${installationVersion}`)
// os.arch() describes Node, which can run under emulation on Windows.
const hostArch = (
process.env['PROCESSOR_ARCHITEW6432'] ||
process.env['RUNNER_ARCH'] ||
os.arch()
).toLowerCase()
const architectures = getMSBuildArchitectures(
MSBUILD_ARCH_INPUT,
hostArch,
vsMajorVersion
)
let msbuildArch = ''
let foundToolPath = ''
if (MSBUILD_ARCH === 'x64' || MSBUILD_ARCH === 'arm64') {
const directory = MSBUILD_ARCH === 'x64' ? 'amd64' : 'arm64'
const toolPath = path.join(
installationPath,
`MSBuild\\Current\\Bin\\${directory}\\MSBuild.exe`
)
core.debug(`Checking for path: ${toolPath}`)
if (fs.existsSync(toolPath)) {
foundToolPath = toolPath
}
} else {
let toolPath = path.join(
installationPath,
'MSBuild\\Current\\Bin\\MSBuild.exe'
)
core.debug(`Checking for path: ${toolPath}`)
if (!fs.existsSync(toolPath)) {
toolPath = path.join(
installationPath,
'MSBuild\\15.0\\Bin\\MSBuild.exe'
)
for (const architecture of architectures) {
const relativePaths =
architecture === 'x64' || architecture === 'arm64'
? [
`MSBuild\\Current\\Bin\\${
architecture === 'x64' ? 'amd64' : 'arm64'
}\\MSBuild.exe`
]
: [
'MSBuild\\Current\\Bin\\MSBuild.exe',
'MSBuild\\15.0\\Bin\\MSBuild.exe'
]
for (const relativePath of relativePaths) {
const toolPath = path.join(installationPath, relativePath)
core.debug(`Checking for path: ${toolPath}`)
if (fs.existsSync(toolPath)) {
foundToolPath = toolPath
msbuildArch = architecture
break
}
}
if (fs.existsSync(toolPath)) {
foundToolPath = toolPath
if (foundToolPath) {
break
}
}
@@ -225,8 +190,21 @@ async function run(): Promise<void> {
return
}
if (!MSBUILD_ARCH_INPUT) {
if (
hostArch === 'arm64' &&
architectures[0] === 'arm64' &&
msbuildArch !== 'arm64'
) {
core.info(`ARM64 MSBuild not found; using ${msbuildArch} instead`)
}
core.info(
`Auto-detected msbuild-architecture: ${msbuildArch} (host architecture=${hostArch}, vsMajorVersion=${vsMajorVersion})`
)
}
core.info(`Found MSBuild tool path: ${foundToolPath}`)
core.info(`Using MSBuild architecture: ${MSBUILD_ARCH}`)
core.info(`Using MSBuild architecture: ${msbuildArch}`)
// try to get the MSBuild version for diagnostic purposes; a failure here shouldn't fail the action
try {