Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm cache does not speed up npm install #385

Closed
ugultopu opened this issue Dec 21, 2021 · 4 comments
Closed

npm cache does not speed up npm install #385

ugultopu opened this issue Dec 21, 2021 · 4 comments
Labels
wontfix This will not be worked on

Comments

@ugultopu
Copy link

Description:
Consider the following builds:

These two repositories are almost identical, with the only difference being the latter repository caches npm via the setup-node GitHub Action, whereas the former one does not. In other words, the only difference between the repositories is at the .github/workflows/main file:

name: Build Pipeline
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '11'
          # Following line is present only on the latter repository
          cache: 'npm'
      - run: npm install
      - run: npm run build

Although the build at "setup-node-with-cache" successfully uses npm cache (as evident by the output of the Run actions/setup-node@v2 step), run time of Run npm install step is almost the same as the corresponding step of the build at "setup-node-without-cache".

Isn’t the run time of Run npm install step of the build at "setup-node-with-cache" supposed to be significantly shorter than the corresponding step of the build at "setup-node-without-cache", since it is supposed to use the cached npm packages? Am I missing something here?

Action version:
2

Platform:
Ubuntu

Runner type:
Hosted

Tools version:
Node 11 and default npm that comes with it.

Repro steps:

Expected behavior:
The npm install step of the build that uses the cached npm to be quicker.

Actual behavior:
The npm install step of the build that uses the cached npm is (virtually) the same as the non-cached version.

@ugultopu ugultopu added bug Something isn't working needs triage labels Dec 21, 2021
@ugultopu
Copy link
Author

Another question is, what is the point of including package-lock.json in the cache key? That is, as far as I can tell, if package-lock.json changes, the cache will be invalid and hence, all packages will be fetched over the network from scratch.

However, I would say this is pointless, since if package-lock.json changes between builds, most likely only one or two new packages are added or removed, compared to the previous build. In other words, most of the packages from the previous build are still being used, even though package-lock.json is changed. Hence, the cache would actually still be useful even if package-lock.json is changed. However, since the cache will be invalidated (since it depends on package-lock.json), we won't be able to benefit from it.

So, what's the point of including package-lock.json in the cache key?

@dmitry-shibanov dmitry-shibanov added wontfix This will not be worked on and removed needs triage bug Something isn't working labels Dec 22, 2021
@dmitry-shibanov
Copy link
Contributor

Hello @ugultopu. Thank you for your report. The action setup-node caches saves cache from the global cache directory. The benefit from the caching comes for medium and large size dependencies. Besides, some dependencies are built in runtime, that is why it takes more time.

We use a package-lock.json file, because it contains the exact versions of all dependencies. If some dependencies change the cache can be outdated, that is why we use package-lock.json to track it. Moreover, we use package-lock.json file because it is of the mostly used package's tree dependency file. You can override this behaviour with cache-dependency-path. We have a feature request with adding restore keys to the action.

For now I'm going to close the issue, because it's an expected behaviour. If you have any concerns feel free to ping us.

@sham97
Copy link

sham97 commented Nov 29, 2022

Hi @dmitry-shibanov ,
i used below file

  • name: Get npm cache directory
    id: npm-cache-dir
    run: |
    echo "::set-output name=dir::$(npm config get cache)"
    • uses: actions/cache@v3
      id: npm-cache # use this to check for cache-hit ==> if: steps.npm-cache.outputs.cache-hit != 'true'
      with:
      path: ${{ steps.npm-cache-dir.outputs.dir }}
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
      ${{ runner.os }}-node-

    • name: install dependencies
      run: npm install

    • name: build app
      run: npm run build --if-present
      env:
      CI: ''

this file however cache has been stored in action cache but after i run pipeline it is install dependencies from scratch it gets 1 minute and doesnt use stored cache.

@sham97
Copy link

sham97 commented Nov 29, 2022

please can you explain how we can use stored cache for future runs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants