2

My solution(.sln) contains contains 21 projects and the nuget restore takes a lot of time(4m and 56s).

I am trying to cache the nuget packages for faster restore.

I've successfully cached the /.nuget/packages and it caches correctly but when the build runs it tries to access the project.assets.json for every project in solution and I get the following error: error NETSDK1004: Assets file 'd:\a\1\s\src\Project\obj\project.assets.json' not found. Run a NuGet package restore to generate this file. [d:\a\1\s\src\Project\Project.csproj]

I've found from the Microsoft community support forums that I need to cache also the project.assets.json for every project... But how? The cache task doesn't support wildcards to recursively search through each project's project.assets.json.

Another solution would be to write 21 cache tasks with the exact path for each project, but this doesn't look right... maybe I'm doing something wrong?

I really appreciate any help you can provide!

Thanks!

EDIT here is my auzre-pipelines.yml:

trigger: none

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  configuration: Release
  NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages
  CACHE_RESTORED: 'false'

- task: UseGitVersion@5
  displayName: 'Install GitVersion5'
  inputs:
    versionSpec: '5.x'
    additionalArguments: '/output buildserver -config .\GitVersion.yml'

- task: NuGetToolInstaller@1
  displayName: 'Install NuGet 5.3.x'
  inputs:
    versionSpec: '5.3.x'

- task: Cache@2
   inputs:
     key: 'v1 | nuget | "$(Agent.OS)"'
     path: $(NUGET_PACKAGES)
     cacheHitVar: CACHE_RESTORED

 - task: NuGetCommand@2
   inputs:
     command: 'restore'
     condition: ne(variables.CACHE_RESTORED, 'true')
     restoreSolution: '**/*.sln'
     feedsToUse: 'select'
     vstsFeed: 'a-dummy-feed'

- task: VSBuild@1
  inputs:
    solution: '**\*.sln'
    msbuildArgs: '/t:Restore'
    platform: 'any cpu'
    configuration: 'Release'
    clean: true
    logProjectEvents: false

- task: NuGetCommand@2
  displayName: 'Generate .nupkg'
  inputs:
    command: 'pack'
    packagesToPack: '**/*.csproj'
    configuration: '$(configuration)'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'UseGitVersion.GitVersion.NuGetVersion

 - task: NuGetCommand@2
   displayName: 'Push .nupkg to AzureArtifacts Feed'
   inputs:
     command: 'push'
     packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
     nuGetFeedType: 'internal'
     publishVstsFeed: 'a-dummy-feed'
     publishPackageMetadata: false
     allowPackageConflicts: true
David Petric
  • 324
  • 3
  • 17

2 Answers2

2

I ran into a similar problem showing the same error message. The root cause in my case was that not all the projects in my solution were set up to generate a packages.lock.json file.

It was in the projects where the packages.lock.json file was not present that dotnet build was complaining of not being able to find the project.assets.json file.

The solution to my problem was to enable the generation of a nuget lock file across all projects in my solution. Here's what I did, step by step.

  1. Placed a Directory.Build.props file at the root folder for my solution, containing this:

    <Project>
      <PropertyGroup>
        <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
        <DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
      </PropertyGroup>
    </Project>
    
  2. Cleaned my solution and rebuilt it to generate all the individual packages.lock.json files. If you try this but files are not generated for some projects, try closing Visual Studio and re-opening it before rebuilding.

  3. Added the Build.Directory.props file and all packages.lock.json files to git and committed them.

PS - I have a slightly different key in my cache task inputs:

    key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**'

This makes the task generate a cache for every project in my solution.

CyberDude
  • 8,541
  • 5
  • 29
  • 47
urig
  • 16,016
  • 26
  • 115
  • 184
1

According to the error message, the project.assets.json file doesn’t exist in the path.

You could try to add the /t:Restore in the MSBuild Arguments.

Arguments

Then the project.assets.json file will be automatically added during the build process.

result

Hope this helps.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Thanks the VSBuild@1 task now works, but my nuget pack task is failing with the following error: Error NU5012: Unable to find 'd:\a\1\s\src\App\Project\bin\Release\Project.dll'. Make sure the project has been built. NuGet.Packaging.Core.PackagingException: Unable to find 'd:\a\1\s\src\App\Project\bin\Release\Project.dll'. Make sure the project has been built. – David Petric May 18 '20 at 10:26
  • 1
    You could try to use the "dotnet pack" task. This [ticket](https://stackoverflow.com/questions/45815070/error-nu5012-nuget-pack-unable-to-find-path-bin-release-myproject-bin-releas) may be useful. – Kevin Lu-MSFT May 19 '20 at 01:17
  • 1
    Thank you for your time Kevin, but because of other issues with this pipeline, I ended up recreating the pipeline from scratch without using the Cache task as it didn't minimize the build time, you can read more about it here: https://developercommunity.visualstudio.com/content/problem/1034379/how-to-cache-the-projectassetsjson-for-each-projec.html – David Petric May 19 '20 at 10:14
  • 1
    Hi @David Glad to know that you have found the suitable method to achieve your requirements. – Kevin Lu-MSFT May 20 '20 at 03:26