mlan
mlan

Reputation: 149

CircleCI error when attempting to restore/save cache

I'm configuring CircleCI to try and cache dependencies so I don't have to run yarn install on every single commit.

This is what my config.yml file looks like:

version: 2.1

jobs:
  build-and-test-frontend:
    docker:
      - image: circleci/node:14
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          working_directory: ./frontend/tests
          name: Run jest tests
          command: |
            yarn install --frozen-lockfile --cache-folder ~/.
            yarn test
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

workflows:
  sample:
    jobs:
      - build-and-test-frontend

But when either restore_cache or save_cache attempts to run, I get the following error:

error computing cache key: template: cacheKey:1:17: executing "cacheKey" at <checksum "yarn.lock">: error calling checksum: open /home/circleci/project/yarn.lock: no such file or directory

I'm brand new to using CircleCI so I'm not sure how to interpret this. What can I do to fix this?

EDIT:

This is the structure of my directory:

--project_root
 |
 |--frontend
    |-node_modules/
    |-public/
    |-src/
    |-tests/
    |-package.json
    |-yarn.lock

Upvotes: 0

Views: 2274

Answers (1)

FelicianoTech
FelicianoTech

Reputation: 4017

It's hard for me to give a great answer since I can't see your files in the repo but the config you have now suggest that the yarn.lock file you have is not in the root of the repo but rather in ./frontend/tests.

If that's where it is and that's where you want to keep it, then I'd suggest moving the working_dir key from the step level to the job level. This will then apply it to every step including the caching steps. Then they should find the file they are looking for.

Update:

Thanks for the repo tree. According to that you likely want to have your config like this:

version: 2.1

workflows:
  sample:
    jobs:
      - build-and-test-frontend

jobs:
  build-and-test-frontend:
    docker:
      - image: cimg/node:14.17
    working_directory: ./frontend
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Run jest tests
          command: |
            yarn install --frozen-lockfile --cache-folder ~/.
            yarn test
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

You'll notice a few things here:

  1. I moved workflows to that top. That's just a personal stylistic choice but I believe it helps keep your config readable as it grows.
  2. I moved working_directory to the job level instead of the step it was on.
  3. I set working_directory to the frontend directory. Most filepaths on CircleCI will be relative to the working_directory. Since that's where yarn.lock is, that's where I set it.
  4. I change the image from circleci/node:14 to cimg/node:14. The images in the circleci namespaces are deprecated. Going forward, you'll want to use the newer CircleCI images which are in the cimg namespace.

Upvotes: 1

Related Questions