Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NU1702 error is cryptic when referencing a project that targets .netfx and .netcore but has TargetPlatformIdentifier set to Windows #13417

Open
kellyanng opened this issue Apr 26, 2024 · 4 comments
Labels
Area:ErrorHandling warnings and errors/log messages & related error codes. Area:Logging Functionality:Restore Priority:2 Issues for the current backlog. Type:Bug

Comments

@kellyanng
Copy link

NuGet Product Used

MSBuild.exe

Product Version

Visual Studio 17.9.6 so presumably the same msbuild

Worked before?

Unknown

Impact

It bothers me. A fix would be nice

Repro Steps & Context

Repro steps:
(I've reproed this within Visual Studio 2022 and from command line, simplified instructions here are from Visual Studio 2022)

  1. Create a new C# library project (WithWindows) in Visual Studio 2022. Set the contents of the csproj file to:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net472;net8.0</TargetFrameworks>
    <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
  </PropertyGroup>

</Project>
  1. Create a second C# library project (ConsumerWithoutWindows) in the same sln.
  2. Add a project reference from ConsumerWithoutWindows to consume WithWindows
    Its project file will look like this:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\WithWindows\WithWindows.csproj" />
  </ItemGroup>

</Project>
  1. Build the project

Actual Result:

Warning	NU1702	ProjectReference 'C:\Repos\WithWindows\WithWindows.csproj' was resolved using '.NETFramework,Version=v4.7.2' instead of the project target framework '.NETCoreApp,Version=v8.0'. This project may not be fully compatible with your project.	ConsumerWithoutWindows	C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets	1867	

Expected Result:
I would expect an error to help me realize that because my first project is limited to Windows that my consuming project would need to be as well. It's really unclear as a user why this would fall back to net472.

More Details:
If I remove the net472 target from the WithWindows project, I get the slightly more helpful:

Error		Project '..\WithWindows\WithWindows.csproj' targets 'net8.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v8.0'.	ConsumerWithoutWindows	C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets	1867	

This as least gives me a better clue as to why my project is failing (something about my TargetFrameworks is definitely mismatching, but it's not clear why). But it still could have a lot clearer instructions that this is failing due to the TargetPlatformIdentifier not matching.

Verbose Logs

No response

@nkolev92
Copy link
Member

Hey @kellyanng

I see a couple of things being called out here, so I wanted to look at them separately:

  1. I would expect an error
    Do you want this to be an error? In that case, AssetTargetFallback needs to be disabled. That's set by default in the SDK.

  2. Regarding the error/warning message.
    Warning part:
    Warning NU1702 ProjectReference 'C:\Repos\WithWindows\WithWindows.csproj' was resolved using '.NETFramework,Version=v4.7.2' instead of the project target framework '.NETCoreApp,Version=v8.0'. This project may not be fully compatible with your project. ConsumerWithoutWindows C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets

What would you like to see different here?
Call out that the fallback is being used. Anything else.

For context, here's the package equivalent:

warning NU1701: Package 'Microsoft.Web.Xdt 2.1.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework
,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project t
arget framework 'net8.0'. This package may not be fully compatible with your project.

Error:

Error		Project '..\WithWindows\WithWindows.csproj' targets 'net8.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v8.0'.	ConsumerWithoutWindows	C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets	1867	

This one is actually mixing the freeform string (TargetFramework value) and the actually TFM values. I think that one could use some work too.

For context, when I try that, I see:

E:\Code\Temp\13417\ConsumerWithoutWindows\ConsumerWithoutWindows.csproj : error NU1201: Project WithWindows is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project WithWindows s
upports: net8.0-windows7.0 (.NETCoreApp,Version=v8.0)

which isn't perfect, but better than the msbuild error IMO.

Any chance you can zip-up a project that reproes the error?
I feel like I'm missing something.

@nkolev92 nkolev92 added Area:ErrorHandling warnings and errors/log messages & related error codes. Area:Logging labels Apr 26, 2024
@kellyanng
Copy link
Author

Sorry, I got so focused on giving the repro steps for the example that I don't think I did a good job explaining why this was so painful for us and how to make this better.

  1. I would like some logging or warnings explaining that a fallback occurred and ideally why it occurred. Even with detailed logging enabled, I couldn't find any clue why ConsumerWithoutWindows was trying to use the net472 version of WithWindows. So, I (and the few other people investigating) just kept staring at it wondering why the net8.0 version of the library was being 'ignored'. It obviously wasn't being ignored, but it seemed like it.

  2. Because the 1702 error isn't documented on learn.microsoft.com we also couldn't use any hints from there about why we the net8.0 version of the library was being ignored. I finally had to just go read the code that introduced the 1702 error, saw references to AssetTargetFallback and thought "Oh...we're falling back! What happens when I remove that fallback?" and then was able to see the more helpful error showing me that there was something different between ConsumerWithoutWindows and WithWindows.

And you're correct, on my simplified repo once I removed net472 from WithWindows I do now see error NU1201 (which is a super-useful error!). I didn't notice that while creating the simplified repro and I just confirmed that I don't see it in our actual repro from our more complex build system (regardless of whether I built from msbuild or Visual Studio).

I've dropped a zipped version of my minimal repo. (Open the WithWindows\WithWindows.sln file to repro), but it seems like you've seen most of the errors already.
NugetIssue13417.zip

@kellyanng
Copy link
Author

Ah, on my actual (i.e. complex repro), I just ran "msbuild /t:restore" and then I got the NU1201 error when my library being consumed had net8.0 and not net472. But I didn't get it until I explicitly ran that NuGet restore. Not sure if that helps you...

@nkolev92
Copy link
Member

nkolev92 commented Apr 29, 2024

Ah, on my actual (i.e. complex repro), I just ran "msbuild /t:restore" and then I got the NU1201 error when my library being consumed had net8.0 and not net472. But I didn't get it until I explicitly ran that NuGet restore. Not sure if that helps you...

That's actually helpful, cause we're now seeing the same things :D

The gist is that MSBuild and NuGet both try to raise errors and unfortunately it seems like you'd get a different one depending on which component is raising it. Those need to be aligned.

These are all very fixable things, especially with all the context, including the missing doc: NuGet/docs.microsoft.com-nuget#2544.

@nkolev92 nkolev92 added the Priority:2 Issues for the current backlog. label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area:ErrorHandling warnings and errors/log messages & related error codes. Area:Logging Functionality:Restore Priority:2 Issues for the current backlog. Type:Bug
Projects
None yet
Development

No branches or pull requests

2 participants