How am I supposed to perform nuget restore with MSBuild

flagrant99 21 Reputation points
2023-07-05T12:20:54.6533333+00:00

I am migrating an MSBuild 15 project to MSBuild 17. I used to call nuget restore directly. Now "maybe?" I am supposed to MSBuild.exe /restore ( or is it -t:restore ?) with restore arg? This doesn't seem to work however and leaves many nuget assemblies out. It all works fine from Visual Studio Solution and IDE, but fresh checkout never does proper restore using MSBuild. Can someone tell me how to get MSBuild to work like Visual Studio 2022?

I am calling C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe

File Version 17.6.3.22601.

It always leaves out DocumentFormat.OpenXml.dll.

The cproj files have the following

  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml">
      <Version>2.11.0</Version>
    </PackageReference>
  </ItemGroup>
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
40,998 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. flagrant99 21 Reputation points
    2023-07-05T16:46:36.5233333+00:00

    I see this in log file where I rely on MSBuild restore arg to do something. Instead it does nothing.

    Target "ResolveNuGetPackageAssets" skipped, due to false condition; ('$(ResolveNuGetPackages)' == 'true' and exists('$(ProjectLockFile)')) was evaluated as ('true' == 'true' and exists('obj\project.assets.json')).

    There are NO obj\project.assets.json files anywhere so I guess it expects some project lock file?

    I can only get obj\project.assets.json files if I call NUGET.exe directly with restore. So I guess do NOT us msbuild with restore command or use both . Who knows.....

    0 comments No comments

  2. Anna Xiu-MSFT 30,791 Reputation points Microsoft Vendor
    2023-07-06T09:04:00.3566667+00:00

    Hi @flagrant99, 

    Welcome to Microsoft Q&A! 

    You can use msbuild -t:restore to restore packages.

    For more details, you can refer to: Restore by using MSBuild

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. flagrant99 21 Reputation points
    2023-07-06T23:42:02.9333333+00:00

    As far as I know there are three ways to perform NUGET restore. It used to be I just called NUGET.exe restore and all was good. For some reason this stopped working completely in MSBUILD17. There MSBuild has two new forms of restore. msbuild -t:restore and msbuild /restore. They are not the same.

    https://github.com/dotnet/msbuild/issues/3000#issuecomment-417675215

    I finally got my stuff working by calling NUGET.exe restore first to create project.assets.json

    Then msbuild /restore to restore the dlls.

    I am not messing around with the different? -t:restore.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.