Hi, I'm completely unable to run custom MsBuild Tasks targetting net5.0.
I created a small repository to show the issue: https://github.com/isc30/custom-msbuild-task
The only thing I'm doing is creating 1 project with the task (CustomTask) and running it when building the consumer (TaskConsumer).
This seems like a massive blocker when working with net5.0.
Detailed error:
Severity Code Description Project File Line Suppression State
Error MSB4018 The "TestTask" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at CustomTask.TestTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
TaskConsumer E:\projects\custom-msbuild-task\TaskConsumer\TaskConsumer.csproj 13
dotnet --list-sdks: 5.0.100 [C:\Program Files\dotnet\sdk]
We are experiencing a similar issue in our build environment.
Our build uses a number of custom tasks and some of them began failing last week after updating MSBuild tools 2019 to latest.
T:\w\a2f8e43c0c6189c\Build\Build.proj(19, 5): error MSB4018: The "MakeApplicationInstaller" task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileLoadException: Could not load file or assembly 'System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Server stack trace:
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
at System.Reflection.RuntimeMethodInfo.GetParameters()
at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Exception.GetObjectData(SerializationInfo info, StreamingContext context)
at System.TypeInitializationException.GetObjectData(SerializationInfo info, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage..ctor(IMethodReturnMessage mrm)
at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage.SmuggleIfPossible(IMessage msg)
at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Pleasant.Build.TaskFactories.IProjectTaskRunner.Execute(String projectFileContent, IEnumerable`1 properties, IEnumerable`1 itemGroups, ProjectTaskLogger logger)
at Pleasant.Build.TaskFactories.ProjectTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
This was working fine on just the previous version of MSBuild and the only other change seems to be the new reliance on .NET 5.
_Note: these tasks were targetted at .NET 4.6 and depended on Microsoft.Build.* packages v15.7.179_
I did some checking with ILSpy.
MSBuild.exe v16.8.2.56705 (distributed with VS 2019 v16.8.2) has a direct dependency on System.Collections.Immutable v5.0.0.0
MSBuild.exe v16.7.0.37604 (for reference) had no direct dependency on System.Collections.Immutable at all.
There was an indirect reference via Microsoft.Build, but it was v1.2.3.0
So it looks like the newest MSBuild has taken some new dependencies and caused breaking dependency changes.
For what it's worth, I was able to bypass the problem by updating our custom build tasks project to depend explicitly on System.Collections.Immutable v5.0.0, which added a bunch of extra dependencies. And now I have to get multiple teams to make updates to their projects to fix their broken builds.
None of this should have been necessary :-(
Sadly your case doesn't seem to be the same as mine, @PleasantD
With assembly binding logs enabled, this is what im getting:
Severity Code Description Project File Line Suppression State
Error MSB4018 The "TestTask" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at CustomTask.TestTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/MSBuild/Current/Bin/
LOG: Initial PrivatePath = NULL
Calling assembly : CustomTask, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\MSBuild.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/MSBuild/Current/Bin/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/MSBuild/Current/Bin/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/MSBuild/Current/Bin/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/MSBuild/Current/Bin/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///E:/Projects/custom-msbuild-task/CustomTask/bin/Debug/net5.0/System.Runtime.DLL.
LOG: Attempting download of new URL file:///E:/Projects/custom-msbuild-task/CustomTask/bin/Debug/net5.0/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///E:/Projects/custom-msbuild-task/CustomTask/bin/Debug/net5.0/System.Runtime.EXE.
LOG: Attempting download of new URL file:///E:/Projects/custom-msbuild-task/CustomTask/bin/Debug/net5.0/System.Runtime/System.Runtime.EXE.
TaskConsumer E:\Projects\custom-msbuild-task\TaskConsumer\TaskConsumer.csproj 13
@PleasantD Please file a new bug with a repro case for your Immutable issues. MSBuild 16.8.2 has a binding redirect that should push all references to the 5.0.0 version and updating tasks to the higher reference should not be necessary.
@isc30 I do not reproduce your problem:
โฏ dotnet build .\TaskConsumer\
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored S:\repro\dotnet\msbuild\issues\5904\TaskConsumer\TaskConsumer.csproj (in 61 ms).
TaskConsumer -> S:\repro\dotnet\msbuild\issues\5904\TaskConsumer\bin\Debug\net5.0\TaskConsumer.dll
Hello from the Custom MsBuild Task!
Are you trying to use the .NET 5.0 task in Visual Studio or MSBuild.exe, rather than in dotnet msbuild? That is not expected to work.
Wow, just tried with dotnet build and it works.
I guess it's an issue when building from Visual Studio then.
It fails on both the latest stable (2019) and preview.
Are you trying to use the .NET 5.0 task in Visual Studio or MSBuild.exe, rather than in dotnet msbuild? That is not expected to work.
Does this mean that even tho .net5 is stable now, Visual Studio doesn't support it and it's never gonna support it?
There is no way to use a .NET 5.0 task from Visual Studio/MSBuild.exe, because those run on .NET Framework 4.7.2 or .NET Framework 4.8, which can't run .NET 5.0 assemblies.
Does this mean that even tho
.net5is stable now, Visual Studio doesn't support it and it's never gonna support it?
Not exactly. Visual Studio is a .NET Framework application and so is MSBuild.exe. Tasks are plugins to MSBuild and thus must be runnable in the environment of the main process. There are two different MSBuilds available: MSBuild.exe is distributed with Visual Studio and is compatible with old (.NET Framework) tasks, and dotnet msbuild comes with the .NET (Core) SDK and is compatible with new .NET (Core) tasks.
We can't make VS just use .NET 5.0 because it could break older tasks if they use .NET Framework functionality that doesn't work or has had a breaking change in .NET Core/5.0.
In the future we may teach MSBuild to support running a .NET 5.0+ task in another process, but haven't done so yet. That's tracked by #4834.
It seems like even if I change the CustomTask project to output a netstandard2.1 dll, it still blows up.
This case did work before updating to .NET5 SDK.
https://github.com/isc30/custom-msbuild-task/compare/netstandard2.1
Severity Code Description Project File Line Suppression State
Error MSB4062 The "CustomTask.TestTask" task could not be loaded from the assembly C:\Dev\custom-msbuild-task\TaskConsumer\..\CustomTask\bin\Debug\netstandard2.0\CustomTask.dll. Could not load file or assembly 'file:///C:\Dev\custom-msbuild-task\CustomTask\bin\Debug\netstandard2.0\CustomTask.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. TaskConsumer C:\Dev\custom-msbuild-task\TaskConsumer\TaskConsumer.csproj 13
@isc30 .NET Standard 2.1 is only supported by .NET Core 2.1 and higher, and not .NET Framework 4.7.2 or 4.8, so that's also expected. You'd need to use .NET Standard 2.0.
sorry, I meant netstandard2.0, the error is about it
https://github.com/isc30/custom-msbuild-task/compare/main...netstandard20
edit: after using netstandard2.0 and getting the same error, restarting VS did solve the issue. thanks a lot!
This sounds resolved? Let me know if I'm wrong, and I can reopen it.
Most helpful comment
@isc30 I do not reproduce your problem:
Are you trying to use the .NET 5.0 task in Visual Studio or MSBuild.exe, rather than in
dotnet msbuild? That is not expected to work.