Skip to content

Commit

Permalink
Added MinimumVisualStudioVersion env variable to force `run-windows…
Browse files Browse the repository at this point in the history
…` to use a particular version of VS installed (#13373)

## Description

Fixes a bug in `run-windows` where we truncate the minimum VS version number so it only allows for one digit minor versions.

We try to detect when building inside a VS command prompt by looking for the `VisualStudioVersion` environment variable. In this way we can make sure to use specific (usually prerelease/preview) versions of VS.

However, we can also set this env variable manually, which I was trying to do. However, there are lots of other places in VS that read this value, and it always expects a major version with a 0 minor version (i.e. `17.0`, `18.0`). When setting `17.11`, (the version I'm trying to test for has fixes to #13339) the code was truncating it to `17.1`. Even fixing the truncation meant that the build would later fail anyway, since it wasn't a  `X.0` number.

So this PR not only fixes the truncating code to keep `17.11` as `17.11`, but also adds a *new* env variable to check for, i.e. `MinimumVisualStudioVersion`, so that we can safely override that without breaking the build.

### Type of Change
- Bug fix (non-breaking change which fixes an issue)

### Why
Trying to test with VS 17.11 preview, the number kept getting truncated to 17.1. I need a way of specify the version I want without breaking the build

### What
Fixes the version range calculating code to not truncate the digits, adds new `MinimumVisualStudioVersion` env variable.

## Screenshots
N/A

## Testing
Verified run-windows could fine the preview 17.11 over the regular 17.10 installed.

## Changelog
Should this change be included in the release notes: yes

Added `MinimumVisualStudioVersion` env variable to force `run-windows` to use a particular version of VS installed
  • Loading branch information
jonthysell authored Jun 21, 2024
1 parent 8892c66 commit 68144e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix bug with specifying min VS versions for run-windows",
"packageName": "@react-native-windows/cli",
"email": "[email protected]",
"dependentChangeType": "patch"
}
10 changes: 8 additions & 2 deletions packages/@react-native-windows/cli/src/utils/msbuildtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class MSBuildTools {
'Microsoft.Component.MSBuild',
getVCToolsByArch(buildArch),
];
const minVersion = process.env.VisualStudioVersion || '17.0';
const minVersion = process.env.MinimumVisualStudioVersion || process.env.VisualStudioVersion || '17.0';
const vsInstallation = findLatestVsInstall({
requires,
minVersion,
Expand All @@ -209,7 +209,13 @@ export default class MSBuildTools {
});

if (!vsInstallation) {
if (process.env.VisualStudioVersion != null) {
if (process.env.MinimumVisualStudioVersion != null) {
throw new CodedError(
'NoMSBuild',
`MSBuild tools not found for version ${process.env.MinimumVisualStudioVersion} (from environment). Make sure all required components have been installed`,
{MinimumVisualStudioVersionFromEnv: process.env.MinimumVisualStudioVersion},
);
} else if (process.env.VisualStudioVersion != null) {
throw new CodedError(
'NoMSBuild',
`MSBuild tools not found for version ${process.env.VisualStudioVersion} (from environment). Make sure all required components have been installed`,
Expand Down
6 changes: 3 additions & 3 deletions packages/@react-native-windows/cli/src/utils/vsInstalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export function enumerateVsInstalls(opts: {

if (minVersionSemVer) {
minVersion = minVersionSemVer.toString();
maxVersion = (minVersionSemVer.major + 1).toFixed(1);
maxVersion = `${(minVersionSemVer.major + 1)}.0`;
} else if (!Number.isNaN(minVersionNum)) {
minVersion = minVersionNum.toFixed(1);
maxVersion = (Math.floor(minVersionNum) + 1).toFixed(1);
minVersion = Number.isInteger(minVersionNum) ? `${minVersionNum}.0` : minVersionNum.toString();
maxVersion = `${(Math.floor(minVersionNum) + 1)}.0`;
} else {
// Unable to parse minVersion and determine maxVersion,
// caller will throw error that version couldn't be found.
Expand Down

0 comments on commit 68144e7

Please sign in to comment.