CircleCI配置Yarn依赖缓存时执行restore_cache/save_cache操作报错的解决咨询
Let's break down what's happening here and fix it step by step:
The Root Cause
The error message tells you CircleCI can't find /home/circleci/project/yarn.lock—and looking at your project structure, that makes total sense! Your yarn.lock (and package.json) live inside the frontend subdirectory, not the project root. When CircleCI runs checksum "yarn.lock", it's looking in the default working directory (/home/circleci/project) instead of frontend.
Fix 1: Update the Cache Key Path
First, adjust the checksum path in both restore_cache and save_cache to point to the correct location of yarn.lock:
- Replace
{{ checksum "yarn.lock" }}with{{ checksum "frontend/yarn.lock" }}
Fix 2: Correct the Yarn Install Working Directory
Your current run step sets working_directory: ./frontend/tests, but yarn install needs to run in the directory where package.json and yarn.lock are (the frontend folder). Running it from tests will cause yarn to look for a lockfile there, which doesn't exist.
Also, your --cache-folder flag has a typo (~/. instead of ~/.cache/yarn), which would point to the home directory instead of yarn's standard cache location.
Fixed Config.yml
Here's the revised configuration with all fixes applied:
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 "frontend/yarn.lock" }} # Updated path - run: working_directory: ./frontend # Changed to frontend root name: Install Dependencies and Run Tests command: | yarn install --frozen-lockfile --cache-folder ~/.cache/yarn # Fixed cache path yarn test # Runs test script from frontend/package.json - save_cache: name: Save Yarn Package Cache key: yarn-packages-{{ checksum "frontend/yarn.lock" }} # Updated path paths: - ~/.cache/yarn workflows: sample: jobs: - build-and-test-frontend
Why This Works
- The
checksumnow targets the correctfrontend/yarn.lockfile, so CircleCI can generate a valid cache key. - Running
yarn installfrom thefrontenddirectory ensures yarn uses the localpackage.jsonandyarn.lockto install dependencies. - The corrected
--cache-foldermatches the path insave_cache, so yarn will store and retrieve cached packages from the same location.
内容的提问来源于stack exchange,提问作者mlan

