Skip to content

Commit 6f8f2d1

Browse files
authored
[release/9.0.1xx] Fix vs2022 Image rejecting preview sdks, failing to resolve sdks (#51621)
2 parents 0686242 + dc66804 commit 6f8f2d1

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

build/RunTestsOnHelix.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ set PATH=%DOTNET_ROOT%;%PATH%
1010
set DOTNET_MULTILEVEL_LOOKUP=0
1111
set TestFullMSBuild=%1
1212

13+
REM Ensure Visual Studio instances allow preview SDKs
14+
PowerShell -ExecutionPolicy ByPass -NoProfile -File "%HELIX_CORRELATION_PAYLOAD%\t\eng\enable-preview-sdks.ps1"
15+
1316
REM Use powershell to call partical Arcade logic to get full framework msbuild path and assign it
1417
if "%TestFullMSBuild%"=="true" (
1518
FOR /F "tokens=*" %%g IN ('PowerShell -ExecutionPolicy ByPass -File "%HELIX_CORRELATION_PAYLOAD%\t\eng\print-full-msbuild-path.ps1"') do (SET DOTNET_SDK_TEST_MSBUILD_PATH=%%g)

eng/common/tools.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function InstallDotNet([string] $dotnetRoot,
295295
if ($runtime -eq "aspnetcore") { $runtimePath = $runtimePath + "\Microsoft.AspNetCore.App" }
296296
if ($runtime -eq "windowsdesktop") { $runtimePath = $runtimePath + "\Microsoft.WindowsDesktop.App" }
297297
$runtimePath = $runtimePath + "\" + $version
298-
298+
299299
$dotnetVersionLabel = "runtime toolset '$runtime/$architecture v$version'"
300300

301301
if (Test-Path $runtimePath) {
@@ -547,19 +547,25 @@ function LocateVisualStudio([object]$vsRequirements = $null){
547547
})
548548
}
549549

550-
if (!$vsRequirements) { $vsRequirements = $GlobalJson.tools.vs }
550+
if (!$vsRequirements) {
551+
if (Get-Member -InputObject $GlobalJson.tools -Name 'vs' -ErrorAction SilentlyContinue) {
552+
$vsRequirements = $GlobalJson.tools.vs
553+
} else {
554+
$vsRequirements = $null
555+
}
556+
}
551557
$args = @('-latest', '-format', 'json', '-requires', 'Microsoft.Component.MSBuild', '-products', '*')
552558

553559
if (!$excludePrereleaseVS) {
554560
$args += '-prerelease'
555561
}
556562

557-
if (Get-Member -InputObject $vsRequirements -Name 'version') {
563+
if ($vsRequirements -and (Get-Member -InputObject $vsRequirements -Name 'version' -ErrorAction SilentlyContinue)) {
558564
$args += '-version'
559565
$args += $vsRequirements.version
560566
}
561567

562-
if (Get-Member -InputObject $vsRequirements -Name 'components') {
568+
if ($vsRequirements -and (Get-Member -InputObject $vsRequirements -Name 'components' -ErrorAction SilentlyContinue)) {
563569
foreach ($component in $vsRequirements.components) {
564570
$args += '-requires'
565571
$args += $component
@@ -610,7 +616,7 @@ function InitializeBuildTool() {
610616
} else {
611617
$initializeBuildToolFramework=$env:_OverrideArcadeInitializeBuildToolFramework
612618
}
613-
619+
614620
$buildTool = @{ Path = $dotnetPath; Command = 'msbuild'; Tool = 'dotnet'; Framework = $initializeBuildToolFramework }
615621
} elseif ($msbuildEngine -eq "vs") {
616622
try {

eng/enable-preview-sdks.ps1

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
param()
2+
3+
. $PSScriptRoot\common\tools.ps1
4+
5+
try {
6+
$vsInfo = LocateVisualStudio
7+
}
8+
catch {
9+
Write-Host "LocateVisualStudio failed: $_"
10+
return
11+
}
12+
13+
if ($null -eq $vsInfo) {
14+
Write-Host "No Visual Studio instance detected; preview SDKs remain enabled by default."
15+
return
16+
}
17+
18+
$vsId = $vsInfo.instanceId
19+
$vsMajorVersion = $vsInfo.installationVersion.Split('.')[0]
20+
$instanceDir = Join-Path $env:USERPROFILE "AppData\Local\Microsoft\VisualStudio\$vsMajorVersion.0_$vsId"
21+
22+
Create-Directory $instanceDir
23+
24+
$sdkFile = Join-Path $instanceDir 'sdk.txt'
25+
26+
$desiredLine = 'UsePreviews=True'
27+
$existingLines = @()
28+
29+
if (Test-Path $sdkFile) {
30+
$existingLines = @(Get-Content -Path $sdkFile -Encoding ASCII)
31+
}
32+
33+
# Determine how to place the UsePreviews flag based on existing content.
34+
$replacementIndex = -1
35+
for ($i = 0; $i -lt $existingLines.Count; $i++) {
36+
if ($existingLines[$i] -match '^UsePreviews=.*$') {
37+
$replacementIndex = $i
38+
break
39+
}
40+
}
41+
42+
# Replace the existing line to enforce it as True
43+
if ($replacementIndex -ge 0) {
44+
$updatedLines = $existingLines
45+
$updatedLines[$replacementIndex] = $desiredLine
46+
}
47+
elseif ($existingLines.Count -gt 0) {
48+
# Write to the top of the file but keep the remaining portion (assumption: order does not matter to VS)
49+
$updatedLines = @($desiredLine) + $existingLines
50+
}
51+
else {
52+
# Write a whole new file
53+
$updatedLines = @($desiredLine)
54+
}
55+
56+
Set-Content -Path $sdkFile -Value $updatedLines -Encoding ASCII
57+
58+
Write-Host "Updated $sdkFile"
59+
Get-Content -Path $sdkFile | ForEach-Object { Write-Host " $_" }

0 commit comments

Comments
 (0)