Skip to content

Commit

Permalink
Use msbuild from VS 2022 if available (#395)
Browse files Browse the repository at this point in the history
* Use `msbuild` from VS 2022 if available
  - should ease local and TeamCity builds
  - make `%InstallDir%` unquoted (unlike `%vswhere%`)
    - shorten `%Path%` slightly
  - nits:
    - use script location more; no need to change directories

* React to new build issues
  - follow up to #396
    (unsure why new FxCop and "spelling" errors didn't fail that PR)

* Put `PortReserver` users into an xUnit collection
  • Loading branch information
dougbu authored Mar 7, 2023
1 parent 6451d77 commit 2da072a
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 17 deletions.
35 changes: 25 additions & 10 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@echo off
pushd %~dp0
setlocal

if exist bin goto Build
Expand Down Expand Up @@ -29,7 +28,7 @@ for /f "usebackq tokens=*" %%i in (`%vswhere% -version 16 -latest -prerelease -p
-requires Microsoft.Net.Component.4.5.2.TargetingPack ^
-requires Microsoft.Net.Component.4.6.2.TargetingPack ^
-property installationPath`) do (
set InstallDir="%%i"
set "InstallDir=%%i"
)

if not DEFINED InstallDir (
Expand All @@ -38,49 +37,65 @@ if not DEFINED InstallDir (
goto BuildFail
)

REM Find or install MSBuild. Need v17.4 due to our .NET SDK choice.
REM Find a 64bit MSBuild and add it to path. Require v17.4 or later due to our .NET SDK choice.
REM Check for VS2022 first.
set InstallDir=
for /f "usebackq tokens=*" %%i in (`%vswhere% -version 17.4 -latest -prerelease -products * ^
-requires Microsoft.Component.MSBuild ^
-property installationPath`) do (
set "InstallDir=%%i"
)

if DEFINED InstallDir (
REM Add MSBuild to the path.
set "PATH=%InstallDir%\MSBuild\Current\Bin;%PATH%"
goto FoundMSBuild
)

REM Otherwise find or install an xcopy-able MSBuild.
echo "Could not find a VS2022 installation with the necessary components (MSBuild). Falling back..."

set "MSBuildVersion=17.4.1"
set "Command=[System.Threading.Thread]::CurrentThread.CurrentCulture = ''"
set "Command=%Command%; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''"
set "Command=%Command%; try { & '%~dp0eng\GetXCopyMSBuild.ps1' %MSBuildVersion%; exit $LASTEXITCODE }"
set "Command=%Command% catch { write-host $_; exit 1 }"
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
if %ERRORLEVEL% neq 0 goto BuildFail

REM Add MSBuild to the path.
set "PATH=%CD%\.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin\;%PATH%"
set "PATH=%~dp0.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin;%PATH%"

:FoundMSBuild
REM Configure NuGet operations to work w/in this repo i.e. do not pollute system packages folder.
REM Note this causes two copies of packages restored using packages.config to land in this folder e.g.
REM StyleCpy.5.0.0/ and stylecop/5.0.0/.
set "NUGET_PACKAGES=%CD%\packages"
set "NUGET_PACKAGES=%~dp0packages"

REM Are we running in a local dev environment (not on CI)?
if DEFINED CI (set Desktop=false) else if DEFINED TEAMCITY_VERSION (set Desktop=false) else (set Desktop=true)

if "%1" == "" goto BuildDefaults

MSBuild Runtime.msbuild /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
MSBuild "%~dp0Runtime.msbuild" /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
/fl /fileLoggerParameters:LogFile=bin\msbuild.log;Verbosity=Normal /consoleLoggerParameters:Summary /t:%*
if %ERRORLEVEL% neq 0 goto BuildFail
goto BuildSuccess

:BuildDefaults
MSBuild Runtime.msbuild /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
MSBuild "%~dp0Runtime.msbuild" /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
/fl /fileLoggerParameters:LogFile=bin\msbuild.log;Verbosity=Normal /consoleLoggerParameters:Summary
if %ERRORLEVEL% neq 0 goto BuildFail
goto BuildSuccess

:BuildFail
echo.
echo *** BUILD FAILED ***
popd
endlocal
exit /B 999

:BuildSuccess
echo.
echo **** BUILD SUCCESSFUL ***
popd
endlocal
exit /B 0
3 changes: 2 additions & 1 deletion eng/GetXCopyMSBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ function InitializeXCopyMSBuild([string]$packageVersion, [bool]$install, [string
return Join-Path $packageDir 'tools'
}

InitializeXCopyMSBuild -packageVersion $Version -install $true -ToolsDir (join-path $PWD .msbuild)
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot '..\')
InitializeXCopyMSBuild -packageVersion $Version -install $true -ToolsDir (join-path $RepoRoot .msbuild)
2 changes: 2 additions & 0 deletions src/System.Net.Http.Formatting/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
[assembly: SuppressMessage("Microsoft.Design", "CA2210:AssembliesShouldHaveValidStrongNames")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Net.Http.Headers", Justification = "We follow the layout of System.Net.Http.")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Net.Http.Handlers", Justification = "Handlers provide an extensibility hook which we want to keep in a separate namespace.")]
// Some resources are specific to the netstandard1.3 assembly
[assembly: SuppressMessage("Microsoft.Web.FxCop", "MW1000:UnusedResourceUsageRule", Justification = "There are a few unused resources due to missing netstandard1.3 features.")]
4 changes: 2 additions & 2 deletions src/System.Net.Http.Formatting/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@
<value>Cannot access a closed stream.</value>
</data>
<data name="JsonMediaTypeFormatter_DCS_NotSupported" xml:space="preserve">
<value>Unable to validate types on this platform when {0} is 'true'. Please reset {0} or move to a supported platform, one where the 'netstandard2.0' assembly is usable.</value>
<value>Unable to validate types on this platform when {0} is 'true'. Please reset {0} or move to a supported platform, one where the .NET Standard 2.0 assembly is usable.</value>
</data>
<data name="XmlMediaTypeFormatter_DCS_NotSupported" xml:space="preserve">
<value>Unable to validate types on this platform when {0} is 'false'. Please set {0} or move to a supported platform, one where the 'netstandard2.0' assembly is usable.</value>
<value>Unable to validate types on this platform when {0} is 'false'. Please set {0} or move to a supported platform, one where the .NET Standard 2.0 assembly is usable.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions test/Microsoft.TestCommon/PortReserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

namespace Microsoft.TestCommon
{
[Xunit.CollectionDefinition("PortReserver Collection", DisableParallelization = true)]
public class PortReserverCollection
{
}

/// <summary>
/// This class allocates ports while ensuring that:
/// 1. Ports that are permanently taken (or taken for the duration of the test) are not being attempted to be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ await Assert.ThrowsAsync<PlatformNotSupportedException>(() =>
formatter.ReadFromStreamAsync(variationType, stream, content, formatterLogger: null),
"Unable to validate types on this platform when UseDataContractJsonSerializer is 'true'. " +
"Please reset UseDataContractJsonSerializer or move to a supported platform, one where the " +
"'netstandard2.0' assembly is usable.");
".NET Standard 2.0 assembly is usable.");
}
}

