Cargo "failed to write Cargo.lock" - Read-only Source in CI
Cargo needed to write or update Cargo.lock but the workspace is read-only or owned by another user. Resolution wants to record the lockfile and cannot, so the build fails before compiling.
What this error means
cargo fails with failed to write <path>/Cargo.lock and a Read-only file system (os error 30) or Permission denied cause. Common in containers that mount the source read-only or check it out as root.
error: failed to write `/src/Cargo.lock`
Caused by:
Read-only file system (os error 30)Common causes
Workspace mounted read-only
A container or CI step mounts the source tree read-only, so cargo cannot create or update Cargo.lock during resolution.
Source owned by another user
The checkout is owned by root (or a different user) while the build runs unprivileged, blocking the lockfile write.
How to fix it
Build with --locked so no write is needed
A committed, current lockfile plus --locked means cargo never rewrites it.
cargo build --locked # uses Cargo.lock as-is, no writeMake the workspace writable
Mount the source read-write, or fix ownership so cargo can write the lockfile.
sudo chown -R "$(id -u):$(id -g)" .
# or remount the source read-write in the containerHow to prevent it
- Commit
Cargo.lockand build with--lockedso no lockfile write occurs in CI. - Mount the source read-write when resolution may update the lockfile.
- Keep checkout ownership consistent with the build user.