Description
libgit2sharp is failing to load the native DLLs if the directory path contains %2F
How did I get in this state? Our Jenkins build checks out GitHub repositories with the branch name in them. In my case, the branch name was in the format features/dev10
with a forward slash in the branch name.
That branch path in Windows ends up being C:\jenkins\workspace\features%2Fdev10\
This failure can be easily reproduced in libgit2sharp by setting the Output Path of LibGit2Sharp.Tests
to busted%2Fdirectory\
I've tracked the problem down to GlobalSettings.cs
, lines 22-31:
static GlobalSettings()
{
if (Platform.OperatingSystem == OperatingSystemType.Windows)
{
string managedPath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
nativeLibraryPath = Path.Combine(Path.Combine(Path.GetDirectoryName(managedPath), "lib"), "win32");
}
registeredFilters = new Dictionary<Filter, FilterRegistration>();
}
It looks like creating a Uri
out of the current path turns the legit %2F
in the directory name back into a forward slash.
After that, the DLL load mechanism fails to find the native git2.dll
in NativeMethods.cs
because it adds an invalid directory to the PATH
environment variable
I hit this issue using Cake and the GitVersion tool which in turn uses libgit2sharp
In the meantime, I might be able to add the DLL directory to my PATH environment variable before calling GitVersion as a workaround. Also not putting forward slashes in the git branch names will work around this issue with Jenkins putting the %2F in the directory name.
Stack trace below
14:35:43 Unhandled Exception: System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-381caf5': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
14:35:43 at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
14:35:43 at LibGit2Sharp.Core.NativeMethods.LibraryLifetimeObject..ctor()
14:35:43 at LibGit2Sharp.Core.NativeMethods..cctor()
14:35:43 --- End of inner exception stack trace ---
14:35:43 at LibGit2Sharp.Core.NativeMethods.RemoveHandle()
14:35:43 at LibGit2Sharp.Core.NativeMethods.LibraryLifetimeObject.Finalize()```