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

Restore fails when .NET Framework Project depends on C++ CLI project #10511

Closed
snthomae opened this issue Feb 1, 2021 · 11 comments
Closed

Restore fails when .NET Framework Project depends on C++ CLI project #10511

snthomae opened this issue Feb 1, 2021 · 11 comments
Labels

Comments

@snthomae
Copy link

snthomae commented Feb 1, 2021

Details about Problem

After upgrading to Visual Studio 16.8 (which integrates NuGet 5.8.1), restore is failing when .NET Framework project is referencing a C++ CLI Project via ProjectReferences. Also setting a TargetFramework Version in C++ Project has no impact.
C++ Project is always treated as Target Framework 'Native' which seems not to be compatible to any .NET Framework Project.

NuGet product used (NuGet.exe | Visual Studio | MSBuild.exe | dotnet.exe):

Visual Studio Version 16.8.4.
MSBuild 16.8.3.61104
NuGet 5.8.1

Worked before? If so, with which NuGet version:

Worked before with Visual Studio 16.7.X and NuGet 5.7.0.

Repro steps and/or sample project

  1. Create a new .NET Framework Project.
  2. Create a new C++ CLI Project.
  3. Edit .NET Framework Project and add a ProjectReference to C++ CLI Project (should have same TargetFramework as .NET Framework Project).
  4. Edit C++ CLI Project and add a PackageReference (e.g. Newtonsoft.Json).
  5. Open Visual Studio 2019 Development Shell.
  6. Call 'msbuild /t:restore' on .NET Framework Project.

Verbose Logs

Checking compatibility for NativeProjectExample with .NETFramework,Version=v4.8.
<PATH>DotNetExample.csproj : error NU1201: Project NativeProjectExample is not compatible with net48 (.NETFramework,Version=v4.8). Project NativeProjectExample supports: native (native,Version=v0.0)
@snthomae snthomae changed the title Restore fails when .NET Framework Project reference Native project Restore fails when .NET Framework Project depends on Native project Feb 1, 2021
@nkolev92
Copy link
Member

nkolev92 commented Feb 5, 2021

Hey @snthomae

PackageReference is not supported for native projects. Tracked here: #8874.

Note that the NuGet team does not have absolute control here, so issues like that are best filled on developer community.

@nkolev92 nkolev92 added Partner:C++ Resolution:ByDesign This issue appears to be ByDesign Resolution:NotABug This issue appears to not be a bug Style:PackageReference WaitingForCustomer Applied when a NuGet triage person needs more info from the OP labels Feb 5, 2021
@snthomae
Copy link
Author

snthomae commented Feb 5, 2021

Hey @nkolev92,

thank you for the reply.

Even when it's not supported, PackageReference is working with native projects. At least calling nuget restore with NuGet Client 5.8.4 on native project is restoring the PackageReferences as expected.
Problem just comes up when there is a ProjectReference between a .NET and Native Project and restore is called on the .NET project.
This just comes up with NuGet Client 5.8.4 whereas same scenario is working with NuGet Client 5.7.0.
So there must be a change added to NuGet between these version that defines the incompatibility between .NET and Native Projects.

Therefore, I would appreciate if you can share some information about this change.

@nkolev92
Copy link
Member

nkolev92 commented Feb 5, 2021

Even when it's not supported, PackageReference is working with native projects. At least calling nuget restore with NuGet Client 5.8.4 on native project is restoring the PackageReferences as expected.

Due to the nature of MSBuild, the tooling cannot block every unsupported scenario.
PackageReference is 2 parts, one is restore the other side is build. Even if the restore side might seem to work, the build side is not implemented right now.

Problem just comes up when there is a ProjectReference between a .NET and Native Project and restore is called on the .NET project.

In the scenario described, the .NET project is not PackageReference. The error suggests that the project is PackageReference.
I'd expect restore to fail with vcxproj because a package such as newtonsoft.json does not support native.

This just comes up with NuGet Client 5.8.4 whereas same scenario is working with NuGet Client 5.7.0.

Can you please upload a project? Following the instructions I just get a failure when vcxproj is restore (which is expected).

So there must be a change added to NuGet between these version that defines the incompatibility between .NET and Native Projects.

Note that even if the behavior did change, this is still an unsupported scenario and we might not be able to change the new behavior.

@snthomae
Copy link
Author

Hi @nkolev92 ,

sorry for delayed answer.
Please find Test Project attached.

NuGetExample.zip

@nkolev92
Copy link
Member

Hey @snthomae

Trying it out with NuGet.exe I wasn't able to repro it (note that NuGet.exe carries the same bits).

C:\Users\Roki2\Downloads\NuGetExample [master]> C:\Programs\NuGet\nuget-5.8.0.exe restore                                                                                                                                                    MSBuild auto-detection: using msbuild version '16.9.0.63002' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin'.
Restoring packages for C:\Users\Roki2\Downloads\NuGetExample\CppProject\CppProject.vcxproj...
Restoring packages for C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj...
NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)
Committing restore...
Installing boost 1.71.0.0.
Generating MSBuild file C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\obj\DotNetProject.csproj.nuget.g.props.
Generating MSBuild file C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\obj\DotNetProject.csproj.nuget.g.targets.
Writing assets file to disk. Path: C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\obj\project.assets.json
Failed to restore C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj (in 269 ms).
Committing restore...
Generating MSBuild file C:\Users\Roki2\Downloads\NuGetExample\CppProject\obj\CppProject.vcxproj.nuget.g.props.
Generating MSBuild file C:\Users\Roki2\Downloads\NuGetExample\CppProject\obj\CppProject.vcxproj.nuget.g.targets.
Writing assets file to disk. Path: C:\Users\Roki2\Downloads\NuGetExample\CppProject\obj\project.assets.json
Restored C:\Users\Roki2\Downloads\NuGetExample\CppProject\CppProject.vcxproj (in 19.44 sec).

