libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.18k stars 888 forks source link

Can't use Library from FSI (Fsharp Interactive): System.DllNotFoundException: Unable to load DLL 'git2-b0d9952': The specified module could not be found. (Exception from HRESULT: 0x8007007E) #1588

Open fbehrens opened 6 years ago

fbehrens commented 6 years ago

Reproduction steps

in a fresh fsi (using ionide ) i can reference LibGit2Sharp.dll and open the LibGit2Sharp namespace, but instantiating a Repository throws following error:

Microsoft (R) F# Interactive version 10.1.0 for F# 4.1

> #r "../../packages/play/LibGit2Sharp/lib/netstandard2.0/LibGit2Sharp.dll";;

--> Referenced 'd:\scripts2dev\libwba\src\play\../../packages/play/LibGit2Sharp/lib/netstandard2.0/LibGit2Sharp.dll' (file may be locked by F# Interactive process)

> open LibGit2Sharp;;
> let  repo = new Repository("path\to\repo.git");;
System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-b0d9952': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
   at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary()
   at LibGit2Sharp.Core.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at LibGit2Sharp.Core.NativeMethods.git_repository_open(git_repository*& repository, FilePath path)
   at LibGit2Sharp.Core.Proxy.git_repository_open(String path)
   at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter)
   at <StartupCode$FSI_0003>.$FSI_0003.main@() in d:\scripts2dev\libwba\src\play\LibGit2Sharp.fsx:line 4
Stopped due to error

And I can't reference the dll from LibGit2Sharp.NativeBinaries, because it throws

> #r @"../..\packages\play\LibGit2Sharp.NativeBinaries\runtimes\win-x64\native\git2-b0d9952.dll";;

LibGit2Sharp.fsx(7,1): error FS0229: Error opening binary file 'd:\scripts2dev\libwba\src\play\../..\packages\play\LibGit2Sharp.NativeBinaries\runtimes\win-x64\native\git2-b0d9952.dll': d:\scripts2dev\libwba\packages\play\LibGit2Sharp.NativeBinaries\runtimes\win-x64\native\git2-b0d9952.dll: bad cli header, rva 0

LibGit2Sharp.fsx(7,1): error FS3160: Problem reading assembly 'd:\scripts2dev\libwba\src\play\../..\packages\play\LibGit2Sharp.NativeBinaries\runtimes\win-x64\native\git2-b0d9952.dll': Processing of a script fragment has stopped because an exception has been raised

What am I missing? How can I use the compiled libraries ?

Expected behavior

It is possible to reference and use LibGit2Sharp in fsi

Actual behavior

see above errors

Version of LibGit2Sharp (release number or SHA1)

So i am dowloading via nuget LibGit2Sharp prerelease in my paket.dependencies, which resolves to

LibGit2Sharp (0.26.0-preview-0027)
LibGit2Sharp.NativeBinaries (1.0.226) - restriction: >= netstandard2.0

Operating system(s) tested; .NET runtime tested

this is on Server 2016 with net471 which should behave like windows 10.

ethomson commented 6 years ago

I'm not familiar with the F# Interactive interpreter, but a number of people have used LibGit2Sharp from an F# application. I suspect that the F# Interactive interpreter is not setting things up nicely.

Can you debug to see where it's trying to load the native library from?

fbehrens commented 6 years ago

Thanks for the feedback. Yes, when I compile it is working fine.

Below are the details of the exception I got when I run above code fsi. Not sure if that helps

Message "The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception."  string
at LibGit2Sharp.Core.NativeMethods.git_repository_open(git_repository*& repository, FilePath path)
at LibGit2Sharp.Core.Proxy.git_repository_open(String path)
at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter)
at <StartupCode$FSI_0003>.$FSI_0003.main@() in D:\\scripts2dev\\libwba\\src\\LibGit\\LibGit2Sharp.fsx:line 3"  string

Inner Exception:
Message "Unable to load DLL 'git2-b0d9952': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"  string
at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary()
at LibGit2Sharp.Core.NativeMethods..cctor()"
generik0 commented 6 years ago

@ethomson I get this issue with 1.0.226 in C# when using NUnit. But not in 1.0.210 (i have not tried 1.0.117). Could there be a bug? Also cannot load the dll manually, but i can with 210...

maikebing commented 5 years ago

at LibGit2Sharp.Core.NativeMethods.git_libgit2_init () atLibGit2Sharp.Core.NativeMethods.LoadNativeLibrary () In LibGit2Sharp.Core.NativeMethods. Cctor () ---the end of the internal exception stack trace--- atLibGit2Sharp.Core.NativeMethods.git_buf_free (Gitbuf buf) atLibGit2Sharp.Core.Handles.GitBuf.Dispose () atLibGit2Sharp.Core.Proxy.ConvertPath (Func ' 2 pathretriever) atLibGit2Sharp.Core.Proxy.git_repository_discover (FilePath start_path) In LibGit2Sharp.Repository.Discover (String startingpath)

goswinr commented 3 years ago

I managed to load the native dll via LoadLibrary of kernel32

See also https://christoph.ruegg.name/blog/loading-native-dlls-in-fsharp-interactive.html

[<DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
extern IntPtr LoadLibrary(string lpFileName);