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).
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
- Read the exception type/message under the MSB4018 line - it names the real failure.
- For IO/permissions, ensure
obj/binare writable and the disk has space. - 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.
dotnet build -bl:build.binlog
# open build.binlog in the MSBuild Structured Log ViewerHow 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.