Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Pipelines "Cache@2" fails with "##[error]The system cannot find the file specified"

I'm using Azure Pipelines with hosted builds to build a web project. Our build times were hitting 10-15 minutes, with most (5-10 minutes) of the time spent doing npm install. To speed this up, I'm trying to use the Cache task (https://learn.microsoft.com/en-us/azure/devops/pipelines/caching/?view=azure-devops).

However, when the auto-added task Post-job: Cache runs, it always errors out with:

##[error]The system cannot find the file specified

The host server is Windows Server 2017.

Here is my entire build YAML

# Node.js with Vue
# Build a Node.js project that uses Vue.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- develop

pool:
  name: Default

variables:
  FONTAWESOME_NPM_AUTH_TOKEN: $(FONTAWESOME_NPM_AUTH_TOKEN_VARIABLE)
  npm_config_cache: $(Pipeline.Workspace)/.npm


steps:
- task: DutchWorkzToolsAllVariables@1

- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'


- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    path: $(npm_config_cache)
    cacheHitVar: NPM_CACHE_RESTORED

- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
  condition: ne(variables.NPM_CACHE_RESTORED, 'true')


- task: Npm@1
  displayName: 'npm run build'
  inputs:
    command: 'custom'
    customCommand: 'run build'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.Repository.LocalPath)\dist'
    Contents: '**'
    TargetFolder: '$(Build.StagingDirectory)'
    CleanTargetFolder: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'    

Cache task output:

Starting: Cache
==============================================================================
Task         : Cache
Description  : Cache files between runs
Version      : 2.0.0
Author       : Microsoft Corporation
Help         : https://aka.ms/pipeline-caching-docs
==============================================================================
Resolving key:
 - npm               [string]
 - "Windows_NT"      [string]
 - package-lock.json [file] --> F93EFA0B87737CC825F422E1116A9E72DFB5A26F609ADA41CC7F80A039B17299
Resolved to: npm|"Windows_NT"|rbCoKv9PzjbAOWAsH9Pgr3Il2ZhErdZTzV08Qdl3Mz8=
Information, ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session zzzzz
Information, Getting a pipeline cache artifact with one of the following fingerprints:
Information, Fingerprint: `npm|"Windows_NT"|rbCoKv9PzjbAOWAsH9Pgr3Il2ZhErdZTzV08Qdl3Mz8=`
Information, There is a cache miss.
Information, ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session zzzzz
Finishing: Cache

Post-job: Cache output:

Starting: Cache
==============================================================================
Task         : Cache
Description  : Cache files between runs
Version      : 2.0.0
Author       : Microsoft Corporation
Help         : https://aka.ms/pipeline-caching-docs
==============================================================================
Resolving key:
 - npm               [string]
 - "Windows_NT"      [string]
 - package-lock.json [file] --> 2F208E865E6510DE6EEAA6DB0CB7F87B323386881F42EB63E18ED1C0D88CA84E
Resolved to: npm|"Windows_NT"|OQo0ApWAY09wL/ZLr6fxlRIZ5qcoTrNLUv1k6i6GO9Q=
Information, ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session zzzzz
Information, Getting a pipeline cache artifact with one of the following fingerprints:
Information, Fingerprint: `npm|"Windows_NT"|OQo0ApWAY09wL/ZLr6fxlRIZ5qcoTrNLUv1k6i6GO9Q=`
Information, There is a cache miss.
Information, ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session zzzzz
##[error]The system cannot find the file specified
Finishing: Cache

How can I fix my build definition so the caching works?

like image 285
jklemmack Avatar asked Dec 06 '19 22:12

jklemmack


4 Answers

@Levi Lu-MSFT was right in his comment but there's a gotcha.

@FLabranche has a working solution in his answer but I believe reasoning is not quite right.

The problem

npm install and @Cache task are looking for the npm cache at different locations. Consider the flow when pipeline runs for the first time:

  1. @Cache task: does nothing since there's no cache yet.
  2. npm i (or npm ci) task: installs packages in node_modules/ and updates the npm cache at default location. Default location is ~/.npm on Linux/Mac and %AppData%/npm-cache on Windows. On Linux hosted cloud agent the absolute path will be /home/vsts/.npm.
  3. (... more tasks from your pipeline)
  4. Post-job @Cache task (added implicitly): reads the npm cache found at user-provided location to store it for future reuse. User-provided location is set by the npm_config_cache: $(Pipeline.Workspace)/.npm variable. On Linux hosted cloud agent the absolute path will be /home/vsts/work/1/.npm.

As a result, @Cache task fails with tar: /home/vsts/work/1/.npm: Cannot open: No such file or directory.

Solution

Make npm install and @Cache task use the same npm cache location.

One option suggested by Levi Lu is to update the npm config with npm config set cache $(npm_config_cache) --global but it won't work in the pipeline (at least it didn't work for me in an Azure-hosted Linux agent): Error: EACCES: permission denied, open '/usr/local/etc/npmrc'

npm ci --cache $(npm_config_cache) updates the npm cache location for a single call and it does work in this case. It feels a bit hacky though since --cache option is not even documented on the npm website.

All in all this code worked for me:

variables:
  NPM_CACHE_FOLDER: $(Pipeline.Workspace)/.npm

steps:
- task: Cache@2
  displayName: Cache npm dependencies
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    restoreKeys: |
      npm | "$(Agent.OS)"
      npm
    path: $(NPM_CACHE_FOLDER)

- script: npm ci --cache $(NPM_CACHE_FOLDER)
  displayName: 'Install npm dependencies'

...
like image 70
Max Ivanov Avatar answered Oct 20 '22 00:10

Max Ivanov


You can log into your Windows Server 2017 server and check if the folder $(Pipeline.Workspace)/.npm is created and the dependencies are stored inside.

I copied and tested your yaml. It worked both on local agent(win2019) and cloud agents. You can try to run your pipeline on the cloud agents or other agents with newer system to check if it is the agent that cause this error.

like image 22
Levi Lu-MSFT Avatar answered Oct 19 '22 23:10

Levi Lu-MSFT


The keys generated with your package-lock.json differ between the two tasks. It happens when the file is modified. Here, they're modified by your npm install task.

You can use the restoreKeys option when configuring the Cache task to fall back onto the latest cache entry. And I think you don't need the 'npm install' task.

Could you try replacing this :

- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    path: $(npm_config_cache)
    cacheHitVar: NPM_CACHE_RESTORED

- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
  condition: ne(variables.NPM_CACHE_RESTORED, 'true')

By this definition :

- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    restoreKeys: |
       npm | "$(Agent.OS)"
       npm
    path: $(npm_config_cache)
  displayName: Cache npm

- script: npm ci --cache $(npm_config_cache)
like image 1
FLabranche Avatar answered Oct 19 '22 23:10

FLabranche


Yesterday, I was able to get it working with no issue at all on a self-hosted machine agent by using this:

- task: Cache@2
  inputs:
    key: '**/package-lock.json, !**/node_modules/**/package-lock.json, !**/.*/**/package-lock.json'
    path: '$(System.DefaultWorkingDirectory)/node_modules'
  displayName: 'Cache Node Modules'

Today, trying to work on a hosted agent today and this doesn't cut it at all. Aggh, Back to the grinding board. Anyhow, maybe could work for you on your self-hosted pipeline

like image 1
Briana Finney Avatar answered Oct 19 '22 22:10

Briana Finney