Skip to content

Commit e618156

Browse files
committed
Do not set default value for NativeLibararyPath on .NET Core
1 parent 53e1fea commit e618156

File tree

3 files changed

+30
-54
lines changed

3 files changed

+30
-54
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ static NativeMethods()
2929
{
3030
if (Platform.IsRunningOnNetFramework() || Platform.IsRunningOnNetCore())
3131
{
32-
string dllPath = Path.Combine(GlobalSettings.GetAndLockNativeLibraryPath(), libgit2 + Platform.GetNativeLibraryExtension());
33-
34-
// Try to load the .dll from the path explicitly.
35-
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
36-
// If it fails the next DllImport will load the library from safe directories.
37-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
38-
{
39-
LoadWindowsLibrary(dllPath);
40-
}
41-
else
32+
string nativeLibraryDir = GlobalSettings.GetAndLockNativeLibraryPath();
33+
if (nativeLibraryDir != null)
4234
{
43-
LoadUnixLibrary(dllPath, RTLD_NOW);
35+
string nativeLibraryPath = Path.Combine(nativeLibraryDir, libgit2 + Platform.GetNativeLibraryExtension());
36+
37+
// Try to load the .dll from the path explicitly.
38+
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
39+
// If it fails the next DllImport will load the library from safe directories.
40+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
41+
{
42+
LoadWindowsLibrary(nativeLibraryPath);
43+
}
44+
else
45+
{
46+
LoadUnixLibrary(nativeLibraryPath, RTLD_NOW);
47+
}
4448
}
4549
}
4650

LibGit2Sharp/Core/Platform.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ public static OperatingSystemType OperatingSystem
3737
}
3838
}
3939

40-
/// <summary>
41-
/// Determines the RID to use when loading libgit2 native library.
42-
/// This method only supports RIDs that are currently used by LibGit2Sharp.NativeBinaries.
43-
/// </summary>
44-
public static string GetNativeLibraryRuntimeId()
45-
{
46-
switch (OperatingSystem)
47-
{
48-
case OperatingSystemType.MacOSX:
49-
return "osx";
50-
51-
case OperatingSystemType.Unix:
52-
return "linux-" + ProcessorArchitecture;
53-
54-
case OperatingSystemType.Windows:
55-
return "win7-" + ProcessorArchitecture;
56-
}
57-
58-
throw new PlatformNotSupportedException();
59-
}
60-
6140
public static string GetNativeLibraryExtension()
6241
{
6342
switch (OperatingSystem)

LibGit2Sharp/GlobalSettings.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static class GlobalSettings
2020

2121
private static string nativeLibraryPath;
2222
private static bool nativeLibraryPathLocked;
23+
private static string nativeLibraryDefaultPath;
2324

2425
static GlobalSettings()
2526
{
@@ -28,24 +29,14 @@ static GlobalSettings()
2829

2930
nativeLibraryPathAllowed = netFX || netCore;
3031

31-
if (nativeLibraryPathAllowed)
32+
if (netFX)
3233
{
33-
string assemblyDirectory = GetExecutingAssemblyDirectory();
34-
35-
if (netFX)
36-
{
37-
// For .NET Framework apps the dependencies are deployed to lib/win32/{architecture} directory
38-
nativeLibraryPath = Path.Combine(assemblyDirectory, "lib", "win32");
39-
}
40-
else
41-
{
42-
// .NET Core apps that depend on native libraries load them directly from paths specified
43-
// in .deps.json file of that app and the native library loader just works.
44-
// However, .NET Core doesn't support .deps.json for plugins yet (such as msbuild tasks).
45-
// To address that shortcoming we assume that the plugin deploys the native binaries to runtimes\{rid}\native
46-
// directories and search there.
47-
nativeLibraryPath = Path.Combine(assemblyDirectory, "runtimes", Platform.GetNativeLibraryRuntimeId(), "native");
48-
}
34+
// For .NET Framework apps the dependencies are deployed to lib/win32/{architecture} directory
35+
nativeLibraryDefaultPath = Path.Combine(GetExecutingAssemblyDirectory(), "lib", "win32");
36+
}
37+
else
38+
{
39+
nativeLibraryDefaultPath = null;
4940
}
5041

5142
registeredFilters = new Dictionary<Filter, FilterRegistration>();
@@ -186,6 +177,10 @@ public static LogConfiguration LogConfiguration
186177
/// This must be set before any other calls to the library,
187178
/// and is not available on other platforms than .NET Framework and .NET Core.
188179
/// </para>
180+
/// <para>
181+
/// If not specified on .NET Framework it defaults to lib/win32 subdirectory
182+
/// of the directory where this assembly is loaded from.
183+
/// </para>
189184
/// </summary>
190185
public static string NativeLibraryPath
191186
{
@@ -196,7 +191,7 @@ public static string NativeLibraryPath
196191
throw new LibGit2SharpException("Querying the native hint path is only supported on .NET Framework and .NET Core platforms");
197192
}
198193

199-
return nativeLibraryPath;
194+
return nativeLibraryPath ?? nativeLibraryDefaultPath;
200195
}
201196

202197
set
@@ -225,10 +220,8 @@ public static string NativeLibraryPath
225220
internal static string GetAndLockNativeLibraryPath()
226221
{
227222
nativeLibraryPathLocked = true;
228-
229-
return Platform.IsRunningOnNetFramework() ?
230-
Path.Combine(nativeLibraryPath, Platform.ProcessorArchitecture) :
231-
nativeLibraryPath;
223+
string result = nativeLibraryPath ?? nativeLibraryDefaultPath;
224+
return Platform.IsRunningOnNetFramework() ? Path.Combine(result, Platform.ProcessorArchitecture) : result;
232225
}
233226

234227
/// <summary>

0 commit comments

Comments
 (0)