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

+64 -67
View File
@@ -34,28 +34,24 @@ 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.
exports.MIN_VS_VERSION_FOR_X64 = 17;
function resolveMSBuildArchitecture(explicitArch, hostArch, vsMajorVersion, arm64Available, x64Available) {
const MIN_VS_VERSION_FOR_X64 = 17;
function getMSBuildArchitectures(explicitArch, hostArch, vsMajorVersion) {
if (explicitArch) {
return explicitArch;
return [explicitArch];
}
if (vsMajorVersion === undefined || vsMajorVersion < exports.MIN_VS_VERSION_FOR_X64) {
return 'x86';
if (vsMajorVersion === undefined || vsMajorVersion < MIN_VS_VERSION_FOR_X64) {
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'];
}
exports.resolveMSBuildArchitecture = resolveMSBuildArchitecture;
exports.getMSBuildArchitectures = getMSBuildArchitectures;
// Parses the major version number out of a vswhere `installationVersion` string
// such as "17.9.34728.123". Returns undefined if it cannot be parsed.
function parseMajorVersion(installationVersion) {
@@ -68,7 +64,7 @@ function parseMajorVersion(installationVersion) {
}
exports.parseMajorVersion = parseMajorVersion;
// if a specific version of VS is requested
let VSWHERE_EXEC = '-products * -requires Microsoft.Component.MSBuild -property installationPath -latest ';
let VSWHERE_EXEC = '-products * -requires Microsoft.Component.MSBuild -format json -utf8 -latest ';
if (ALLOW_PRERELEASE === 'true') {
VSWHERE_EXEC += ' -prerelease ';
}
@@ -109,79 +105,80 @@ function run() {
return;
}
core.debug(`Full tool exe: ${vswhereToolExe}`);
let vsMajorVersion;
if (!MSBUILD_ARCH_INPUT) {
let installationVersion = '';
const versionOptions = {};
versionOptions.listeners = {
stdout: (data) => {
installationVersion += data.toString();
}
};
const versionExec = VSWHERE_EXEC.replace('-property installationPath', '-property installationVersion');
yield exec.exec(`"${vswhereToolExe}" ${versionExec}`, [], versionOptions);
core.debug(`Found installation version: ${installationVersion.trim()}`);
vsMajorVersion = parseMajorVersion(installationVersion);
}
let installationPath = '';
const output = [];
const options = {};
options.listeners = {
stdout: (data) => {
installationPath += data.toString();
output.push(data);
}
};
yield exec.exec(`"${vswhereToolExe}" ${VSWHERE_EXEC}`, [], options);
installationPath = installationPath.trim();
core.debug(`Found installation path: ${installationPath}`);
if (!installationPath) {
const installations = 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 >= exports.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 = 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;
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;
}
}
if (!foundToolPath) {
core.setFailed('Unable to find MSBuild.');
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 {
let msbuildVersion = '';