PackageReference for NuGet packages in C++ projects - visual-studio-2017

I'm part of a project where we use packages.config files for NuGet packages in Visual Studioe and then include each package dependency through the Import statement in our project files.
This works fine if you always want the packages, but we have some conditional packages that we'd like to exclude if the developer doesn't need/want it. I found the PackageReference tag, that would make the dependency much cleaner and would let us include the packages conditionally.
It seems to be fully supported in C# projects, but I can't figure out if it's supported for C++ projects or not.
I have tried deleting all of our packages.config files and replacing all references with PackageReference tags, but the PackageReference tag doesn't seem to be picked up by the package manager. I've gone through all the "migration tools" that I could find for Visual Studio too, with no luck.
Is it possible to use it for C++ projects? If not, is there a workaround that lets me exclude certain packages conditionally?
I'm using Visual Studio Professional 15.6.7.

Is it possible to use it for C++ projects?
At this moment, PackageReference is not yet supported for C++ projects. NuGet team is evaluating it to support for future releases. You might had made it work through some hacks but if you create a new c++ project and tries to install this package as PackageReference, it won't allow you. So I recommend you to continue using packages.config for your c++ projects and libraries.
Besides, you can add your request or vote for this feature on the Visual Studio UserVoice site: Use PackageReference in vcxproj. When there are enough communities vote and add comments for this feedback, the product team member will take this feedback seriously.
If not, is there a workaround that lets me exclude certain packages conditionally?
You can include the conditional references in the custom targets file and deploy the dlls in the tools folder of the package so they are not added as references by Nuget automatically.
certified: NuGet update and conditional references
Hope this helps.

While not supported, it IS possible to use PackageReference with C++ projects.
If you check the comments at https://developercommunity.visualstudio.com/t/use-packagereference-in-vcxproj/351636, there are a couple examples on how to enable this.
Note, the Visual Studio experience may degrade, but building with MSBuild (command line) has had no issues.
Note, we currently use PackageReference for VCXPROJ projects in ReactNativeWindows.
Being an unsupported feature, a couple hacks are required for Visual Studio compatibility, but all in all it works.
https://github.com/microsoft/react-native-windows

To summarize the other answers and provide some more recent information:
PackageReference is not officially supported in C++ projects, as it's not fully integrated with NuGet and Visual Studio, but:
MSBuild supports PackageReference, and Visual Studio 16.9 and later (this is 2019) have some integration implemented as well.
The full steps are described in a comment of the Make the PackageReference support general purpose for all languages PR, but basically:
above the project folder, create Directory.Build.props file with the following content:
<Project>
<Import Project="$(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).*.props" />
</Project>
and Directory.Build.targets file with the following content:
<Project>
<Import Project="$(MSBuildProjectExtensionsPath)$(MSBuildProjectFile).*.targets" />
</Project>
in the project file add the following in the Project node:
<ItemGroup>
<ProjectCapability Include="PackageReferences" />
</ItemGroup>
and
<PropertyGroup>
<NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">native,Version=v0.0</NuGetTargetMoniker>
</PropertyGroup>
(or similar moniker)
Reload the project.
The above will make MSBuild and Package Manager work, with some caveats, e.g. you have to "Save All" after modifying packages through the UI, package references are not shown in the References node in the Solution Explorer, etc.
Note that Augustin Popa, which is PM for the Visual C++ team, gives an explanation why updating projects to the partially supported PackageReference may not be a good idea, because they have other plans for the future:
Our concern there is that if developers do the work to migrate to C++ PackageReference, they would later realize that we don't intend to provide ongoing support and want them to move to something else. I expect that would be a frustrating experience.
The "something else" he's talking about is probably a better integration with vcpkg, as discussed in his comment to the Use PackageReference in vcxproj feature request for Visual Studio:
From our side, we have to consider the needs of a very large user base with diverse requirements, which include support for CMake projects, installing packages for non-Windows platforms, and providing a better story for acquiring libraries that are actually compatible with the executable they attach to and with each other (achieved with vcpkg today). NuGet is not designed to handle these scenarios[...]
And still, even dev teams inside Microsoft are using the PackageReference approach, as seen in this comment for the same feature request:
We have made this work in React Native Windows via the following Pull Request.
As it’s been mentioned, MSBuild can handle this case for C++ (VCXPROJ) projects.
First-class support in the Visual Studio IDE would be highly beneficial for existing projects wanting to drop the traditional NuGet.exe/pakcages.config mechanism but can’t afford to rewrite the whole project to adjust to VCPKG.
At the very least, there is a drive space benefit to using PackageReference, so it’s not only about aesthetics.

Related

How do I fix Visual Studio 2022 Error E1696 for WinRT

