|
| 1 | +# Lockfile Management |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document provides guidance on managing the pnpm lockfile (`pnpm-lock.yaml`) in this project, particularly in CI environments where lockfile compatibility issues can occur. |
| 6 | + |
| 7 | +## Understanding Lockfile Behavior |
| 8 | + |
| 9 | +### What is the Lockfile? |
| 10 | + |
| 11 | +The `pnpm-lock.yaml` file is a crucial part of dependency management that: |
| 12 | + |
| 13 | +- Records exact versions of all dependencies |
| 14 | +- Ensures consistent installations across different environments |
| 15 | +- Speeds up installation by providing a pre-computed dependency tree |
| 16 | + |
| 17 | +### Frozen vs. Non-Frozen Lockfile |
| 18 | + |
| 19 | +pnpm offers two main approaches to handling lockfiles during installation: |
| 20 | + |
| 21 | +1. **Frozen Lockfile** (`--frozen-lockfile` flag): |
| 22 | + - Ensures the lockfile isn't modified during installation |
| 23 | + - Fails if the lockfile is out of sync with package.json |
| 24 | + - Default behavior in CI environments |
| 25 | + |
| 26 | +2. **Non-Frozen Lockfile** (`--no-frozen-lockfile` flag): |
| 27 | + - Updates the lockfile if it's out of sync with package.json |
| 28 | + - More flexible but less strict about dependency consistency |
| 29 | + - Recommended for development environments |
| 30 | + |
| 31 | +## Common Lockfile Issues |
| 32 | + |
| 33 | +### ERR_PNPM_OUTDATED_LOCKFILE |
| 34 | + |
| 35 | +This error occurs when the lockfile doesn't match the dependencies specified in package.json. The error message looks like: |
| 36 | + |
| 37 | +``` |
| 38 | +ERR_PNPM_OUTDATED_LOCKFILE Cannot install with "frozen-lockfile" because pnpm-lock.yaml is not up to date with package.json |
| 39 | +``` |
| 40 | + |
| 41 | +Common causes: |
| 42 | +1. Adding/removing dependencies without updating the lockfile |
| 43 | +2. Different pnpm versions generating incompatible lockfiles |
| 44 | +3. Merge conflicts in the lockfile that weren't properly resolved |
| 45 | + |
| 46 | +## Best Practices |
| 47 | + |
| 48 | +### For Local Development |
| 49 | + |
| 50 | +1. **Always commit the lockfile**: The `pnpm-lock.yaml` file should always be committed to version control |
| 51 | +2. **Use the same pnpm version**: Ensure you're using the version specified in `packageManager` field in package.json |
| 52 | +3. **Update the lockfile when changing dependencies**: |
| 53 | + ```bash |
| 54 | + pnpm install |
| 55 | + ``` |
| 56 | +4. **Avoid manual edits**: Never edit the lockfile manually |
| 57 | + |
| 58 | +### For CI Environments |
| 59 | + |
| 60 | +1. **Use `--no-frozen-lockfile` in CI**: |
| 61 | + ```yaml |
| 62 | + # In GitHub Actions workflow |
| 63 | + - name: Install dependencies |
| 64 | + run: pnpm install --no-frozen-lockfile |
| 65 | + ``` |
| 66 | +
|
| 67 | +2. **Ensure complete repository history**: |
| 68 | + ```yaml |
| 69 | + # In GitHub Actions workflow |
| 70 | + - name: Checkout code |
| 71 | + uses: actions/checkout@v4 |
| 72 | + with: |
| 73 | + fetch-depth: 0 # Fetch all history for proper lockfile validation |
| 74 | + ``` |
| 75 | +
|
| 76 | +## Troubleshooting |
| 77 | +
|
| 78 | +If you encounter lockfile issues: |
| 79 | +
|
| 80 | +1. **Update your local environment**: |
| 81 | + ```bash |
| 82 | + # Enable Corepack (if using Node.js 20+) |
| 83 | + corepack enable |
| 84 | + |
| 85 | + # Prepare the correct pnpm version |
| 86 | + corepack prepare pnpm@8.15.4 --activate |
| 87 | + ``` |
| 88 | + |
| 89 | +2. **Regenerate the lockfile**: |
| 90 | + ```bash |
| 91 | + # Remove node_modules and lockfile |
| 92 | + rm -rf node_modules pnpm-lock.yaml |
| 93 | + |
| 94 | + # Reinstall dependencies |
| 95 | + pnpm install |
| 96 | + ``` |
| 97 | + |
| 98 | +3. **Commit the updated lockfile**: |
| 99 | + ```bash |
| 100 | + git add pnpm-lock.yaml |
| 101 | + git commit -m "Update lockfile" |
| 102 | + ``` |
| 103 | + |
| 104 | +## Additional Resources |
| 105 | + |
| 106 | +- [pnpm Documentation on Lockfile](https://pnpm.io/lockfile) |
| 107 | +- [CI Setup Documentation](./CI.md) |
| 108 | +- [Package Manager Documentation](./PACKAGE-MANAGER.md) |
0 commit comments