From 65c8f848b2847699896332435580ee48dd500f9f Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Wed, 5 Oct 2022 15:29:10 -0500 Subject: [PATCH] Multi-targeting logic in Source Generators in NuGet packages doesn't work transitively (#75813) When packages that contain source generators (System.Text.Json and Microsoft.Extensions.Logging.Abstractions) are referenced transitively, the MSBuild logic that controls which source generator version gets picked, and allows for disabling the source generator, doesn't get used in the build. This is because the MSBuild logic is in the "build" folder and not the "buildTransitive" folder. The fix is to move the logic from the "build" to "buildTransitive" folder in the NuGet package. However, we need to also fix up the "NETStandard Compat" build logic to skip adding a placeholder file when the "buildTransitive" folder is not empty. This is a partial backport of https://github.com/dotnet/runtime/commit/538934c25f6b0977cc1ecef1d5ecbab42c7937a0. Fix #60870 --- eng/packaging.targets | 21 ++++++++++++------- ...oft.Extensions.Logging.Abstractions.csproj | 4 ++-- .../src/System.Text.Json.csproj | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/eng/packaging.targets b/eng/packaging.targets index e84c64ae1be90..d86910ccdf6a6 100644 --- a/eng/packaging.targets +++ b/eng/packaging.targets @@ -6,8 +6,8 @@ 6.0.0 - AddNETStandardCompatErrorFileForPackaging;IncludeAnalyzersInPackage;$(PackDependsOn) - AddNETStandardCompatErrorFileForPackaging;IncludeAnalyzersInPackage;$(BeforePack) + IncludeAnalyzersInPackage;AddNETStandardCompatErrorFileForPackaging;$(PackDependsOn) + IncludeAnalyzersInPackage;AddNETStandardCompatErrorFileForPackaging;$(BeforePack) $(PackageOutputPath) $(TargetsForTfmSpecificContentInPackage);AddRuntimeSpecificFilesToPackage;IncludePrivateProjectReferencesWithPackAttributeInPackage $(TargetsForTfmSpecificDebugSymbolsInPackage);AddRuntimeSpecificSymbolToPackage @@ -175,9 +175,8 @@ Condition="'@(AnalyzerReference)' != '' and '$(IncludeMultiTargetRoslynComponentTargets)' == 'true'" DependsOnTargets="GenerateMultiTargetRoslynComponentTargetsFile"> - + + @@ -224,6 +223,7 @@ ]]> + <_NETStandardCompatErrorPlaceholderFilePackagePath>buildTransitive$([System.IO.Path]::DirectorySeparatorChar)%(NETStandardCompatError.Supported) + <_PackageBuildFile Include="@(None->Metadata('PackagePath')); + @(Content->Metadata('PackagePath'))" /> + <_PackageBuildFile PackagePathWithoutFilename="$([System.IO.Path]::GetDirectoryName('%(Identity)'))" /> + + + PackagePath="$(_NETStandardCompatErrorPlaceholderFilePackagePath)\" + Pack="true" + Condition="'@(_PackageBuildFile)' == '' or + !@(_PackageBuildFile->AnyHaveMetadataValue('PackagePathWithoutFilename', '$(_NETStandardCompatErrorPlaceholderFilePackagePath)'))" /> diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/Microsoft.Extensions.Logging.Abstractions.csproj b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/Microsoft.Extensions.Logging.Abstractions.csproj index cf9fa73c283e5..bc940df62bcdd 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/Microsoft.Extensions.Logging.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/Microsoft.Extensions.Logging.Abstractions.csproj @@ -17,8 +17,8 @@ Microsoft.Extensions.Logging.LogLevel Microsoft.Extensions.Logging.Logger<T> Microsoft.Extensions.Logging.LoggerMessage Microsoft.Extensions.Logging.Abstractions.NullLogger - 2 - false + true + 3 diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index d94baea3fba4b..0f0d951726aa8 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -9,8 +9,8 @@ enable true true - false - 6 + true + 7 Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data. Commonly Used Types: