msbuild: Project-to-project reference fails to detect correct multi-target framework if is defined
If a project has both <TargetFramework> and <TargetFrameworks> defined, other projects cannot correctly reference the project’s target framework that is different than what <TargetFramework> specifies.
Building the project with both properties defined does correctly produce the binaries for each targeted framework. It’s just project-to-project references that doesn’t work.
Create project A (ClassLibrary1) like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
and project B (ClassLibrary2) like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
</Project>
Project B (ClassLibrary2) will fail to build with the following error:
Project '..\ClassLibrary1\ClassLibrary1.csproj' targets 'net6.0-windows'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v6.0'. ClassLibrary2 C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 1806
From the binlog, it seems MSBuild will treat project A as having a single target framework:

If I remove <TargetFramework> in project A, then MSBuild correctly treats project A as having multiple target frameworks:

About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 4
- Comments: 15 (10 by maintainers)
Then the tooling should enforce this, not act in a completely illogical way of silently ignoring
TFSwhen both it andTFare provided. IfTFStook precedence overTF(as would make sense per the principle of least astonishment), orTFwas merged intoTFS, then this would not be a problem.If implementing the aforementioned logical behaviour is a breaking change, then add a flag we can set in
Directory.Build.propsor ourcsprojs to opt in to that behaviour. But don’t leave this known issue to sit for 2+ years without any action being taken.@rainersigwald a simple verification of unnecessary single TF value in TFs property could be:
Do you think it could be shipped as a MSBuild change wave feature?
When you build the project
ClassLibrary1separately,TFStakes precedence and multi-build comes into play and the inner build replaces theTFspecified in the project file, even ifTFis some garbage value. It builds and it produces multiple outputs.But when using project reference, the resolution logic thinks it’s a single target build because
TFtakes precedence there. Yes, this is more of a design issue than an implementation bug. I don’t know if the team would allow the change since it’s a behavior change, and a huge breaking change at that.In the meantime, the specific problem with your setup considering the current project reference behavior is that you’re trying to reference
Windows-specific implementation to a cross-platform library which is not allowed. If you’re expecting the project to build asnet6.0instead ofnet6.0-windows, you can modify theClassLibrary2project file as shown below…That sounds like a breaking change (adding a warning) for a legal situation (doing a “foreach” with one item isn’t ideal but doesn’t cause very many problems). I would expect a very high bar for such a thing. (Adding it as a BuildCheck/analyzer would make sense to me.)
@dsplaisted I think the SDK team should come up with something, be it a warning or
ProjectReferencebehaviour change. Any solution is fine as long as it’s understandable from User PoV.Thanks for the tip! While it works, the disadvantage is every new project reference made to ClassLibrary1 will need this special workaround. It doesn’t “just work”.
I am currently manually unsetting the target framework in my ClassLibrary1 csproj file by doing this:
This allows other projects to reference ClassLibrary1 normally.
However, unsetting the target framework is also not ideal, since every project in our solution that wants to multi-target now needs to know to manually unset the target framework.
In addition to the build preferring
TFSoverTF, the Visual Studio UI also showsTFSoverTFif both are defined. Here is the ClassLibrary1 properties UI:It just seems to be that if both
TFandTFSare defined,TFSgenerally trumpsTF. I feel like the current project reference behavior preferringTFoverTFSseems inconsistent with how other areas of MSBuild works.Hopefully changing the project reference behavior can be considered!
My company’s build infrastructure specifies a default
TFviaDirectory.Build.propsfile, so I am not in control ofTFhaving a value that seems to contradictTFS.To me, it seems even if
TFSconflicts withTF,TFSshould take precedence? Certainly when I build the project,TFSoverridesTFas expected, as I see multiple binaries being built.