When I generate a new WinRT project in Visual Studio 2022 I get Error E1696 cannot open source file "winrt/Windows.Foundation.h" yet when I look at the Include directories the files do exist at the correct location.
This is an artifact of the way C++/WinRT works. While the header files do exist in the Windows SDK, that's not where the project goes looking for them. Instead, they are generated on the fly into the source tree under the Generated Files directory.
So to fix the issue you will have to compile a newly created project at least once. This by itself isn't sufficient for IntelliSense to pick up the changes in environment. To help IntelliSense out you're going to have to right-click into the source editor, and select Rescan -> Rescan File.
Once that is done, all the IntelliSense errors go away, including E1696.
Historic background
It's easy to get confused why the C++/WinRT header files are part of the Windows SDK, yet the C++/WinRT VSIX templates aren't using them. A look back at C++/WinRT's history helps explain how we landed in this situation:
Initially, the code generator responsible for producing the language projection header files (modern.exe, later renamed to cppwinrt.exe) wasn't published. Instead, the header files were generated by Kenny Kerr, and published through his modern repo.
Publishing the language projection header files through a GitHub repository carried over into the cppwinrt repo owned by Microsoft, and continued to be the deployment channel for several versions of Windows.
This wasn't exactly convenient for developers, so with the release of the Windows SDK for Windows 10, version 1803 (10.0.17134.0) the headers got added to the SDK (including the code generator). This worked, but wasn't an ideal situation either, as the release cycle of C++/WinRT was now tied to that of the Windows SDK, roughly 6 months.
Decoupling the release cycles was crucial in allowing C++/WinRT to progress at its own pace, shipping frequent updates with improvements and bug fixes. This was enabled by deploying the code generator as part of a NuGet package that the C++/WinRT project templates reference. The MSBuild project drives generation of the language projection headers, and clients can freely decide, which version of the C++/WinRT library they wish to use, controlled through the NuGet package reference.
This is how things work today, but the language projection headers can no longer be yanked from the Windows SDK. They were published, with clients relying on finding them there, and expecting an SDK update to not break their builds. And this is why the Windows SDK contains header files you aren't meant to be using.
Often a Build --> Clean Solution followed by a Build --> Build Solution is enough to resolve the issue. Give Visual Studio a few seconds to complete any background work.
If that fails try reinstalling the Microsoft.Windows.CppWinRT NuGet package.
Go to Tools --> NuGet Package Manager --> Manage NuGet Packages For Solution...
In the NuGet Solution pane choose the Installed option.
Tick the CheckBox next to your Project name in the Window to the right.
Click the Uninstall button
And click Ok in the Change Preview dialog box.
The CPPWinRT package will now be removed.
Change to the Browse option in the NuGet Solution pane.
Type "cppwinrt" into the Search text box of the pane.
Select the Microsoft.Windows.CppWinRt package and install it.
Finally choose the Build --> Build Solution option.
After the Build has been completed give Visual Studio a few more seconds to complete any background work and the errors should be gone.

Can't use my installed NuGet package in code

I have installed NuGet package Miracle.FileZilla.Api in my VS project, but when I try to include it in my code via directive "using" VS says there's no namespace "Miracle". How do I fix it? Thanks.
I have installed NuGet package Miracle.FileZilla.Api in my VS project,
but when I try to include it in my code via directive "using" VS says
there's no namespace "Miracle". How do I fix it?
The main issue is that the VS Code tool level like nuget package manager is too low to be used in a more advanced VS IDE. And thanks to Matti for the tips and information that make this question much simpler.
VS Code is a lightweight code editor and can install many extensions for development environment tools, such as compiling, installing nuget packages, publishing, etc.It has wide compatibility with projects, including Java. But tools are not powerful enough to use in some other areas.And it is more suitable for code editing.
VS IDE is a powerful integrated development environment and includes many of the tools of the development process, such as compiling, debugging, publishing, and so on. It is better suited to developing an entire project lifecycle.
In brief, due to the powerful function of tool in VS IDE and wide compatibility of projects in VS Code, nuget package in VS IDE can be used in VS Code and in turn, the errors occurs. Or please install nuget packages and develop projects in the same version of VS.
Solution
1) Please install nuget packages and then use it in the same VS version. And l recommend you use VS IDE which is powerful enough.
2) Or you should create a project and then install the nuget package in VS and then migrate it to VS Code, you can include it in my code via directive "using" without any errors.
Hope it could help you.

Nuget Solution-Level alternative

