Skip to content

Commit e5f35a2

Browse files
jjonesczjaredpar
andauthored
Use Roslyn from SDK always for SDK-based project builds (#48028)
Co-authored-by: Jared Parsons <[email protected]>
1 parent 3e55799 commit e5f35a2

File tree

16 files changed

+229
-18
lines changed

16 files changed

+229
-18
lines changed

Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageVersion Include="Basic.CompilerLog.Util" Version="0.9.9" />
910
<PackageVersion Include="AwesomeAssertions" Version="$(AwesomeAssertionsVersion)" />
1011
<PackageVersion Include="AwesomeAssertions.Json" Version="$(AwesomeAssertionsJsonVersion)" />
1112
<PackageVersion Include="MicroBuild.Plugins.SwixBuild.Dotnet" Version="1.1.87-gba258badda" />
@@ -77,6 +78,7 @@
7778
<PackageVersion Include="Microsoft.Web.Xdt" Version="$(MicrosoftWebXdtPackageVersion)" />
7879
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="$(MicrosoftWin32SystemEventsPackageVersion)" />
7980
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="$(MicrosoftWindowsCsWin32PackageVersion)" />
81+
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.2.386" />
8082
<PackageVersion Include="Moq" Version="$(MoqPackageVersion)" />
8183
<PackageVersion Include="NETStandard.Library.NETFramework" Version="$(NETStandardLibraryNETFrameworkVersion)" />
8284
<PackageVersion Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />

documentation/general/decouple-vs-and-net-sdk.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,19 @@ There is nothing preventing NuGet based analyzers from following the same model
117117

118118
Solutions that mix .NET SDK and Visual Studio projects will end up with multiple compiler servers running. This is a result of the .NET SDK projects using the compiler from the .NET SDK and non-SDK projects using the compiler from Visual Studio. There is nothing functionally wrong with this but it's possible customers will notice this and ask questions about it.
119119

120-
The compiler will offer a property that allows SDK projects to use the MSBuild version of the compiler when being built with `msbuild`: `<RoslynUseMSBuildCompiler>true</RoslynUseMSBuildCompiler>`. This can be added to a `Directory.Build.props` file to impact the entire solution. This is not expected to be a common scenario but is available for customers who need it. This property will be ignored when using `dotnet build` as there is no way to fall back to the Visual Studio compiler in that scenario.
120+
The compiler will offer a property that allows SDK projects to use the MSBuild version of the compiler when being built with `msbuild`: `<RoslynCompilerType>Framework</RoslynCompilerType>`. This can be added to a `Directory.Build.props` file to impact the entire solution. This is not expected to be a common scenario but is available for customers who need it. This property will be ignored when using `dotnet build` as there is no way to fall back to the Visual Studio compiler in that scenario.
121+
122+
> [!NOTE]
123+
> These values are recognized for property `RoslynCompilerType`:
124+
> - `Core`: use the compiler that comes with the .NET SDK
125+
> - `Framework`: use the compiler that comes with .NET Framework MSBuild
126+
> - `FrameworkPackage`: download package with .NET Framework compiler corresponding to the .NET SDK version
121127
122128
### .NET Framework based analyzers
123129

124130
There are a few analyzers which are built against .NET Framework TFMs. That means when loaded in a .NET Core compiler it could lead to compatibility issues. This is not expected to be a significant issue as our processes have been pushing customers to target `netstandard` in analyzers for 5+ years. However it is possible that some customers will run into issues here.
125131

126-
For those customers we will recommend that they set `<RoslynUseMSBuildCompiler>true</RoslynUseMSBuildCompiler>` in their build to ensure a .NET Framework based compiler is used. However, it is not our intent to support loading .NET Framework based analyzers in perpetuity. Starting in .NET 12 the compiler will begin issueing warnings is this setup when it detects framework analyzers, and this will become an error in .NET 13. Non-SDK projects will support loading framework based analyzers for the foreseeable future.
132+
For those customers we will recommend that they set `<RoslynCompilerType>Framework</RoslynCompilerType>` in their build to ensure a .NET Framework based compiler is used. However, it is not our intent to support loading .NET Framework based analyzers in perpetuity. Starting in .NET 12 the compiler will begin issueing warnings is this setup when it detects framework analyzers, and this will become an error in .NET 13. Non-SDK projects will support loading framework based analyzers for the foreseeable future.
127133

128134
### Build server shutdown
129135

documentation/general/torn-sdk.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Torn .NET SDK
22

3+
> [!CAUTION]
4+
> This document has been superseded by [Decoupling the .NET SDK and Visual Studio](decouple-vs-and-net-sdk.md).
5+
36
## Terminology
47

58
- **msbuild**: refers the .NET Framework based msbuild included with Visual Studio.

sdk.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1919
build.sh = build.sh
2020
Directory.Build.props = Directory.Build.props
2121
Directory.Build.targets = Directory.Build.targets
22+
Directory.Packages.props = Directory.Packages.props
2223
LICENSE.TXT = LICENSE.TXT
24+
NuGet.config = NuGet.config
2325
README.md = README.md
2426
restore.cmd = restore.cmd
2527
restore.sh = restore.sh

src/Layout/redist/targets/Crossgen.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
<RazorToolFiles Include="$(InstallerOutputDirectory)Sdks\Microsoft.NET.Sdk.Razor\tools\**\*" />
5959

60-
<RemainingFiles Include="$(InstallerOutputDirectory)**\*" Exclude="$(InstallerOutputDirectory)FSharp\FSharp.Build.dll;@(RoslynFiles);@(FSharpFiles)" />
60+
<RemainingFiles Include="$(InstallerOutputDirectory)**\*" Exclude="$(InstallerOutputDirectory)FSharp\FSharp.Build.dll;@(RoslynFiles);@(FSharpFiles);$(InstallerOutputDirectory)Roslyn\binfx\**\*" />
6161

6262
<!-- Don't crossgen VB since the usage doesn't justify the size cost for everyone -->
6363
<RoslynFiles Remove="$(InstallerOutputDirectory)Roslyn\bincore\Microsoft.CodeAnalysis.VisualBasic.dll" />

src/Layout/redist/targets/GenerateLayout.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
<RoslynFrameworkAssembly Include="$(RoslynPackagePath)System.*.dll;
5151
$(RoslynPackagePath)runtimes\**\System.*.dll"
5252
Exclude="$(RoslynPackagePath)runtimes\**\System.IO.Pipes.AccessControl.dll"/>
53+
<!-- binfx folder contains full framework dlls that are needed only on Windows -->
54+
<RoslynFrameworkAssembly Include="$(RoslynPackagePath)binfx\**\*" Condition="'$(OS)' != 'Windows_NT'"/>
5355
<RoslynFile Include="$(RoslynPackagePath)**\*" Exclude="@(RoslynFrameworkAssembly)" />
5456
</ItemGroup>
5557

src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Compilation.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Copyright (c) .NET Foundation. All rights reserved.
129129
ChecksumAlgorithm="$(ChecksumAlgorithm)"
130130
CodeAnalysisRuleSet="$(ResolvedCodeAnalysisRuleSet)"
131131
CodePage="$(CodePage)"
132+
CompilerType="$(RoslynCompilerType)"
132133
DebugType="$(DebugType)"
133134
DefineConstants="$(DefineConstants)"
134135
DelaySign="$(DelaySign)"

src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.Component.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Copyright (c) .NET Foundation. All rights reserved.
195195
ChecksumAlgorithm="$(ChecksumAlgorithm)"
196196
CodeAnalysisRuleSet="$(ResolvedCodeAnalysisRuleSet)"
197197
CodePage="$(CodePage)"
198+
CompilerType="$(RoslynCompilerType)"
198199
DebugType="$(DebugType)"
199200
DefineConstants="$(DefineConstants)"
200201
DelaySign="$(DelaySign)"

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,45 @@ Copyright (c) .NET Foundation. All rights reserved.
299299
<IsNetCoreAppTargetingLatestTFM Condition="'$(IsNetCoreAppTargetingLatestTFM)' == '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(_TargetFrameworkVersionWithoutV)' == '$(NETCoreAppMaximumVersion)'">true</IsNetCoreAppTargetingLatestTFM>
300300
</PropertyGroup>
301301

302+
<!--
303+
Use Roslyn deployed with SDK for builds of SDK-style projects (regardless of whether the initiator is `dotnet` or `msbuild`).
304+
See https://github.com/dotnet/sdk/blob/main/documentation/general/decouple-vs-and-net-sdk.md.
305+
-->
306+
<Choose>
307+
<!-- RoslynCompilerType specified by user, do not overwrite it. -->
308+
<When Condition="'$(RoslynCompilerType)' != ''"></When>
309+
310+
<!-- BuildWithNetFrameworkHostedCompiler specified by user to opt into downloading compiler framework toolset package. -->
311+
<When Condition="'$(BuildWithNetFrameworkHostedCompiler)' == 'true'">
312+
<PropertyGroup><RoslynCompilerType>FrameworkPackage</RoslynCompilerType></PropertyGroup>
313+
</When>
314+
315+
<!-- On core MSBuild or if we have path to dotnet host, use Core compiler type. -->
316+
<When Condition="'$(MSBuildRuntimeType)' == 'Core' or '$(DOTNET_HOST_PATH)' != '' or '$(DOTNET_EXPERIMENTAL_HOST_PATH)' != ''">
317+
<PropertyGroup><RoslynCompilerType>Core</RoslynCompilerType></PropertyGroup>
318+
</When>
319+
320+
<!-- Automatically opt users into using the toolset package if they are running an MSBuild other than what this SDK was built against.
321+
This is to reduce 'tearing'/dependency mismatch, but as always users can override this behavior by disabling the hosted compiler flag. -->
322+
<When Condition="'$(MSBuildRuntimeType)' == 'Full'
323+
and '$(BuildWithNetFrameworkHostedCompiler)' == ''
324+
and '$(_IsDisjointMSBuildVersion)' == 'true'
325+
and ('$(MSBuildProjectExtension)' == '.csproj'
326+
or '$(MSBuildProjectExtension)' == '.vbproj')">
327+
<PropertyGroup><RoslynCompilerType>FrameworkPackage</RoslynCompilerType></PropertyGroup>
328+
</When>
329+
330+
<!-- Otherwise we must be on full MSBuild, so choose Framework compiler type. -->
331+
<Otherwise>
332+
<PropertyGroup><RoslynCompilerType>Framework</RoslynCompilerType></PropertyGroup>
333+
</Otherwise>
334+
</Choose>
335+
336+
<PropertyGroup Condition="'$(RoslynCompilerType)' == 'Core'">
337+
<RoslynTargetsPath Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\..\..\Roslyn</RoslynTargetsPath>
338+
<RoslynTargetsPath Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)..\..\..\Roslyn\binfx</RoslynTargetsPath>
339+
<CSharpCoreTargetsPath>$(MSBuildThisFileDirectory)..\..\..\Roslyn\Microsoft.CSharp.Core.targets</CSharpCoreTargetsPath>
340+
<VisualBasicCoreTargetsPath>$(MSBuildThisFileDirectory)..\..\..\Roslyn\Microsoft.VisualBasic.Core.targets</VisualBasicCoreTargetsPath>
341+
</PropertyGroup>
342+
302343
</Project>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.Common.targets

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,7 @@ Copyright (c) .NET Foundation. All rights reserved.
7575
<UsingTask TaskName="NETSdkInformation" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
7676
<UsingTask TaskName="ShowPreviewMessage" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
7777

