Skip to content

Commit 0bc107d

Browse files
Make private runtime-only dependencies on .NET Standard 2.0. (#11109)
### Context Since #6148 we produce only reference assemblies for .NET Standard 2.0, which means that several of the our NuGet packages' dependencies when targeting .NET Standard 2.0 are unused. ### Changes Made The project files for `Microsoft.Build.Framework`, `Utilities.Core` and `Tasks` were updated to apply `PrivateAssets="all"` to all package references that are not exposed in the package's public API, when targeting .NET Standard 2.0. ### Testing I ran `dotnet pack` on these projects and validated manually that on .NET Standard 2.0, `Microsoft.Build.Framework` has zero dependencies, `Utilities.Core` depends only on `Framework`, and `Tasks` depends only on the previous two. Because Roslyn [keeps some internal APIs in reference assemblies](https://github.com/dotnet/roslyn/blob/main/docs/features/refout.md#definition-of-ref-assemblies), these reference assemblies still reference some assemblies whose respective package is not depended upon. I manually validated with ILSpy that the types in these assemblies are used only by internal APIs. ### Notes This is going to be a (minor) source-breaking change if a .NET Standard 2.0 project uses APIs from on one of the removed packages and transitively depended on it. They will have to directly depend on them, and it's not the first time we do a change like this (#9055). I don't think that this is a binary-breaking change because the .NET Standard 2.0 binaries are not being used at runtime. --------- Co-authored-by: Jan Provazník <janprovaznik@microsoft.com>
1 parent 9ce7e9c commit 0bc107d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/Framework/Microsoft.Build.Framework.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
3636
</ItemGroup>
3737

38+
<!-- When targeting NS2.0, make private all references not exposed in the public API. -->
3839
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">
39-
<!-- Need Win32 API on .NET Standard to ping registry for some methods in ToolLocationHelper -->
40-
<PackageReference Include="Microsoft.Win32.Registry" />
40+
<PackageReference Update="System.Collections.Immutable" PrivateAssets="all" />
41+
<PackageReference Update="System.Diagnostics.DiagnosticSource" PrivateAssets="all" />
4142
</ItemGroup>
4243

4344
<ItemGroup>

src/Tasks/Microsoft.Build.Tasks.csproj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,6 @@
696696
<Content Include="$(NuGetPackageRoot)microsoft.net.compilers.toolset\$(MicrosoftNetCompilersToolsetVersion)\tasks\net472\**\*" CopyToOutputDirectory="PreserveNewest" LinkBase="Roslyn" />
697697
</ItemGroup>
698698

699-
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">
700-
<PackageReference Include="Microsoft.Win32.Registry" />
701-
<PackageReference Include="System.Reflection.Metadata" />
702-
<PackageReference Include="System.Threading.Channels" />
703-
</ItemGroup>
704-
705699
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
706700
<PackageReference Include="System.CodeDom" />
707701
<PackageReference Include="System.Security.Cryptography.Pkcs" />
@@ -710,6 +704,20 @@
710704
<Content Include="$(NuGetPackageRoot)microsoft.net.compilers.toolset\$(MicrosoftNetCompilersToolsetVersion)\tasks\netcore\**\*" CopyToOutputDirectory="PreserveNewest" LinkBase="Roslyn" />
711705
</ItemGroup>
712706

707+
<!-- When targeting NS2.0, make private all references not exposed in the public API. -->
708+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">
709+
<ProjectReference Update="..\StringTools\StringTools.csproj" PrivateAssets="all" />
710+
<PackageReference Update="System.CodeDom" PrivateAssets="all" />
711+
<PackageReference Update="System.Collections.Immutable" PrivateAssets="all" />
712+
<PackageReference Update="System.Resources.Extensions" PrivateAssets="all" />
713+
<PackageReference Update="System.Security.Cryptography.Pkcs" PrivateAssets="all" />
714+
<PackageReference Update="System.Security.Cryptography.Xml" PrivateAssets="all" />
715+
<PackageReference Update="System.Threading.Tasks.Dataflow" PrivateAssets="all" />
716+
<PackageReference Include="Microsoft.Win32.Registry" PrivateAssets="all" />
717+
<PackageReference Include="System.Reflection.Metadata" PrivateAssets="all" />
718+
<PackageReference Include="System.Threading.Channels" PrivateAssets="all" />
719+
</ItemGroup>
720+
713721
<ItemGroup>
714722
<!--
715723
Content items in this project are used to copy files to the output directory but we don't

src/Utilities/Microsoft.Build.Utilities.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" PrivateAssets="all" />
3232
</ItemGroup>
3333

34+
<!-- When targeting NS2.0, make private all references not exposed in the public API. -->
3435
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'">
35-
<PackageReference Include="System.Text.Encoding.CodePages" />
36+
<ProjectReference Update="..\StringTools\StringTools.csproj" PrivateAssets="all" />
37+
<PackageReference Update="Microsoft.IO.Redist" PrivateAssets="all" />
38+
<PackageReference Update="System.Collections.Immutable" PrivateAssets="all" />
39+
<PackageReference Update="System.Configuration.ConfigurationManager" PrivateAssets="all" />
40+
<PackageReference Include="Microsoft.Win32.Registry" PrivateAssets="all" />
3641
</ItemGroup>
3742

3843
<ItemGroup Label="Shared Code">

0 commit comments

Comments
 (0)