Skip to content
Latchkey

Cargo "Permission denied" Writing target/ in CI

Cargo could not write to the target directory or ${CARGO_HOME}. The path is owned by another user (often root from an earlier container step) or mounted read-only, so cargo fails before it can compile.

What this error means

The build fails early with failed to create directory or Permission denied (os error 13) pointing at target/ or ~/.cargo. Frequently appears after a step ran as root and left root-owned files the unprivileged build user cannot overwrite.

cargo
error: failed to create directory `/home/runner/work/app/target/debug`

Caused by:
  Permission denied (os error 13)

Common causes

Target/CARGO_HOME owned by another user

A prior container or root step created target/ or ~/.cargo as root. The later build runs as an unprivileged user that cannot write into root-owned paths.

Read-only or wrong-permission mount

A cached or volume-mounted target dir restored with restrictive permissions (or mounted read-only) blocks cargo from writing object files.

How to fix it

Fix ownership or redirect the target dir

Reclaim the path, or point cargo at a writable location.

Terminal
# reclaim ownership
sudo chown -R "$(id -u):$(id -g)" target ~/.cargo
# or write somewhere you own
export CARGO_TARGET_DIR="${RUNNER_TEMP}/target"

Run cargo as a consistent user

Avoid mixing root and non-root steps that touch the same build dirs.

.github/workflows/ci.yml
# in a container step, run the build as the runner user, not root
- run: cargo build --locked
  # ensure earlier steps did not create target/ as root

How to prevent it

  • Keep all build steps under one user so target/ ownership stays consistent.
  • Point CARGO_TARGET_DIR/CARGO_HOME at writable, user-owned paths.
  • Do not cache root-owned build artifacts that a later unprivileged step must overwrite.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →