Using target runtime win-x64 include runtime assets which should be excluded
See original GitHub issueDescribe the bug
I have a nuget package with a .targets file to exclude a dependency from runtime. It works (i.e. dependency is not included) when publishing in Visual Studio with target runtime “portable” but dependency is included when target runtime is “win-x64”
The nuget package “NoRuntimeTest” (.nupkg not allowed, renamed to .zip NoRuntimeTest.1.0.0.zip) contain a NoRuntimeTest.targets
file
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Update="NoRuntimeTest">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
</Project>
When added to a new console app, Visual Studio shows it like this i.e. the dll is correctly shown a compile time only dependency. (the Colors.Net.dll is just an example)
When publishing with target runtime “portable” the .deps.json
looks like: (no dependency on Colors.Net.dll)
"targets": {
".NETCoreApp,Version=v6.0": {
"NoRuntimeTestApp/1.0.0": {
"dependencies": {
"NoRuntimeTest": "1.0.0"
},
"runtime": {
"NoRuntimeTestApp.dll": {}
}
},
"NoRuntimeTest/1.0.0": {}
}
},
but when publishing with target runtime “win-x64” the .deps.json
file looks like: (a runtime dependency on Colors.Net.dll)
".NETCoreApp,Version=v6.0/win-x64": {
"NoRuntimeTestApp/1.0.0": {
"dependencies": {
"NoRuntimeTest": "1.0.0"
},
"runtime": {
"NoRuntimeTestApp.dll": {}
}
},
"NoRuntimeTest/1.0.0": {
"runtime": {
"lib/net6.0/Colors.Net.dll": {
"assemblyVersion": "1.1.0.0",
"fileVersion": "1.1.0.0"
}
}
}
To Reproduce
In Visual Studio:
- Create a new net 6.0 console application
- Add the attached “NoRuntimeTest” nuget package
- Build and publish to a folder with target runtime “portable”
- Build and publish to a folder with target runtime “win-x64”
I noted publishing from command line will always include the dependency!
- dotnet new console --name …
- dotnet add package NoRuntimeTest --source …
- dotnet publish --output Portable --framework net6.0
- dotnet publish --output Win64 --framework net6.0 --runtime win-x64 --no-self-contained
Also adding <excludeAssets>runtime</excludeAssets>
to .csproj will always exclude the runtime dependency.
What is correct behavior?
And if correct behavior is to include the runtime dependency, what should I do to exclude it?
(I can’t use the developmentDependency
tag in .nuspec because it is transitive)
Further technical details
dotnet --info
dotnetinfo.txt
VS 2022 17.0.2 on windows 10
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
Modifying
PackageReference
items in .targets files that are included in packages is not supported. This is because thePackageReferences
are an input to the NuGet restore process, and the .targets files from packages are not used until after NuGet restore is complete.In Visual Studio, it can sometimes appear to work, but it is not recommended.
FYI @dotnet/nuget-team @nkolev92
Thank you all for your input. I now know how to solve my problem.