You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apparently PackageReference will work in C++ projects now. We should try it and see if there are any gaps, and if acceptable then remove the packages.config stuff as it complicates things.
Alexander Sklar (@asklar) As it turns out, PackageReference support in C++ projects works today. You will need to do the following:
Add <ProjectCapability Include="PackageReferences" /> to an item group in your vcxproj file or a targets file included by it.
Add a Directory.Build.props file or above the project’s directory, and add an Import element to it with the following value: $(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).*.props. (You can technically add this anywhere that MSBuild will see it, but I find that putting it in Directory.Build.props ensures that it is both included at the correct place, and is also easily shared between all the projects in your solution.)
Add a Directory.Build.targets file in the same location, and add an Import element to it with this value: $(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).*.targets.
Unload and reload your C++ project(s).
After that, not only will NuGet packages auto-restore like with csproj, but the NuGet Package Manager UI will also add and remove PackageReference elements from the project instead of creating a packages.config file. (If this doesn’t seem to work, you may need to use the VS 16.11 beta; that’s what I’m using, and it works great for me.) Note that vcxproj will require you to run File > Save All before changes made in the NuGet UI will persist to the project file, as with any other changes to a vcxproj made using the GUI. Hope this helps!
I think Danny van Velzen (@dannyvv) tried this before, only native projects still rely on traditional NuGet locations (meaning, packages will still be stored in the packages relative folder).
This is definitely useful, however, as it reduces the possibility of version mismatches.
Julio César Rocha (@JunielKatarn) Not necessarily. I have used vcxproj PackageReference with both the global (per-user) NuGet package cache, and a local package cache set via the nuget.config file, and have never ran into any such problems. Keep in mind that either way, the packages will all be unpacked as siblings of the same directory (either the per-user cache, or the path set in the nuget.config file), just as with packages.config. IMHO, if a NuGet package requires a specific restore layout to function properly, that is a bug in that package that should be corrected. (All paths in props/targets files in NuGet packages should be relative to $(MSBuildThisFileDirectory).) What packages do you expect will give problems?
William Kent (@wjk) none specifically. Maybe I was misinformed?
If a Native C++ project can load the packages from the NuGet package cache instead of requiring an explicit location (usually relative to the VCXPROJ file), that would be a huge gain for us, because we wouldn't need to copy the packages for each local git clone.
As it turns out, PackageReferences uses neither! Just run MSBuild.exe /t:Restore to download all needed packages, just as you would do with csproj (or run MSBuild.exe /restore to do so and then build the project).
Yeah I'm excited about the opportunity for simplification this brings!
If it requires a specific min version of VS, then that'd be important to know.
Today our C++ projects (both the framework as well as 3rd party libraries) have hardcoded dependencies to find nuget packages under the solution folder, so being able to remove all that would be great.
Alexander Sklar (@asklar) Support for C++ PackageReference in the VS GUI requires at least 16.9 per dotnet/project-system#6629, which was the last PR needed to implement this feature. Support for restoring C++ PackageReferences via the command line goes back much, much further.
David Hunter (@davidhunter22) React Native for Windows does not support C++/CLI. If you have questions about msbuild or VS support, please file them through VS feedback.
Activity
JunielKatarn commentedon Jul 6, 2021
I think Danny van Velzen (@dannyvv) tried this before, only native projects still rely on traditional NuGet locations (meaning, packages will still be stored in the
packagesrelative folder).This is definitely useful, however, as it reduces the possibility of version mismatches.
wjk commentedon Jul 6, 2021
Julio César Rocha (@JunielKatarn) Not necessarily. I have used vcxproj PackageReference with both the global (per-user) NuGet package cache, and a local package cache set via the
nuget.configfile, and have never ran into any such problems. Keep in mind that either way, the packages will all be unpacked as siblings of the same directory (either the per-user cache, or the path set in thenuget.configfile), just as with packages.config. IMHO, if a NuGet package requires a specific restore layout to function properly, that is a bug in that package that should be corrected. (All paths in props/targets files in NuGet packages should be relative to$(MSBuildThisFileDirectory).) What packages do you expect will give problems?JunielKatarn commentedon Jul 6, 2021
William Kent (@wjk) none specifically. Maybe I was misinformed?
If a Native C++ project can load the packages from the NuGet package cache instead of requiring an explicit location (usually relative to the VCXPROJ file), that would be a huge gain for us, because we wouldn't need to copy the packages for each local git clone.
Did I understand this correctly?
wjk commentedon Jul 6, 2021
That’s correct! 🚀
JunielKatarn commentedon Jul 6, 2021
🤯
I guess I'll be submitting this enhancement for this project... TODAY, then.
One last question. Does this
PackageReferencemechanism work with plain NuGet.exe, or does it require some VS magic?wjk commentedon Jul 6, 2021
As it turns out, PackageReferences uses neither! Just run
MSBuild.exe /t:Restoreto download all needed packages, just as you would do with csproj (or runMSBuild.exe /restoreto do so and then build the project).JunielKatarn commentedon Jul 6, 2021
Pretty much, pure modern MSBuild APIs, then.
Awesome.
asklar commentedon Jul 6, 2021
Yeah I'm excited about the opportunity for simplification this brings!
If it requires a specific min version of VS, then that'd be important to know.
Today our C++ projects (both the framework as well as 3rd party libraries) have hardcoded dependencies to find nuget packages under the solution folder, so being able to remove all that would be great.
wjk commentedon Jul 7, 2021
Alexander Sklar (@asklar) Support for C++ PackageReference in the VS GUI requires at least 16.9 per dotnet/project-system#6629, which was the last PR needed to implement this feature. Support for restoring C++ PackageReferences via the command line goes back much, much further.
mingkuang-Chuyu commentedon Jul 16, 2021
Haha, you only discovered this secret now. How long have I been using it.
Here is a more comprehensive configuration that allows vcxproj to support PackageReference.
Directory.Build.props
davidhunter22 commentedon Aug 9, 2021
asklar commentedon Aug 9, 2021
David Hunter (@davidhunter22) React Native for Windows does not support C++/CLI. If you have questions about msbuild or VS support, please file them through VS feedback.