Expand All @@ -211,7 +211,7 @@ await Assert.ThrowsAsync<PlatformNotSupportedException>(() =>
formatter.WriteToStreamAsync(variationType, testData, stream, content, transportContext: null),
"Unable to validate types on this platform when UseDataContractJsonSerializer is 'true'. " +
"Please reset UseDataContractJsonSerializer or move to a supported platform, one where the " +
"'netstandard2.0' assembly is usable.");
".NET Standard 2.0 assembly is usable.");
}

#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public async Task ReadFromStreamAsync_UsingDataContractSerializer_Throws(Type va
await Assert.ThrowsAsync<PlatformNotSupportedException>(() =>
formatter.ReadFromStreamAsync(variationType, stream, content, formatterLogger: null),
"Unable to validate types on this platform when UseXmlSerializer is 'false'. Please set " +
"UseXmlSerializer or move to a supported platform, one where the 'netstandard2.0' assembly " +
"UseXmlSerializer or move to a supported platform, one where the .NET Standard 2.0 assembly " +
"is usable.");
}
}
Expand All @@ -534,7 +534,7 @@ public async Task WriteToStreamAsync_UsingDataContractSerializer_Throws(Type var
await Assert.ThrowsAsync<PlatformNotSupportedException>(() =>
formatter.WriteToStreamAsync(variationType, testData, stream, content, transportContext: null),
"Unable to validate types on this platform when UseXmlSerializer is 'false'. Please set " +
"UseXmlSerializer or move to a supported platform, one where the 'netstandard2.0' assembly " +
"UseXmlSerializer or move to a supported platform, one where the .NET Standard 2.0 assembly " +
"is usable.");
}

Expand Down
1 change: 1 addition & 0 deletions test/System.Web.Http.Owin.Test/OwinHostIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace System.Web.Http.Owin
{
[Xunit.Collection("PortReserver Collection")] // Avoid conflicts between different PortReserver consumers.
public class OwinHostIntegrationTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace System.Web.Http
{
[Xunit.Collection("PortReserver Collection")] // Avoid conflicts between different PortReserver consumers.
public class BasicOverHttpTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace System.Web.Http.SelfHost
{
[Xunit.Collection("PortReserver Collection")] // Avoid conflicts between different PortReserver consumers.
public class HttpSelfHostResponseTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace System.Web.Http.SelfHost
{
[Xunit.Collection("PortReserver Collection")] // Avoid conflicts between different PortReserver consumers.
public class HttpSelfHostServerTest : IDisposable
{
private HttpSelfHostServer server = null;
Expand Down

0 comments on commit 2da072a

Please sign in to comment.