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.

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 ""MSB4018: ... task failed unexpectedly""?
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 "MSB4018: ... task failed unexpectedly"?
Read the exception and fix the input/environment

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card