Edit

Share via


MSB4018

This article describes the MSB4018 error code.

Message text

MSB4018: The task failed unexpectedly.

Description

This error is emitted when a task fails with an unhandled exception. This issue is generally a sign of a bug in the task.

You might encounter this error when the build runs a task in an environment for which it wasn't prepared. For example, when a task has an x86 dependency, but the task runs in a 64-bit MSBuild environment. This scenario can manifest as a System.DllNotFoundException exception, as shown in the following output:

S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018: The "AnyCPUTaskWithPInvoke" task failed unexpectedly.
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018: System.DllNotFoundException: Unable to load DLL 'Native32BitLibrary.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.NativeMethod()
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.OtherMethod() in S:\BitnessInMSBuild\AnyCPUTaskWithPInvoke\AnyCPUTaskWithPInvoke.cs:line 19
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.Execute() in S:\BitnessInMSBuild\AnyCPUTaskWithPInvoke\AnyCPUTaskWithPInvoke.cs:line 12
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

Resolution

Ensure the task expresses a dependency on the runtime environment that it requires in the task's UsingTask element.

Applies to

All versions of MSBuild