I have read about Nuget Solution-Level package has been deprecated. Is there an alternative for that? or Which would be the best way to work with a multiple project solution that it is being shared between developers without breaking the references?
It seems to have be deprecated in Nuget 3.0.
According to this document: Maybe the option Manage Nuget Packages for Solution is what you want.
In VS2017, if you have a multiple-project solution, right-click the solution name in Solution Explorer and click Manage Nuget Packages for Solution to manage solution packages:
Visual Studio makes it easy to manage NuGet packages for multiple projects in a solution using this option.
Update:
There is a discussion about bringing them back.

Missing file NuGet.targets on existing project

The scenario is:
1. Created a NEW solution
2. ENABLED download missing packages
3. Add projects to solution that already exists and depends on nuget packages
4. Building the solution generates an error:
... The missing file is <solution folder>\.nuget\NuGet.targets.
Why? Have missing something?
I'm using Visual Studio 2017 Pro on Windows 10
All my searches answer about the scenario where create a new solution and adds a new project that depends on nuget package, that's ok, but when have an existing project, nothing.
<solution folder>\.nuget\NuGet.targets is a file that NuGet's Visual Studio extension used to add in Visual Studio 2010, 2012 and 2013 when you right click on the solution and select "Enable NuGet Package Restore". It would then add an import in all of your project files to import the targets file. I was recently investigating a customer issue and as part of that investigation I found it made the following changes. Near the top of the csproj, it adds something like this:
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
and near the end of the csproj it adds somthing like this:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
So, the problem is that you're creating a new solution, but using existing project files that have been modified in this way. One option is to edit your csrpoj files and remove these changes. Another option is to create new projects, in addition to the new solution, and then copy all your code and content files.
The only disadvantage is if you build on a CI server and use packages.config, your build script needs to run nuget.exe restore, whereas projects that use NuGet.targets could just build the solution and msbuild would execute nuget restore as needed. One advantage of no longer using NuGet.targets is that restoring the whole solution is faster than restoring project by project. Visual Studio automatically restores packages on build, even in VS2010, VS2012 and VS2013, so personally I discourage the use of using this feature, even if you use those old versions of Visual Studio. The benefit of reducing your build script by one step is not worth the issues it brings, in my opinion.

Type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft'

I can see from here, that the namespace Visual Studio should exist within namespace Microsoft, yet I get that error and none of my tests are working!
The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
I am using Visual Studio 2012 with Windows 8.1 (all updated).
I got this error when trying to build a project in TFS. These steps fixed it:
remove reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework
add nuget MSTest.TestFramework
add nuget MSTest.TestAdapter (optional but needed to run tests inside Visual Studio)
The above steps caused this element to be removed from my .csproj file:
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
And these two were added instead:
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
If you're using VS 2017, simply don't put the original project and the test project in the same folder. This will solve the conflict.
The namespace Microsoft.VisualStudio... is defined in assemblies which are not part of the standard Installation of Visual Studio. It is available only if you installed the Visual Studio SDK.
The MSDN Reference to the SDK and included namespaces can be found here.
After installing the SDK add a reference to the required assemblies to your project as described here.
EDIT: Please also check your Visual Studio Version. In the MSDN Link you have posted it says:
You can customize some aspects of Visual Studio Ultimate or Visual Studio Premium to extend existing features or to add new capabilities if you have special requirements. The API reference provides information about the classes to help you with your customization.
If you have one of these Versions, open your unit test project, right click on 'References' -> Choose 'Add Reference' -> Click on 'Assemblies' on the top left and search in the List for 'Microsoft.VisualStudio.TestTools.UnitTesting' (Its either in the 'Framework' or in the 'Extensions' section which you can also choose on the left). You do not need to know the actual location of the dll.
For VS 2017, I had to change the Target Framework in Properties
Ok I found out what the issue was. I had all the solution files in a different place than the usual default.
I had them in a Dropbox folder hoping that I can work on the solution from multiple computers, but apparently that was the cause of the issue.
Moving the whole solution to the default My Documents/VS 2012/Projects/ fixed the problem for me!
In short, if you want things to work smoothly, have the solution files in the usual place, unless you're an advanced user and know what you're doing (I'm not).
I suggest avoid using the "Add reference", it took me some time but I came across this NuGet that has everything that is needed for CodedUITests.
https://www.nuget.org/packages/CodedUIDependencies/1.0.0
Copy Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll somewhere into the root folder of project. When later you will be building test project in console or otherwise, copy it back to debug\bin.
If a project has no reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework I have experienced problems launching tests from Visual Studio UI -> glitches.
I got this error after installing the NuGet Microsoft.JavaScript.UnitTest. After uninstall it worked.
https://www.nuget.org/packages/Microsoft.JavaScript.UnitTest/
In my case, I had an invalid private package source. Because of this, I could not install any package at all (of course). Removing the invalid package source also allowed Visual Studio to install the packages correctly.