Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

project.assets.json not found with msbuild

I have an ASP.NET Core application that i wish to build on a jenkins machine with MSBuild 15.

When i try to build i get the following error:

C:\Program Files\dotnet\sdk\2.1.502\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(198, 5): error NETSDK1004: Assets file 'C:\sync\Src\Util\myUtil\ob j\project.assets.json' not found. Run a NuGet package restore to generate this file

I understand that i need to do nuget restore somehow, but i failed to make it work.

My build process: Running a batch filed with the following command:

call "%VS150COMNTOOLS%VsDevCmd.bat"
MSBuild DailyBuild.proj /t:DailyBuild /p:VersionNumber=%2 /l:FileLogger,Microsoft.Build.Engine;logfile=Build.log

The DailyBuild.proj file look like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <SourcesPath>$(MSBuildProjectDirectory)\..\..\</SourcesPath>
    <CSCleanProperties>BuildType=Clean;Configuration=Release;IsFormalBuild=true</CSCleanProperties>
    <CSBuildProperties>BuildType=ReBuild;Configuration=Release;PauseBuildOnError=false;PublishWebSites=true;VersionName=myProd-$(VersionNumber)</CSBuildProperties>
  </PropertyGroup>

  <Target Name="DailyBuildWithClean">
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="Clean" Properties="$(CSCleanProperties)"/>
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Properties="$(CSCleanProperties)"/>
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="FormalBuild" Properties="$(CSBuildProperties)"/>
  </Target>

  <Target Name="DailyBuild">
    <MSBuild Projects="$(MSBuildProjectDirectory)\Make.proj" Targets="SW;PreparePackFolder" Properties="$(CSBuildProperties)"/>
  </Target>

</Project>

The Make.proj is a proj file containing definitions for many applications to be built, one of them is my ASP.NET Core app.

How do i fix this problem? thank you.

SOLUTION EDIT: Thanks to solution by Martin Ullrich:

Added in the DailyBuild.proj the target Restore, also added in the Make.proj a target called restore as suggested (IE:

<Target Name="Restore">
  <MSBuild Projects="$(SourcesPath)\my.sln" Targets="Restore" />
</Target>

)

like image 230
Tomer Something Avatar asked Mar 31 '19 11:03

Tomer Something


People also ask

What is project assets JSON file?

json file is generated in the process of restoring the NuGet packages in projects that use project. json . It holds a snapshot of all the information that is generated as NuGet walks the graph of packages and includes the version, contents, and dependencies of all the packages in your project.

How do I fix netsdk1004?

To resolve the error, remove the % from the folder name, and rerun dotnet build . A change to the project file wasn't automatically detected and restored by the project system. To resolve the error, open a command prompt and run dotnet restore on the project.

How do I run a NuGet package restore to generate this file?

Restore packages manually using Visual Studio Enable package restore by choosing Tools > Options > NuGet Package Manager. Under Package Restore options, select Allow NuGet to download missing packages. In Solution Explorer, right click the solution and select Restore NuGet Packages.

Does MSBuild use packages config?

MSBuild 16.5+ also has opt-in support for the packages. config format.


2 Answers

Add -r (-restore//Restore) to your MSBuild command to trigger a restore before the main build.

The restore parameter will build the Restore target, clear internal caches and then run the rest of the build as specified.

Since you build a custom MSBuild project, you then need to add a Restore target to it:

<Target Name="Restore">
  <MSBuild Projects="$(SourcesPath)\my.sln" Targets="Restore" />
</Target>

(or alternatively add another Restore target on the make.proj file and forward it from there to the solution or individual projects that you need to be restored)

like image 82
Martin Ullrich Avatar answered Sep 22 '22 08:09

Martin Ullrich


Be careful using Restore Nuget packages directly in MS build task configuration. This option is deprecated, as mentioned here for Azure DevOps. (However, I am not sure how context-dependent that is.)

(Important) This option is deprecated. Make sure to clear this checkbox and instead use the NuGet Installer build step.

Source Link: MSBuild

However, I already used that step (in TFS), so this obviously would not fix it for me.

I tried removing the packages-folder in Source Control Explorer as mentioned here, but that did not fix it either.

Inspired by this, I upgraded the TFS NuGet Installer build step to use Nuget 4.0 (in "Advanced" options), and that did fix it. (Maybe in combination with the removal of the packages-folder?)

like image 40
Yahoo Serious Avatar answered Sep 23 '22 08:09

Yahoo Serious