Bug 2266 - DllNotFoundException and PATH environment variable
Summary: DllNotFoundException and PATH environment variable
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Chambers
URL:
Depends on:
Blocks:
 
Reported: 2011-11-30 08:10 UTC by emeric.fermas
Modified: 2012-06-23 12:26 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description emeric.fermas 2011-11-30 08:10:23 UTC
This bug occurs on Windows 7 32bits running Mono 2.10

It's been discovered while building LibGit2Sharp, a .Net library that wraps the libgit2 native library.

Previously, native libgit2.dll was next the LibGit2Sharp.dll assembly. DllImport was working fine. Tests were passing fine on Windows against both .Net and Mono 2.10

Commit https://github.com/libgit2/libgit2sharp/commit/52de337d#diff-5 changed this.

The native libgit2.dll was moved into a subdirectory. The PATH process variable has been valued to hint the loader with the correct path.

Tests still pass ok when ran against the .Net framework. However running them against Mono raises this kind of error:

Test 'LibGit2Sharp.Tests.BlobFixture.CanGetBlobAsUtf8' failed: System.DllNotFoundException : git2
	at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_repository_open (LibGit2Sharp.Core.RepositorySafeHandle&,string)
	at LibGit2Sharp.Repository..ctor (System.String path) [0x00000] in <filename unknown>:0
	at LibGit2Sharp.Tests.BlobFixture.CanGetBlobAsUtf8 () [0x00000] in <filename unknown>:0

I've tried to google this. I only found a two years old similar report and no answer (http://lists.ximian.com/pipermail/mono-list/2009-December/043984.html)
Comment 1 emeric.fermas 2012-05-31 17:58:33 UTC
In order to attempt at making the issue easier to reproduce, I've written the following Console app (https://gist.github.com/2846311). This is supposed to reference the LibGit2Sharp NuGet Package v0.9.0 or greater.

Following instructions from http://www.mono-project.com/Interop_with_Native_Libraries#Troubleshooting , I've done the following in a Mono 2.10.8 Command prompt:

SET MONO_LOG_MASK=dll
set MONO_LOG_LEVEL=debug
mono --debug ConsoleApplication2.exe > debug.txt

Content of debug.txt is available here: http://mibpaste.com/AdpjNg

The following exception was thrown and printed out in the console

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2
at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init ()
at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0


--- End of inner exception stack trace ---
at LibGit2Sharp.Repository.RetrieveVersion () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Core.Compat.Lazy`1[System.String].Evaluate () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Core.Compat.Lazy`1[System.String].get_Value () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Repository.get_Version () [0x00000] in <filename unknown>:0
at Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2
at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init ()
at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0


--- End of inner exception stack trace ---
at LibGit2Sharp.Repository.RetrieveVersion () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Core.Compat.Lazy`1[System.String].Evaluate () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Core.Compat.Lazy`1[System.String].get_Value () [0x00000] in <filename unknown>:0
at LibGit2Sharp.Repository.get_Version () [0x00000] in <filename unknown>:0
at Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
Comment 2 emeric.fermas 2012-05-31 18:04:20 UTC
Below the output of the run of ConsoleApplication2.exe without involving Mono

Last line is the version of LibGit2Sharp.
Note the PATHs are only printed once, because no exception was thrown.

originalAssemblypath = C:\Users\Emeric\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe
parentPath = C:\Users\Emeric\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug
currentArchSubPath = NativeBinaries/x86
path = C:\Users\Emeric\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\NativeBinaries/x86

PATH.Machine = C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\CMake 2.8\bin
PATH.User =
PATH.Process = C:\PROGRA~1\MONO-2~1.8\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\CMake 2.8\bin
0.9.5-c1743ba-4c977a6 (x86)


When peeking at the output of the Mono debug session (http://mibpaste.com/AdpjNg), the PATHs are printed twice.
The second batch contains the following line which correctly points at the dll location

PATH.Process = C:\Users\Emeric\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\NativeBinaries/x86;
Comment 3 emeric.fermas 2012-05-31 18:07:51 UTC
One final note, the PATH env variable is dynamically set by the static constructor of the NativeMethods type of LibGit2Sharp.

cf. https://github.com/libgit2/libgit2sharp/blob/vNext/LibGit2Sharp/Core/NativeMethods.cs#L15-32
Comment 4 Jonathan Chambers 2012-06-23 00:28:55 UTC
This should be fixed by a pull request to libgit2sharp: https://github.com/libgit2/libgit2sharp/pull/190
Comment 5 emeric.fermas 2012-06-23 12:26:32 UTC
Thanks a lot for this. Indeed the pull request does the trick!