Skip to content

Commit ff9acd7

Browse files
authored
🔨Fix build scripts to handle missing Visual Studio (#1626)
- Fix init.ps1 to check if Visual Studio exists before using vswhere - Add proper null checks for MSBuild path - Make NanoFramework build optional when MSBuild is not available - Show clear warning messages when Visual Studio is not installed This allows the main build to work without Visual Studio installed, while still supporting NanoFramework builds when VS is available.
1 parent dd045a8 commit ff9acd7

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

Build/build-functions.psm1

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ $toolsDir = "$root\.tools"
88

99
$nuget = "$toolsDir\NuGet.exe"
1010
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
11-
$msbuildPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
1211

13-
if ($msbuildPath) {
14-
$msbuildx64 = join-path $msbuildPath 'MSBuild\Current\Bin\amd64\MSBuild.exe'
12+
# Check if Visual Studio is installed before trying to find MSBuild
13+
if (Test-Path $vswhere) {
14+
$msbuildPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath 2>$null
15+
16+
if ($msbuildPath) {
17+
$msbuildx64 = join-path $msbuildPath 'MSBuild\Current\Bin\amd64\MSBuild.exe'
18+
}
19+
} else {
20+
$msbuildPath = $null
21+
$msbuildx64 = $null
1522
}
1623

1724
import-module $PSScriptRoot\build-pack-nano-nugets.psm1
@@ -45,15 +52,22 @@ function Start-Build([boolean] $IncludeNanoFramework = $false) {
4552
}
4653
else
4754
{
48-
write-host -foreground green "Build .NET nanoFramework."
49-
$fileLoggerArg = "/logger:FileLogger,Microsoft.Build;logfile=$logsDir\UnitsNet.NanoFramework.msbuild.log"
50-
51-
# msbuild does not auto-restore nugets for this project type
52-
& "$nuget" restore "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln"
53-
54-
# now build
55-
& "$msbuildx64" "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" /verbosity:minimal /p:Configuration=Release /p:Platform="Any CPU" /p:ContinuousIntegrationBuild=true $fileLoggerArg
56-
if ($lastexitcode -ne 0) { exit 1 }
55+
# Check if MSBuild is available before attempting NanoFramework build
56+
if (-not $msbuildx64 -or -not (Test-Path $msbuildx64)) {
57+
write-host -foreground yellow "Cannot build .NET nanoFramework - MSBuild not found. Install Visual Studio to build NanoFramework projects."
58+
write-host -foreground yellow "Continuing with main build only..."
59+
}
60+
else {
61+
write-host -foreground green "Build .NET nanoFramework."
62+
$fileLoggerArg = "/logger:FileLogger,Microsoft.Build;logfile=$logsDir\UnitsNet.NanoFramework.msbuild.log"
63+
64+
# msbuild does not auto-restore nugets for this project type
65+
& "$nuget" restore "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln"
66+
67+
# now build
68+
& "$msbuildx64" "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" /verbosity:minimal /p:Configuration=Release /p:Platform="Any CPU" /p:ContinuousIntegrationBuild=true $fileLoggerArg
69+
if ($lastexitcode -ne 0) { exit 1 }
70+
}
5771
}
5872

5973
write-host -foreground blue "Start-Build...END`n"

Build/init.ps1

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@ if (-not (Test-Path "$nugetPath")) {
2727
###################################################
2828
## TODO: OK to remove after moving to AZDO pipeline
2929
$VsWherePath = "${env:PROGRAMFILES(X86)}\Microsoft Visual Studio\Installer\vswhere.exe"
30-
$VsPath = $(&$VsWherePath -latest -property installationPath)
31-
$msbuildPath = Join-Path -Path $VsPath -ChildPath "\MSBuild"
30+
31+
# Check if Visual Studio is installed
32+
if (Test-Path $VsWherePath) {
33+
$VsPath = $(&$VsWherePath -latest -property installationPath 2>$null)
34+
if ($VsPath) {
35+
$msbuildPath = Join-Path -Path $VsPath -ChildPath "\MSBuild"
36+
Write-Host -Foreground Green "Visual Studio found at: $VsPath"
37+
} else {
38+
Write-Host -Foreground Yellow "Visual Studio not found via vswhere, NanoFramework builds will be skipped"
39+
$VsPath = $null
40+
$msbuildPath = $null
41+
}
42+
} else {
43+
Write-Host -Foreground Yellow "Visual Studio not installed - NanoFramework builds will be skipped"
44+
$VsPath = $null
45+
$msbuildPath = $null
46+
}
3247

3348
# Install dotnet CLI tools declared in /.config/dotnet-tools.json
3449
pushd $root
3550
dotnet tool restore
3651
popd
3752

3853
# Install .NET nanoFramework build components
39-
if (!(Test-Path "$msbuildPath/nanoFramework")) {
54+
if ($msbuildPath -and !(Test-Path "$msbuildPath/nanoFramework")) {
4055
Write-Host "Installing .NET nanoFramework VS extension..."
4156

4257
[System.Net.WebClient]$webClient = New-Object System.Net.WebClient
@@ -60,7 +75,7 @@ if (!(Test-Path "$msbuildPath/nanoFramework")) {
6075

6176
Write-Output "VsWherePath is: $VsWherePath"
6277

63-
$VsInstance = $(&$VSWherePath -latest -property displayName)
78+
$VsInstance = $(&$VSWherePath -latest -property displayName 2>$null)
6479

6580
Write-Output "Latest VS is: $VsInstance"
6681

0 commit comments

Comments
 (0)