Skip to content

Commit 3411f13

Browse files
authored
Merge pull request #218 from AArnott/fix217
Fixes failures on non-Windows platforms
2 parents f919d71 + 0da2e40 commit 3411f13

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/MSBuildExtensionTask/ContextAwareTask.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212

1313
public abstract class ContextAwareTask : Task
1414
{
15+
#if NETCOREAPP2_0
16+
/// <summary>
17+
/// Our custom <see cref="AssemblyLoadContext"/> that we create to host our Task.
18+
/// </summary>
19+
/// <remarks>
20+
/// We only create *one* of these, and reuse it for subsequent task invocations,
21+
/// because creating multiple of them hit https://github.com/dotnet/coreclr/issues/19654
22+
/// </remarks>
23+
private static CustomAssemblyLoader Loader;
24+
#endif
25+
1526
protected virtual string ManagedDllDirectory => Path.GetDirectoryName(new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath);
1627

1728
protected virtual string UnmanagedDllDirectory => null;
@@ -20,8 +31,13 @@ public override bool Execute()
2031
{
2132
#if NETCOREAPP2_0
2233
string taskAssemblyPath = new Uri(this.GetType().GetTypeInfo().Assembly.CodeBase).LocalPath;
23-
var ctxt = new CustomAssemblyLoader(this);
24-
Assembly inContextAssembly = ctxt.LoadFromAssemblyPath(taskAssemblyPath);
34+
if (Loader == null)
35+
{
36+
Loader = new CustomAssemblyLoader();
37+
}
38+
39+
Loader.LoaderTask = this;
40+
Assembly inContextAssembly = Loader.LoadFromAssemblyPath(taskAssemblyPath);
2541
Type innerTaskType = inContextAssembly.GetType(this.GetType().FullName);
2642
object innerTask = Activator.CreateInstance(innerTaskType);
2743

@@ -71,31 +87,41 @@ public override bool Execute()
7187
#if NETCOREAPP2_0
7288
private class CustomAssemblyLoader : AssemblyLoadContext
7389
{
74-
private readonly ContextAwareTask loaderTask;
90+
private ContextAwareTask loaderTask;
7591

76-
internal CustomAssemblyLoader(ContextAwareTask loaderTask)
92+
internal CustomAssemblyLoader()
7793
{
78-
this.loaderTask = loaderTask;
94+
}
95+
96+
internal ContextAwareTask LoaderTask
97+
{
98+
get => this.loaderTask;
99+
set => this.loaderTask = value;
79100
}
80101

81102
protected override Assembly Load(AssemblyName assemblyName)
82103
{
83-
// Always load libgit2sharp in the default context.
84-
// Something about the p/invoke done in that library with its custom marshaler
85-
// doesn't sit well with Core CLR 2.x.
86-
// See https://github.com/AArnott/Nerdbank.GitVersioning/issues/215 and https://github.com/dotnet/coreclr/issues/19654
87-
AssemblyLoadContext preferredContext = assemblyName.Name.Equals("libgit2sharp", StringComparison.OrdinalIgnoreCase) ? Default : this;
104+
if (this.loaderTask == null)
105+
{
106+
throw new InvalidOperationException(nameof(this.loaderTask) + " must be set first.");
107+
}
108+
88109
string assemblyPath = Path.Combine(this.loaderTask.ManagedDllDirectory, assemblyName.Name) + ".dll";
89110
if (File.Exists(assemblyPath))
90111
{
91-
return preferredContext.LoadFromAssemblyPath(assemblyPath);
112+
return this.LoadFromAssemblyPath(assemblyPath);
92113
}
93114

94115
return Default.LoadFromAssemblyName(assemblyName);
95116
}
96117

97118
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
98119
{
120+
if (this.loaderTask == null)
121+
{
122+
throw new InvalidOperationException(nameof(this.loaderTask) + " must be set first.");
123+
}
124+
99125
string unmanagedDllPath = Directory.EnumerateFiles(
100126
this.loaderTask.UnmanagedDllDirectory,
101127
$"{unmanagedDllName}.*").Concat(

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Globalization;
66
using System.IO;
77
using System.Linq;
8+
using System.Reflection;
89
using Microsoft.Build.Framework;
910
using Microsoft.Build.Utilities;
1011
using MSBuildExtensionTask;
@@ -169,7 +170,7 @@ public GetBuildVersion()
169170
[Output]
170171
public ITaskItem[] CloudBuildVersionVars { get; private set; }
171172

172-
protected override string UnmanagedDllDirectory => GitExtensions.FindLibGit2NativeBinaries(Path.Combine(this.TargetsPath, "MSBuildFull"));
173+
protected override string UnmanagedDllDirectory => GitExtensions.FindLibGit2NativeBinaries(this.TargetsPath);
173174

174175
protected override bool ExecuteInner()
175176
{
@@ -179,7 +180,7 @@ protected override bool ExecuteInner()
179180
{
180181
Requires.Argument(!Path.IsPathRooted(this.ProjectPathRelativeToGitRepoRoot), nameof(this.ProjectPathRelativeToGitRepoRoot), "Path must be relative.");
181182
Requires.Argument(!(
182-
this.ProjectPathRelativeToGitRepoRoot.Contains(".." + Path.DirectorySeparatorChar) ||
183+
this.ProjectPathRelativeToGitRepoRoot.Contains(".." + Path.DirectorySeparatorChar) ||
183184
this.ProjectPathRelativeToGitRepoRoot.Contains(".." + Path.AltDirectorySeparatorChar)),
184185
nameof(this.ProjectPathRelativeToGitRepoRoot),
185186
"Path must not use ..\\");

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@
2222
<file src="$BaseOutputPath$net461\Nerdbank.GitVersioning.Tasks.dll" target="build\MSBuildFull\Nerdbank.GitVersioning.Tasks.dll" />
2323
<file src="$BaseOutputPath$net461\Newtonsoft.Json.dll" target="build\MSBuildFull\Newtonsoft.Json.dll" />
2424
<file src="$BaseOutputPath$net461\Validation.dll" target="build\MSBuildFull\Validation.dll" />
25-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.dll" target="build\MSBuildFull\lib\win32\x64\git2-6311e88.dll" />
26-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.dll" target="build\MSBuildCore\lib\win32\x64\git2-6311e88.dll" />
27-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.pdb" target="build\MSBuildFull\lib\win32\x64\git2-6311e88.pdb" />
28-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.pdb" target="build\MSBuildCore\lib\win32\x64\git2-6311e88.pdb" />
29-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.dll" target="build\MSBuildFull\lib\win32\x86\git2-6311e88.dll" />
30-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.dll" target="build\MSBuildCore\lib\win32\x86\git2-6311e88.dll" />
31-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.pdb" target="build\MSBuildFull\lib\win32\x86\git2-6311e88.pdb" />
32-
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.pdb" target="build\MSBuildCore\lib\win32\x86\git2-6311e88.pdb" />
33-
<file src="$LibGit2SharpNativeBinaries$runtimes\osx\native\libgit2-6311e88.dylib" target="build\MSBuildFull\lib\osx\libgit2-6311e88.dylib" />
34-
<file src="$LibGit2SharpNativeBinaries$runtimes\osx\native\libgit2-6311e88.dylib" target="build\MSBuildCore\lib\osx\libgit2-6311e88.dylib" />
35-
<file src="$LibGit2SharpNativeBinaries$runtimes\linux-x64\native\libgit2-6311e88.so" target="build\MSBuildFull\lib\linux\x86_64\libgit2-6311e88.so" />
36-
<file src="$LibGit2SharpNativeBinaries$runtimes\linux-x64\native\libgit2-6311e88.so" target="build\MSBuildCore\lib\linux\x86_64\libgit2-6311e88.so" />
25+
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.dll" target="build\lib\win32\x64\git2-6311e88.dll" />
26+
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x64\native\git2-6311e88.pdb" target="build\lib\win32\x64\git2-6311e88.pdb" />
27+
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.dll" target="build\lib\win32\x86\git2-6311e88.dll" />
28+
<file src="$LibGit2SharpNativeBinaries$runtimes\win7-x86\native\git2-6311e88.pdb" target="build\lib\win32\x86\git2-6311e88.pdb" />
29+
<file src="$LibGit2SharpNativeBinaries$runtimes\osx\native\libgit2-6311e88.dylib" target="build\lib\osx\libgit2-6311e88.dylib" />
30+
<file src="$LibGit2SharpNativeBinaries$runtimes\linux-x64\native\libgit2-6311e88.so" target="build\lib\linux\x86_64\libgit2-6311e88.so" />
3731
<file src="$LibGit2SharpNativeBinaries$libgit2\LibGit2Sharp.dll.config" target="build\MSBuildCore\LibGit2Sharp.dll.config" />
3832
<file src="$BaseOutputPath$netcoreapp2.0\LibGit2Sharp.dll" target="build\MSBuildCore\LibGit2Sharp.dll" />
3933
<file src="$BaseOutputPath$netcoreapp2.0\MSBuildExtensionTask.dll" target="build\MSBuildCore\MSBuildExtensionTask.dll" />

0 commit comments

Comments
 (0)