Closed MatthieuMEZIL closed 3 weeks ago
@MatthieuMEZIL when you multi-target, NuGet will categorize the contents of the package by target framework as well. It does this by running a target once per target framework to get any extra files you want to include for that target framework. A lot of the functionality of .NET build happens per target framework like compilation so it makes sense for NuGet to only make those properties available for the "inner builds" of the individual target frameworks. That said, it is possible to do what you want:
Documentation: https://learn.microsoft.com/nuget/reference/msbuild-targets#targetsfortfmspecificbuildoutput
Example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<IsPackable>true</IsPackable>
<Authors>Name</Authors>
<Company>Company</Company>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeSystemTextJson</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.5" ExcludeAssets="all" PrivateAssets="all" GeneratePathProperty="true" />
</ItemGroup>
<Target Name="IncludeSystemTextJson">
<ItemGroup>
<BuildOutputInPackage Include="$(PkgSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" TargetPath="SomeDirectory" />
</ItemGroup>
</Target>
</Project>
Result:
Talked offline with @MatthieuMEZIL and the solution I provided works.
NuGet Product Used
MSBuild.exe
Product Version
.NET 8.0.203, msbuild 17.11.9.46202, nuget 6.10.1
Worked before?
No
Impact
It's more difficult to complete my work
Repro Steps & Context
Output project does not support GeneratePathProperty causing GenerateNuspec target to fail.
It fails because PkgSystem_Text_Json is empty for the output project. See NuGetBug.csproj.nuget.g.props:
I don't understand why PkgSystem_Text_Json is only defined when TargetFramework is set, and this is causing GenerateNuspec target to fail.
Verbose Logs