Search code examples
visual-c++msbuildnugetgoogletestnuget-package-restore

How to get MSBuild to to automatically download new nuget packages referenced in a solution/project?


I am in the process of switching from building googletest from source for my projects to using the nuget package Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1 within Visual Studio 2019. Forewarning, this is my first foray into Nuget so this issue may be something very basic that I'm missing.

Everything works fine (AFAIK) when building/running from within the VS IDE. However, on my build machine (where builds are automated using MSbuild and triggered from source control commits, the test projects fail to build with the error:

Note: obviously the path below is changed but I made sure the directory nesting level is the same

C:\Path\To\Project\SolutionName\ProjectName\TestsProject\TestProject.vcxproj(215,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ....\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets.

The automated build process builds using the following MSBuild command:

msbuild ""C:\Path\To\Project\SolutionName\SolutionFile.sln"" /target:Rebuild /Property:Configuration=Release;Platform=x64 /m /consoleloggerparameters:ErrorsOnly /fileLogger /fileloggerparameters:LogFile=""C:\Path\To\Project\SolutionName\BuildLog.txt"" /flp1:LogFile=""C:\Path\To\Project\SolutionName\BuildErrors.txt"";ErrorsOnly /flp2:LogFile=""C:\Path\To\Project\SolutionName\BuildWarnings.txt"";WarningsOnly

I have been through the link in the error what seems like countless times (specifically the section of the document pertaining to MSBuild). This lead me down the path of adding an additional Restore target on the above command (changing to .. /target:Restore;Rebuild ...) but that didn't work (don't remember the exact error but I can obviously duplicate it again if anyone thinks it's relevant after reading more). I tried several other iterations of this but where I'm at right now, I believe is the correct way to do this but it's not working so that's why I'm here for help. Right now, I have the following command being executed right before the above Build command:

msbuild "C:\Path\To\Project\SolutionName\SolutionFile.sln" /target:Restore /Property:Configuration=Release;Platform=x64

The output of this command is:

Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Framework

Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.

Build started 6/3/2020 1:02:06 AM.

Project "C:\Path\To\Project\SolutionName\SolutionFile.sln" on node 1 (Restore target(s)). ValidateSolutionConfiguration:

Building solution configuration "Release|x64". ValidateProjects:

The project "unrelated_project_in_solution_1" is not selected for building in solution configuration "Release|x64".

The project "unrelated_project_in_solution_2" is not selected for building in solution configuration "Release|x64".

The project "unrelated_project_in_solution_3" is not selected for building in solution configuration "Release|x64".

Restore:

Nothing to do. None of the projects specified contain packages to restore.

Done Building Project "C:\Path\To\Project\SolutionName\SolutionFile.sln" (Restore target(s)).

Build succeeded. 0 Warning(s) 0 Error(s)

Time Elapsed 00:00:01.99

And the of course (since the restore didn't do anything) what follows is the project failing to build with the same error as before I added the command.

So what am I missing here? My logical thought process tells me the project file isn't referencing the nuget package but it appears to be there (both in the IDE and the .vcxproj file) so I'm at a loss as to why the restore command isn't finding it.

UPDATE1 - OK, after reading through https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets apparently the msbuild restore option only works with PackageReference style projects and, by default, VS2019 still creates packages.config style projects for C++ projects.

So it seems I need to switch the project to using PackageReference but I'm still fuzzy on how to do that since it seems that PackageReference may not be supported at all for C++ projects? Or is it just the migration tool that doesn't work with C++ projects? https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference

Alternatively, I need to find a way to manage the install automatically using a package.config style project but I don't yet understand how to do that either.

UPDATE2- PackageReference is not supported at all for native C++ projects in MSBuild. There is an ongoing discussion here (last updated in March 2020 by a MSFT rep) saying they will add support for C++/Cli but that native c++ requires a lot more work. https://developercommunity.visualstudio.com/idea/351636/use-packagereference-in-vcxproj.html So, I guess I am stuck managing this external to MSBuild.


Solution

  • Came back to this to post Update 2 as an answer because it's still current as of now.

    UPDATE2- PackageReference is not supported at all for native C++ projects in MSBuild. There is an ongoing discussion here (last updated in March 2020 by a MSFT rep) saying they will add support for C++/Cli but that native c++ requires a lot more work. https://developercommunity.visualstudio.com/idea/351636/use-packagereference-in-vcxproj.html So, I guess I am stuck managing this external to MSBuild.