78-
<PropertyGroup Condition="'$(MSBuildRuntimeType)' == 'Full'">
79-
<!-- Automatically opt users into using the toolset package if they are running an MSBuild other than what this SDK was built against.
80-
This is to reduce 'tearing'/dependency mismatch, but as always users can override this behavior by disabling the hosted compiler flag.
81-
This is done in Common.targets so that it applies to ApiCompat which uses RoslynTargetsPath in the outer build. -->
82-
<BuildWithNetFrameworkHostedCompiler Condition="'$(BuildWithNetFrameworkHostedCompiler)' == ''
83-
and '$(_IsDisjointMSBuildVersion)' == 'true'
84-
and ('$(Language)' == 'C#'
85-
or '$(Language)' == 'VB')">true</BuildWithNetFrameworkHostedCompiler>
86-
</PropertyGroup>
87-
88-
<PropertyGroup Condition="'$(BuildWithNetFrameworkHostedCompiler)' == 'true' and '$(OS)' == 'Windows_NT' and '$(MSBuildRuntimeType)' == 'Full'">
78+
<PropertyGroup Condition="'$(RoslynCompilerType)' == 'FrameworkPackage' and '$(OS)' == 'Windows_NT' and '$(MSBuildRuntimeType)' == 'Full'">
8979
<RoslynTargetsPath>$(NuGetPackageRoot)\microsoft.net.sdk.compilers.toolset\$(NETCoreSdkVersion)</RoslynTargetsPath>
9080
<_NeedToDownloadMicrosoftNetSdkCompilersToolsetPackage>true</_NeedToDownloadMicrosoftNetSdkCompilersToolsetPackage>
9181
<_MicrosoftNetSdkCompilersToolsetPackageRootEmpty Condition="'$(NuGetPackageRoot)' == ''">true</_MicrosoftNetSdkCompilersToolsetPackageRootEmpty>

0 commit comments

Comments
 (0)