From 95debd7a7fe9cccd09d04c1915c4542145fe3063 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 21 Sep 2025 19:03:13 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A8Fix=20build=20scripts=20to=20ha?= =?UTF-8?q?ndle=20missing=20Visual=20Studio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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. --- Build/build-functions.psm1 | 38 ++++++++++++++++++++++++++------------ Build/init.ps1 | 23 +++++++++++++++++++---- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Build/build-functions.psm1 b/Build/build-functions.psm1 index bf777a7b73..c3f9a68f14 100644 --- a/Build/build-functions.psm1 +++ b/Build/build-functions.psm1 @@ -8,10 +8,17 @@ $toolsDir = "$root\.tools" $nuget = "$toolsDir\NuGet.exe" $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -$msbuildPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath -if ($msbuildPath) { - $msbuildx64 = join-path $msbuildPath 'MSBuild\Current\Bin\amd64\MSBuild.exe' +# Check if Visual Studio is installed before trying to find MSBuild +if (Test-Path $vswhere) { + $msbuildPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath 2>$null + + if ($msbuildPath) { + $msbuildx64 = join-path $msbuildPath 'MSBuild\Current\Bin\amd64\MSBuild.exe' + } +} else { + $msbuildPath = $null + $msbuildx64 = $null } import-module $PSScriptRoot\build-pack-nano-nugets.psm1 @@ -45,15 +52,22 @@ function Start-Build([boolean] $IncludeNanoFramework = $false) { } else { - write-host -foreground green "Build .NET nanoFramework." - $fileLoggerArg = "/logger:FileLogger,Microsoft.Build;logfile=$logsDir\UnitsNet.NanoFramework.msbuild.log" - - # msbuild does not auto-restore nugets for this project type - & "$nuget" restore "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" - - # now build - & "$msbuildx64" "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" /verbosity:minimal /p:Configuration=Release /p:Platform="Any CPU" /p:ContinuousIntegrationBuild=true $fileLoggerArg - if ($lastexitcode -ne 0) { exit 1 } + # Check if MSBuild is available before attempting NanoFramework build + if (-not $msbuildx64 -or -not (Test-Path $msbuildx64)) { + write-host -foreground yellow "Cannot build .NET nanoFramework - MSBuild not found. Install Visual Studio to build NanoFramework projects." + write-host -foreground yellow "Continuing with main build only..." + } + else { + write-host -foreground green "Build .NET nanoFramework." + $fileLoggerArg = "/logger:FileLogger,Microsoft.Build;logfile=$logsDir\UnitsNet.NanoFramework.msbuild.log" + + # msbuild does not auto-restore nugets for this project type + & "$nuget" restore "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" + + # now build + & "$msbuildx64" "$root\UnitsNet.NanoFramework\GeneratedCode\UnitsNet.nanoFramework.sln" /verbosity:minimal /p:Configuration=Release /p:Platform="Any CPU" /p:ContinuousIntegrationBuild=true $fileLoggerArg + if ($lastexitcode -ne 0) { exit 1 } + } } write-host -foreground blue "Start-Build...END`n" diff --git a/Build/init.ps1 b/Build/init.ps1 index bed427b0dd..77d2a0dbc1 100644 --- a/Build/init.ps1 +++ b/Build/init.ps1 @@ -27,8 +27,23 @@ if (-not (Test-Path "$nugetPath")) { ################################################### ## TODO: OK to remove after moving to AZDO pipeline $VsWherePath = "${env:PROGRAMFILES(X86)}\Microsoft Visual Studio\Installer\vswhere.exe" -$VsPath = $(&$VsWherePath -latest -property installationPath) -$msbuildPath = Join-Path -Path $VsPath -ChildPath "\MSBuild" + +# Check if Visual Studio is installed +if (Test-Path $VsWherePath) { + $VsPath = $(&$VsWherePath -latest -property installationPath 2>$null) + if ($VsPath) { + $msbuildPath = Join-Path -Path $VsPath -ChildPath "\MSBuild" + Write-Host -Foreground Green "Visual Studio found at: $VsPath" + } else { + Write-Host -Foreground Yellow "Visual Studio not found via vswhere" + $VsPath = $null + $msbuildPath = $null + } +} else { + Write-Host -Foreground Yellow "Visual Studio not installed - NanoFramework build will be skipped" + $VsPath = $null + $msbuildPath = $null +} # Install dotnet CLI tools declared in /.config/dotnet-tools.json pushd $root @@ -36,7 +51,7 @@ dotnet tool restore popd # Install .NET nanoFramework build components -if (!(Test-Path "$msbuildPath/nanoFramework")) { +if ($msbuildPath -and !(Test-Path "$msbuildPath/nanoFramework")) { Write-Host "Installing .NET nanoFramework VS extension..." [System.Net.WebClient]$webClient = New-Object System.Net.WebClient @@ -60,7 +75,7 @@ if (!(Test-Path "$msbuildPath/nanoFramework")) { Write-Output "VsWherePath is: $VsWherePath" - $VsInstance = $(&$VSWherePath -latest -property displayName) + $VsInstance = $(&$VSWherePath -latest -property displayName 2>$null) Write-Output "Latest VS is: $VsInstance" From 3289cea782db2583644e5dd485850b9bbc885f9f Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 21 Sep 2025 19:21:40 +0200 Subject: [PATCH 2/2] minor fix --- Build/init.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Build/init.ps1 b/Build/init.ps1 index 77d2a0dbc1..bb3478bf63 100644 --- a/Build/init.ps1 +++ b/Build/init.ps1 @@ -35,12 +35,12 @@ if (Test-Path $VsWherePath) { $msbuildPath = Join-Path -Path $VsPath -ChildPath "\MSBuild" Write-Host -Foreground Green "Visual Studio found at: $VsPath" } else { - Write-Host -Foreground Yellow "Visual Studio not found via vswhere" + Write-Host -Foreground Yellow "Visual Studio not found via vswhere, NanoFramework builds will be skipped" $VsPath = $null $msbuildPath = $null } } else { - Write-Host -Foreground Yellow "Visual Studio not installed - NanoFramework build will be skipped" + Write-Host -Foreground Yellow "Visual Studio not installed - NanoFramework builds will be skipped" $VsPath = $null $msbuildPath = $null }