Skip to content
Latchkey

MSBuild "MSB4018: The task failed unexpectedly" in CI

An MSBuild task threw an unhandled exception, so MSBuild reports MSB4018 with the task name and the exception. This is a task crash, not a compiler or resolution error - the stack trace in the log points at the cause.

What this error means

Build fails with MSB4018 naming a task (a built-in like Csc/ResolveAssemblyReference, or a custom/third-party task) followed by an exception and stack trace. It can be deterministic (a bug/bad input) or environment-specific (IO/permissions).

MSBuild output
error MSB4018: The "GenerateDepsFile" task failed unexpectedly.
System.UnauthorizedAccessException: Access to the path '/app/obj/...' is denied.

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

Common causes

A task hit an IO or permissions problem

A task crashed on a denied path, a locked file, or a full disk - common in containers where obj/bin permissions or read-only mounts differ from local.

A custom or third-party task threw

A custom target, source generator, or third-party MSBuild task has a bug or bad input that raises an unhandled exception.

How to fix it

Read the exception and fix the input/environment

  1. Read the exception type/message under the MSB4018 line - it names the real failure.
  2. For IO/permissions, ensure obj/bin are writable and the disk has space.
  3. For a custom/third-party task, update or fix the task and verify its inputs.

Capture a binlog for the full task context

A binlog shows the failing task’s inputs and the complete exception.

Terminal
dotnet build -bl:build.binlog
# open build.binlog in the MSBuild Structured Log Viewer

How to prevent it

  • Ensure build output directories are writable and have disk space in CI.
  • Pin and test custom/third-party MSBuild tasks and generators.
  • Capture a binlog as a CI artifact for hard-to-reproduce task crashes.

Frequently asked questions

What causes MSBuild "MSB4018: the task failed unexpectedly" in CI?
There are 2 common causes: a task hit an io or permissions problem and a custom or third-party task threw. A task crashed on a denied path, a locked file, or a full disk - common in containers where obj/bin permissions or read-only mounts differ from local.
How do I fix MSBuild "MSB4018: the task failed unexpectedly" in CI?
There are 2 fixes depending on which cause you have: read the exception and fix the input/environment and capture a binlog for the full task context. Work through them in order, since the first is the most common.
What does MSBuild "MSB4018: the task failed unexpectedly" in CI actually mean?
Build fails with MSB4018 naming a task (a built-in like Csc/ResolveAssemblyReference, or a custom/third-party task) followed by an exception and stack trace.
How do I stop MSBuild "MSB4018: the task failed unexpectedly" in CI happening again?
Ensure build output directories are writable and have disk space in CI. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card