# MSBuild "MSB3027: could not copy ... file is locked" in CI

> Fix MSBuild "MSB3027: Could not copy X to Y. Exceeded retry count. The file is locked by: another process" in CI - a transient file lock during the copy step.

Source: https://latchkey.dev/learn/dotnet/dotnet-msb3027-could-not-copy-file-locked-in-ci  
Updated: 2026-06-26

MSB3027 means MSBuild tried to copy an output file but the destination (or source) was held open by another process and the copy exhausted its retries. On a busy runner this is usually a transient lock from a lingering test host, antivirus scan, or a parallel build touching the same file.

## Diagnose it: SDK version and restore first

Most .NET CI failures are an SDK mismatch or a restore that did not happen. `global.json` pins the SDK, and if the runner does not have that exact version the failure message is about the project rather than the SDK.

```Terminal
dotnet --info
cat global.json 2>/dev/null

# restore explicitly so a restore failure is not reported as a build failure
dotnet restore --verbosity normal
dotnet build --no-restore -warnaserror
```

> Pin the SDK with `global.json` and install exactly that version with `actions/setup-dotnet`. A floating SDK turns a runner image update into a build break on unchanged code.

## FAQ

### What causes MSBuild "MSB3027: could not copy ... file is locked" in CI?

There are 2 common causes: a lingering process holds the output file and on-runner scanning or indexing locks the file. A previous test host, a still-running app instance, or a parallel build target keeps the DLL/EXE open, so the copy cannot replace it.

### How do I fix MSBuild "MSB3027: could not copy ... file is locked" in CI?

There are 2 fixes depending on which cause you have: kill lingering processes and serialize the copy and lean on auto-retry for transient locks. Work through them in order, since the first is the most common.

### What does MSBuild "MSB3027: could not copy ... file is locked" in CI actually mean?

The build fails with MSB3027 naming the file and the process holding the lock, after several copy retries.

### How do I stop MSBuild "MSB3027: could not copy ... file is locked" in CI happening again?

Do not run multiple jobs against the same output path on one runner. The prevention section lists 3 changes that keep it from recurring.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
