|
| 1 | +# pnpm Lock File Merge Conflict Resolution |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +When multiple developers work on the same repository and modify dependencies in parallel, merging branches often results in conflicts in the `pnpm-lock.yaml` file. These conflicts can be tedious to resolve manually. |
| 6 | + |
| 7 | +## Solution |
| 8 | + |
| 9 | +This repository includes an **automated merge driver** for `pnpm-lock.yaml` that resolves conflicts automatically by regenerating the lockfile using `pnpm install`. |
| 10 | + |
| 11 | +## How It Works |
| 12 | + |
| 13 | +1. **`.gitattributes`**: Configures Git to use a custom merge driver for `pnpm-lock.yaml` |
| 14 | +2. **`scripts/pnpm-merge-driver.sh`**: The merge driver script that automatically runs `pnpm install --lockfile-only` to regenerate the lockfile when conflicts occur |
| 15 | +3. **Git configuration**: Local git config that points to the merge driver script |
| 16 | + |
| 17 | +## Setup Instructions |
| 18 | + |
| 19 | +### For Developers |
| 20 | + |
| 21 | +After cloning this repository, run the setup script once: |
| 22 | + |
| 23 | +```bash |
| 24 | +./scripts/setup-merge-driver.sh |
| 25 | +``` |
| 26 | + |
| 27 | +This will configure your local Git repository to use the automatic merge driver for `pnpm-lock.yaml`. |
| 28 | + |
| 29 | +### What Happens During a Merge? |
| 30 | + |
| 31 | +When you merge a branch that has conflicting changes in `pnpm-lock.yaml`: |
| 32 | + |
| 33 | +1. Git detects the conflict and invokes the custom merge driver |
| 34 | +2. The merge driver runs `pnpm install --lockfile-only` to regenerate the lockfile |
| 35 | +3. The newly generated lockfile incorporates dependencies from both branches |
| 36 | +4. The merge completes automatically without manual intervention |
| 37 | + |
| 38 | +### Example Workflow |
| 39 | + |
| 40 | +```bash |
| 41 | +# Start merging a branch |
| 42 | +git merge feature-branch |
| 43 | + |
| 44 | +# If there's a pnpm-lock.yaml conflict: |
| 45 | +# ✅ The merge driver automatically resolves it |
| 46 | +# ✅ pnpm install is run to regenerate the lockfile |
| 47 | +# ✅ The merge completes successfully |
| 48 | + |
| 49 | +# Verify the changes |
| 50 | +git diff --cached pnpm-lock.yaml |
| 51 | + |
| 52 | +# Commit the merge |
| 53 | +git commit |
| 54 | +``` |
| 55 | + |
| 56 | +### Important Notes |
| 57 | + |
| 58 | +⚠️ **Prerequisites:** |
| 59 | +- You must have `pnpm` installed and available in your PATH |
| 60 | +- The repository enforces `pnpm >= 10.0.0` (see `package.json` engines field) |
| 61 | + |
| 62 | +⚠️ **Package.json Conflicts:** |
| 63 | +- If there are conflicts in `package.json` files, you must resolve those manually **before** the merge driver can successfully regenerate `pnpm-lock.yaml` |
| 64 | +- The merge driver will fail if `pnpm install` fails due to invalid `package.json` files |
| 65 | + |
| 66 | +⚠️ **Review After Merge:** |
| 67 | +- Always review the regenerated `pnpm-lock.yaml` to ensure dependencies are correct |
| 68 | +- Run tests after merging to verify everything works as expected |
| 69 | + |
| 70 | +## Manual Resolution (Fallback) |
| 71 | + |
| 72 | +If the automatic merge driver fails or if you prefer manual resolution: |
| 73 | + |
| 74 | +1. Resolve any `package.json` conflicts first |
| 75 | +2. Accept either version of `pnpm-lock.yaml` (doesn't matter which) |
| 76 | +3. Run `pnpm install` to regenerate the lockfile |
| 77 | +4. Stage and commit the regenerated lockfile: |
| 78 | + |
| 79 | +```bash |
| 80 | +# After resolving package.json conflicts |
| 81 | +pnpm install |
| 82 | + |
| 83 | +# Stage the regenerated lockfile |
| 84 | +git add pnpm-lock.yaml |
| 85 | + |
| 86 | +# Complete the merge |
| 87 | +git commit |
| 88 | +``` |
| 89 | + |
| 90 | +## Version Consistency |
| 91 | + |
| 92 | +To prevent lockfile conflicts caused by different pnpm versions, this repository: |
| 93 | + |
| 94 | +- Specifies `packageManager: "pnpm@10.0.0"` in `package.json` |
| 95 | +- Requires `pnpm >= 10.0.0` in the `engines` field |
| 96 | +- Uses `pnpm@10` in GitHub Actions CI |
| 97 | + |
| 98 | +All developers should use the same major version of pnpm. Consider using [Corepack](https://nodejs.org/api/corepack.html) to automatically use the correct pnpm version: |
| 99 | + |
| 100 | +```bash |
| 101 | +corepack enable |
| 102 | +corepack prepare pnpm@10.0.0 --activate |
| 103 | +``` |
| 104 | + |
| 105 | +## Troubleshooting |
| 106 | + |
| 107 | +### "pnpm is not installed" |
| 108 | + |
| 109 | +Install pnpm globally: |
| 110 | + |
| 111 | +```bash |
| 112 | +npm install -g pnpm@10 |
| 113 | +``` |
| 114 | + |
| 115 | +Or use Corepack: |
| 116 | + |
| 117 | +```bash |
| 118 | +corepack enable |
| 119 | +corepack prepare pnpm@10.0.0 --activate |
| 120 | +``` |
| 121 | + |
| 122 | +### Merge driver not being invoked |
| 123 | + |
| 124 | +1. Verify the merge driver is configured: |
| 125 | + ```bash |
| 126 | + git config --local merge.pnpm-merge-driver.driver |
| 127 | + ``` |
| 128 | + |
| 129 | +2. Re-run the setup script: |
| 130 | + ```bash |
| 131 | + ./scripts/setup-merge-driver.sh |
| 132 | + ``` |
| 133 | + |
| 134 | +3. Check that `.gitattributes` exists and contains the merge driver configuration: |
| 135 | + ```bash |
| 136 | + cat .gitattributes |
| 137 | + ``` |
| 138 | + |
| 139 | +### Merge driver fails |
| 140 | + |
| 141 | +1. Check that you have no `package.json` conflicts remaining |
| 142 | +2. Try running `pnpm install` manually to see the error |
| 143 | +3. Resolve any dependency issues |
| 144 | +4. Continue the merge manually (see "Manual Resolution" above) |
| 145 | + |
| 146 | +## CI/CD Integration |
| 147 | + |
| 148 | +The GitHub Actions workflows in this repository already use pnpm@10 consistently. No additional CI configuration is needed. |
| 149 | + |
| 150 | +## References |
| 151 | + |
| 152 | +- [pnpm - Working with Git](https://pnpm.io/git) |
| 153 | +- [Git Attributes - Custom Merge Drivers](https://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver) |
| 154 | +- [Automating Lockfile Merge Conflicts](https://blog.aspect.build/easier-merges-on-lockfiles) |
0 commit comments