Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
barrett777 opened this issue Jan 23, 2020 · 26 comments · Fixed by #2221
Labels
Milestone

Comments

@barrett777
Copy link

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]```
@SeppPenner
Copy link

I can confirm that. I have the same issue now...

@SeppPenner
Copy link

@barrett777 Thanks for the link to the workaround :)

@thoemmi
Copy link
Contributor

thoemmi commented Mar 17, 2020

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)

@arturcic
Copy link
Member

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

@thoemmi
Copy link
Contributor

thoemmi commented Mar 17, 2020

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

@arturcic
Copy link
Member

Have you upgraded the sdk on the CI to 3.1.200?

@thoemmi
Copy link
Contributor

thoemmi commented Mar 17, 2020

Yes, I have updated the VS Build Tools to 16.5 on our build machine an hour ago.

Our CI Build includes a build step to use .NET Core SDK 3.1.x, so it already used the latest SDK before my update 🙄

@arturcic
Copy link
Member

Then either you downgrade the build tools to 16.4 and used sdk 3.1.102 or just wait for a fix, but it could take some time to fix it.

@thoemmi
Copy link
Contributor

thoemmi commented Mar 17, 2020

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 😅

@arturcic
Copy link
Member

That's great

@rainersigwald
Copy link
Contributor

This is likely a result of MSBuild changes to improve task isolation (dotnet/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.

@arturcic
Copy link
Member

@rainersigwald setting MSBUILDSINGLELOADCONTEXT=1 helped, at least I can confirm the integration tests that test the GitVersionTask passes.

Is there a way we can enable this without setting this env variable? I'd prefer someone using the package not to set the env variable prior to build

@bonesoul
Copy link

@arturcic this may be a possible fix;

SpecFlowOSS/SpecFlow#1912 (comment)

@arturcic
Copy link
Member

thanks for pointing out, I'll check if it can be applied for us as well

@rainersigwald
Copy link
Contributor

The SpecFlow fix doesn't seem to be relevant to GitVersion. I've been debugging it but haven't figured out what's up yet. I was able to break past the first problem but immediately found other problems.

The reason this is happening is that the tasks don't do work themselves, but load an implementation via reflection and invoke that. When the default AssemblyLoadContext isn't configured to find task assemblies, this fails to load.

Is there someone that can walk me through the intention of this code?

@rainersigwald
Copy link
Contributor

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

@rainersigwald
Copy link
Contributor

I think #2221 should do the trick. Comments welcome.

@tkrafael
Copy link

Is it possible to ilmerge all dependent assemblies and avoid assembly load context?

@github-actions
Copy link

github-actions bot commented May 4, 2020

🎉 This issue has been resolved in version 5.3.0 🎉
The release is available on:

Your GitReleaseManager bot 📦🚀

@tkrafael
Copy link

tkrafael commented May 4, 2020

Thanks guys, you're awesome!

@bonesoul
Copy link

bonesoul commented May 4, 2020

great!

@SeppPenner
Copy link

Can confirm that it's fixed 👍

SacredSkull added a commit to SacredSkull/dotnet-tidal-usdk that referenced this issue May 7, 2020
oelderinkX pushed a commit to oelderinkX/FileCache that referenced this issue Sep 2, 2020
Update GitVersionTask to 5.3.7 because of issue with dotnet build GitTools/GitVersion#2063
@jmevel
Copy link

jmevel commented Oct 16, 2020

I'm referencing the Nuget package version 5.3.7 and this issue still remains on my side. Building with Visual Studio works but building with dotnet build fails for some unknown reasons.

C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018: Échec inattendu de la tâche "WriteVersionInfoToBuildLog". [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018: System.TypeInitializationException: The type initializer for 'GitVersionTask.MsBuild.TaskProxy' threw an exception. [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:  ---> System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Le fichier spécifié est introuvable. [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018: File name: 'GitVersionTask.MsBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:  ---> System.IO.FileNotFoundException: Could not load file or assembly 'GitVersionTask.MsBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Le fichier spécifié est introuvable. [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018: File name: 'GitVersionTask.MsBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersionTask.MsBuild.LibGit2Sharp.GitLoaderContext.Load(AssemblyName assemblyName) in D:\a\1\s\src\GitVersionTask.MsBuild\LibGit2Sharp\GitLoaderContext.cs:line 30 [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\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) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at System.Reflection.Assembly.GetType(String name, Boolean throwOnError) [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersionTask.MsBuild.TaskProxy..cctor() in D:\a\1\s\src\GitVersionTask.MsBuild\TaskProxy.cs:line 22 [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    --- End of inner exception stack trace --- [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at GitVersionTask.MsBuild.Tasks.WriteVersionInfoToBuildLog.Execute() in D:\a\1\s\src\GitVersionTask.MsBuild\Tasks\WriteVersionInfoToBuildLog.cs:line 5 [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\dev\Git\Project\src\Project\Project.csproj]
C:\Users\mevelje\.nuget\packages\gitversiontask\5.0.1\build\GitVersionTask.targets(10,9): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\dev\Git\Project\src\Project\Project.csproj]

@SeppPenner
Copy link

SeppPenner commented Oct 20, 2020

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

grafik

@jmevel
Copy link

jmevel commented Oct 20, 2020

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants