Skip to content

Commit

Permalink
Don't load MSBuild.Framework in GitLoaderContext
Browse files Browse the repository at this point in the history
Unit tests were failing because they load MSBuild assemblies from the
test output directory. Normally they're not available there, so
GitLoaderContext doesn't load them. But in tests, they were there, and
being loaded in both ALC.Default (from the main part of the test) and
a GitLoaderContext created a type mismatch

```
MissingMethodException: Method not found: 'Microsoft.Build.Framework.ITaskItem[] GitVersion.MSBuildTask.Tasks.UpdateAssemblyInfo.get_CompileFiles()'.
   at GitVersion.MSBuildTask.GitVersionTaskExecutor.UpdateAssemblyInfo(UpdateAssemblyInfo task)
   at GitVersion.MSBuildTask.GitVersionTasks.<>c__DisplayClass1_0.<UpdateAssemblyInfo>b__0(IGitVersionTaskExecutor executor) in D:\a\1\s\src\GitVersionTask\GitVersionTasks.cs:line 15
   at GitVersion.MSBuildTask.GitVersionTasks.ExecuteGitVersionTask[T](T task, Action`1 action) in D:\a\1\s\src\GitVersionTask\GitVersionTasks.cs:line 30
```

That was happening because the loaded `UpdateAssemblyInfo.CompileFiles`
was of type (`Microsoft.Build.Framework.ITaskItem[]` in the
`GitLoaderContext`) while it was looking for
(`Microsoft.Build.Framework.ITaskItem[]` in
`AssemblyLoadContext.Default`).
  • Loading branch information
rainersigwald committed Apr 6, 2020
1 parent de68537 commit 12bed98
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/GitVersionTask.MsBuild/LibGit2Sharp/GitLoaderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ public sealed class GitLoaderContext : AssemblyLoadContext

protected override Assembly Load(AssemblyName assemblyName)
{
if (assemblyName.Name == entryPointAssembly.GetName().Name)
string simpleName = assemblyName.Name;

if (simpleName == entryPointAssembly.GetName().Name)
{
return entryPointAssembly;
}

var path = Path.Combine(Path.GetDirectoryName(typeof(GitLoaderContext).Assembly.Location), assemblyName.Name + ".dll");
if (simpleName == "Microsoft.Build.Framework")
{
// Delegate loading MSBuild types up to an ALC that should already have them
// once we've gotten this far
return null;
}

var path = Path.Combine(Path.GetDirectoryName(typeof(GitLoaderContext).Assembly.Location), simpleName + ".dll");

if (File.Exists(path))
{
Expand Down

0 comments on commit 12bed98

Please sign in to comment.