Errors in C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj
    NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)

NuGet Config files used:
    C:\Users\Roki2\Downloads\NuGetExample\NuGet.Config
    C:\Users\Roki2\AppData\Roaming\NuGet\NuGet.Config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    C:\Program Files (x86)\NuGet\Config\Xamarin.Offline.config

Feeds used:
    C:\Users\Roki2\Downloads\NuGetExample\.nuget

Installed:
    1 package(s) to C:\Users\Roki2\Downloads\NuGetExample\CppProject\CppProject.vcxproj
C:\Users\Roki2\Downloads\NuGetExample [master +3 ~0 -0 !]>                                                                                                                                                                                   C:\Users\Roki2\Downloads\NuGetExample [master +3 ~0 -0 !]> C:\Programs\NuGet\nuget-5.7.0.exe restore                                                                                                                                         MSBuild auto-detection: using msbuild version '16.9.0.63002' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin'.
Committing restore...
Assets file has not changed. Skipping assets file writing. Path: C:\Users\Roki2\Downloads\NuGetExample\CppProject\obj\project.assets.json
Restored C:\Users\Roki2\Downloads\NuGetExample\CppProject\CppProject.vcxproj (in 54 ms).
Restoring packages for C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj...
NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)
Committing restore...
Generating MSBuild file C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\obj\DotNetProject.csproj.nuget.g.props.
Assets file has not changed. Skipping assets file writing. Path: C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\obj\project.assets.json
Failed to restore C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj (in 274 ms).

Errors in C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj
    NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)

NuGet Config files used:
    C:\Users\Roki2\Downloads\NuGetExample\NuGet.Config
    C:\Users\Roki2\AppData\Roaming\NuGet\NuGet.Config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config
    C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    C:\Program Files (x86)\NuGet\Config\Xamarin.Offline.config

Feeds used:
    C:\Users\Roki2\Downloads\NuGetExample\.nuget
1 of 2 projects are up-to-date for restore.
C:\Users\Roki2\Downloads\NuGetExample [master +3 ~0 -0 !]> 

cc @zivkan who's the handling incoming issues this week.

@snthomae
Copy link
Author

snthomae commented Feb 12, 2021

Hey @nkolev92

but you were able to reproduce it - you are getting exactly same NU1201 error as mentioned:
Errors in C:\Users\Roki2\Downloads\NuGetExample\DotNetProject\DotNetProject.csproj NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)

This error doesn't come up when you do a restore with nuget version < 5.8.X.

@nkolev92
Copy link
Member

@snthomae

If you look at my logs again you will see that I ran the same thing with 5.7.0 and got the same result.

The indentation is messed up but search for nuget-5.7.0.exe.

@snthomae snthomae changed the title Restore fails when .NET Framework Project depends on Native project Restore fails when .NET Framework Project depends on C++ CLI project Feb 12, 2021
@snthomae
Copy link
Author

@nkolev92
Sorry, I've uploaded the wrong Test Project (TargetFramework property was missing in C++ project).

Please can you retry it again with following sample:
NuGetExample.zip

Result with NuGet 5.7.0:
Checking compatibility for boost 1.71.0.0 with .NETFramework,Version=v4.8.

Result with NuGet 5.8.X:
Checking compatibility for CppProject 1.0.0 with .NETFramework,Version=v4.8. NU1201: Project CppProject is not compatible with net48 (.NETFramework,Version=v4.8). Project CppProject supports: native (native,Version=v0.0)

@nkolev92
Copy link
Member

Thanks for clarifying. I can repro the behavior.

Unfortunately, this is exactly where we get in the unsupported scenario.

Prior to 5.8 (and .NET Core 5.0), NuGet and the dotnet SDK were incorrectly handling the TargetFramework inference.
They were using TargetFrameworks instead of TargetFrameworkMoniker. For vcxproj, NuGet just assumes native. The TargetFrameworks property took precedence, which was incorrect.

When frameworks of the net5.0-windows were introduced in .NET 5, NuGet and the SDK needed to fix this longstanding inconsistency.
You can read more about these issues: #9756 and #8388.

You can add another hack on top of your current behavior by adding <AssetTargetFallback>$(AssetTargetFallback);native</AssetTargetFallback>.

Unfortunately at this point, we can't commit to making any changes. NuGet does not have support for vcxproj projects & AssetTargetFallback is the big hammer to workaround 2 things being incompatible.

@nkolev92 nkolev92 removed Resolution:NotABug This issue appears to not be a bug WaitingForCustomer Applied when a NuGet triage person needs more info from the OP labels Feb 12, 2021
@ghost
Copy link

ghost commented May 20, 2021

thanks so much for this incredible trick - I spent hours to fix the pipeline and now finally with the "remove" or "condition" in the Directory.Build.Props it works

@jarig
Copy link

jarig commented Apr 15, 2022

@lorenz-haenggi-jarowa can you please share your condition or remove in the Directory.Build.Props?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants