GitVersion: the combination of VS 2019 Version 16.5.0 Preview 2.0 and .NET Core 3.1.200-preview-014883 fails dotnet build

If you install VS 2019 Version 16.5.0 Preview 2.0 (Released 1/22/2020), dotnet build fails with this error. Here’s a related StackOverflow question/answer on how to work around.

https://stackoverflow.com/a/59876257/1603663

I don’t know if this issue is a bug in .net core, or an update required in GitVersion

C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018: System.TypeInitializationException: The type initializer for 'GitVersion.MSBuildTask.TaskProxy' threw an exception. [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  ---> System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=5.1.3.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018: File name: 'GitVersionTask.MsBuild, Version=5.1.3.0, Culture=neutral, PublicKeyToken=null' [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  ---> System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=5.1.3.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018: File name: 'GitVersionTask.MsBuild, Version=5.1.3.0, Culture=neutral, PublicKeyToken=null' [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersion.MSBuildTask.LibGit2Sharp.GitLoaderContext.Load(AssemblyName assemblyName) in D:\a\1\s\src\GitVersionTask.MsBuild\LibGit2Sharp\GitLoaderContext.cs:line 30 [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.GetType(QCallAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive, ObjectHandleOnStack assemblyLoadContext) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.Assembly.GetType(String name, Boolean throwOnError) [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersion.MSBuildTask.TaskProxy..cctor() in D:\a\1\s\src\GitVersionTask.MsBuild\TaskProxy.cs:line 22 [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:  [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    --- End of inner exception stack trace --- [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersion.MSBuildTask.Tasks.WriteVersionInfoToBuildLog.Execute() in D:\a\1\s\src\GitVersionTask.MsBuild\Tasks\WriteVersionInfoToBuildLog.cs:line 5 [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [ProjectFile]
C:\Users\benb\.nuget\packages\gitversiontask\5.1.3\build\GitVersionTask.targets(10,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [ProjectFile]```

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 26 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Notes on what I’ve figured out so far. I don’t see a super clear path forward at the moment, will continue to look on Monday:

TaskProxy has an explicit reference to the stub implementations of the tasks in GitVersionTask.MsBuild

TaskProxy tries to load the assembly GitVersionTask in its private GitLoaderContext ALC

GitVersionTask depends on GitVersionTask.MsBuild, and the type GitVersion.MSBuildTask.GitVersionTasks has explicit references back to it, so when it’s not available in the private ALC it fails with the original error

Could not load file or assembly 'GitVersionTask.MsBuild, Version=5.1.3.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

at

var type = LibGit2SharpLoader.Instance.Assembly.GetType("GitVersion.MSBuildTask.GitVersionTasks", throwOnError: true).GetTypeInfo();

If you amend the private ALC to also load GitVersionTask.MsBuild, we get past that, but the immediately following line

GetVersion = GetMethod<GetVersion>(type, nameof(GetVersion));
...

private static Func<T, bool> GetMethod<T>(TypeInfo type, string name) => (Func<T, bool>)type.GetDeclaredMethod(name).CreateDelegate(typeof(Func<T, bool>));

fails, because the GetVersion in the type parameter is in the task ALC, while the GetVersion returned from reflection is in the GitLoaderContext ALC, so they don’t match and you get

System.ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type.

In the old MSBuild, there was no task ALC, only the default ALC. That meant that

  1. GitVersionTask.MsBuild is loaded in the default ALC
  2. The GitVersionTask assembly is loaded in the private GitLoaderContext ALC
  3. When loading the type GitVersion.MSBuildTask.GitVersionTasks, GitVersionTask.MsBuild is not found
  4. so it falls back to the default ALC, where it’s already loaded and returned.
  5. The cast then succeeds because it’s the same type in the same ALC

I noticed that as well. I’ll need to find a fix for this

I experience the same issue with the released 3.1.200 and GitVersionTask 5.2.4. All our CI builds fail now. Unfortunately, even a global.json with 3.1.101 does not help.

##[error]C:\Users\TeamCityTFSAgent\.nuget\packages\gitversiontask\5.2.4\build\GitVersionTask.targets(10,9): Error MSB4018: Unerwarteter Fehler bei der WriteVersionInfoToBuildLog-Aufgabe.
System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=5.2.4.0, Culture=neutral, PublicKeyToken=null'. Das System kann die angegebene Datei nicht finden.
File name: 'GitVersionTask.MsBuild, Version=5.2.4.0, Culture=neutral, PublicKeyToken=null'
 ---> System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=5.2.4.0, Culture=neutral, PublicKeyToken=null'. Das System kann die angegebene Datei nicht finden.
File name: 'GitVersionTask.MsBuild, Version=5.2.4.0, Culture=neutral, PublicKeyToken=null'
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
   at GitVersion.MSBuildTask.LibGit2Sharp.GitLoaderContext.Load(AssemblyName assemblyName) in D:\a\GitVersion\GitVersion\src\GitVersionTask.MsBuild\LibGit2Sharp\GitLoaderContext.cs:line 29
   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
   at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)


   at GitVersion.MSBuildTask.GitVersionTaskBase.Execute() in D:\a\GitVersion\GitVersion\src\GitVersionTask.MsBuild\GitVersionTaskBase.cs:line 33
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)

Can confirm that it’s fixed 👍

This is likely a result of MSBuild changes to improve task isolation (microsoft/msbuild#4916). However, it looks like GitVersion has its own GitLoaderContext, which should have insulated you from that change.

If you’re affected by this, can you try setting the environment variable MSBUILDSINGLELOADCONTEXT=1 before your build and reporting whether that helps. If it does, it’s a lighter-weight workaround than downgrading to SDK 3.1.1xx.

I’ve uninstalled 16.5 and installed 16.4.5 (for reference, you can get specific versions here). However, this didn’t help, as each build agent has its own copy of dotnet + SDK, therefore I had to remove _tools/dotnet folder of each agent manually. Afterwards all pipelines built successfully, even with GitVersionTask 5.2.4. I.e. the pressure is reduced 😅

@jmevel You seem to be using GitVersionTask 5.0.1, not 5.3.7, check your log.

grafik

Hum that’s correct, thanks for pointing me this mistake!

@jmevel You seem to be using GitVersionTask 5.0.1, not 5.3.7, check your log.

grafik

Neither pinning to 3.1.101 with global.json nor a downgrade of GitVersionTask to 5.1.2 does help 😕

@barrett777 Thanks for the link to the workaround 😃