Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found. #1533

Open
CalvinFengDatacom opened this issue Feb 11, 2018 · 31 comments

Comments

@CalvinFengDatacom
Copy link

I'm using Azure Function App to create a app for updating Excel file to our ASP.Net Web Api project, which is hosting in Azure, and we are using Git to control the version of files.

Now, I'm having an issue of using LibGit2Sharp. I'd like to use Commands.Checkout() command to switch between branches. However, I got this issue:
LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found.

I'm using the latest version of LibGit2Sharp version 0.24.0, and I see it depends on LibGit2Sharp.NativeBinaries": "[1.0.185]"

Does anyone who has any idea how to solve this issue on Function App?

Thanks

@ethomson
Copy link
Member

Good question - LibGit2Sharp relies on libgit2 itself, a native library, which it accesses through PInvoke. I'm not familiar enough to know if you can use pinvoke with Azure Functions, or if we need to adjust the way we load the native libraries so that Azure Functions is successful.

@CalvinFengDatacom
Copy link
Author

@ethomson Thanks your for your reply, seems this is a quite new question of LibGit2Sharp. Can you tell me which PInvoke should I use? Can I find it through NuGet? If it's available on NuGet, then I can try to add it to my function app.

@bording
Copy link
Member

bording commented Feb 12, 2018

@CalvinFengDatacom You don't have to add anything. The LibGit2Sharp package includes a reference to the LibGit2Sharp.NativeBinaries package, which includes the required native library.

There must be something specific to how Azure Functions works that is preventing the native library from being in the expected location when your code runs.

@CalvinFengDatacom
Copy link
Author

@bording Thanks, I see when I reload the packages of my Azure function App, LibGit2Sharp.NativeBinaries/1.0.185 has been added automatically.
But, I still got this issue: LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found.
Maybe @ethomson is right, seems the way of loading native libraries to Azure Functions need to be adjusted.

But, I'm happy to try other ways if you guys have some idea. I can just setup a small Function App to try this.

@ethomson
Copy link
Member

Are you able to run this locally in Azure Functions Core Tools?

Are you set up for a 64 bit Azure Function runtime? https://blogs.msdn.microsoft.com/benjaminperkins/2017/02/09/azure-functions-on-64-bit/
Is there any effect from changing these settings?

@CalvinFengDatacom
Copy link
Author

@ethomson I have changed for using 64 bit of Azure function runtime, but still got the same result.
I haven't try running it locally, should be the same I think.

@CalvinFengDatacom
Copy link
Author

CalvinFengDatacom commented Feb 13, 2018

@ethomson I have tried to debug Azure function locally from Visual studio by Attach Debugger.
https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/

I got the same issue from log console: LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found.

Um... Seems I have tried everything already.

@CalvinFengDatacom
Copy link
Author

@ethomson @bording Hi guys, any updates of using LibGit2Sharp in Azure function app? I have tried to run azure function app locally following this way https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/

But, I still got the same issue from log console.

@MikhailTymchukDX
Copy link

MikhailTymchukDX commented Mar 2, 2018

I get the same message when trying to run a .NET Core app (v0.25.0-preview0073) inside a Linux docker container (using Windows 10 Docker for Windows).

According to this comment, I expected it works inside a Linux containers.

Running an app inside Windows container works ok, but it's not an option for me, because I already use Linux containers for another app (WIndows for Docker can not run both types of containers simultaneously).

@ethomson
Copy link
Member

ethomson commented Mar 2, 2018

@MikhailTymchukDX please try using a debian based container. If you're using alpine or centos, you will need to provide your own libgit2 binaries.

@MikhailTymchukDX
Copy link

@ethomson Thanks for that tip!

I changed base image to microsoft/dotnet:2.0-sdk-jessie and everything works now on Linux container.

@Cyberboss
Copy link

Cyberboss commented Mar 17, 2018

Changing to jessie didn't quite do the trick: I'm still getting the same error with this simple program

using LibGit2Sharp;

namespace TestClone
{
    class Program
    {
        static void Main(string[] args) => Repository.Clone("https://github.com/tgstation/tgstation", "tgstation");
    }
}

Only dep is LibGit2Sharp 0.25.0-preview-0081
Dockerfile:

FROM microsoft/aspnetcore:2.0.6-jessie AS base
WORKDIR /app

FROM microsoft/aspnetcore-build:2.0.6-2.1.101-jessie AS build
WORKDIR /src
COPY . .

RUN dotnet restore TestClone/TestClone.csproj

WORKDIR /src/TestClone
RUN dotnet publish -c Docker -o /app
FROM base AS final
WORKDIR /app
COPY --from=build /app .
RUN ls
RUN dotnet TestClone.dll
ENTRYPOINT ["./TestClone"]

Result:

1>Step 12/14 : RUN ls
1> ---> Running in 9142e6549a2a
1>LibGit2Sharp.dll
1>TestClone.deps.json
1>TestClone.dll
1>TestClone.pdb
1>TestClone.runtimeconfig.json
1>runtimes
1>Removing intermediate container 9142e6549a2a
1> ---> 0fb110df7e42
1>Step 13/14 : RUN dotnet TestClone.dll
1> ---> Running in d71b2bd4a62e
1>�[91m
1>Unhandled Exception: �[0m�[91mSystem.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-2d2a602': The specified module or one of its dependencies could not be found.
1> (Exception from HRESULT: 0x8007007E)
1>   at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
1>   at LibGit2Sharp.Core.NativeMethods.LoadNativeLibrary()
1>   at LibGit2Sharp.Core.NativeMethods..cctor()
1>   --- End of inner exception stack trace ---
1>   at LibGit2Sharp.Core.NativeMethods.git_clone(git_repository*& repo, String origin_url, FilePath workdir_path, GitCloneOptions& opts)
1>   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts)
1>   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)
1>   at TestClone.Program.Main(String[] args) in /src/TestClone/Program.cs:line 9
1>�[0m�[91mAborted
1>Service 'mapdiffbot' failed to build: The command '/bin/sh -c dotnet TestClone.dll' returned a non-zero code: 134

EDIT: Upon further investigation this seems to be unique to the aspnetcore image. dotnet-sdk and aspnetcore-build work fine

@mattjohnsonpint
Copy link

mattjohnsonpint commented Mar 30, 2018

I'm getting the same problem also (in a docker container). Maybe this is naive, but is the issue not just that the libgit2sharp.nativebinaries package ships the file as git2-version.dll on Windows, and libgit2-version.so on Linux?

There's some discussion of these naming issues in dotnet/coreclr#930.

(Using -jessie or -stretch didn't work for me.)

@Cyberboss
Copy link

Cyberboss commented Mar 30, 2018 via email

@bording
Copy link
Member

bording commented Mar 30, 2018

I'm getting the same problem also (in a docker container). Maybe this is naive, but is the issue not just that the libgit2sharp.nativebinaries package ships the file as git2-version.dll on Windows, and libgit2-version.so on Linux?

@mj1856 That pattern is the one that is already accounted for via DllImport, so no that's not the issue.

There are two things that could be causing the problem:

  • The native binary isn't being included in the container in the correct location for it to be found
  • The base container image you're using doesn't have all of the native dependencies that libgit2 needs.

@mattjohnsonpint
Copy link

Ok, I got it working with Jessie. I'm not using asp.net core, just the dotnet base images. microsoft/dotnet:2.0-runtime-jessie and microsoft/dotnet:2.0-sdk-jessie. Of the dependencies listed in @Cyberboss's link, the only one I needed to install was libcurl3-gnutls. I'm guessing this is because I was cloning an https repo.

The -stretch images did not work, so I'm wondering what else needs to be installed there. Also, I'd prefer to use openssl instead of gnutls, but I'm not sure how to do that. Anyone know?

Thanks.

@Cyberboss
Copy link

Cyberboss commented Apr 2, 2018 via email

@jhueppauff
Copy link

did anyone managed to get this working inside an azure function?

@marcdurham
Copy link

I was having this same issue on a microsoft/aspnetcore:2.0 Docker image.
I was getting this error: DllNotFoundException: Unable to load DLL 'git2-6311e88':
So I followed the link from Cyberboss
Then changed my image version from 2.0 to 2.0.7-jessie

FROM microsoft/aspnetcore:2.0.7-jessie AS base

Then I pasted this code right below the FROM line...

# Dependencies for libgit2
RUN apt-get update && apt-get install -y --no-install-recommends \
	libc6 \
	zlib1g-dev \
	libcomerr2 \
	libc6-dev \
	libgcrypt20 \
	libkeyutils1 \
	libcurl3-gnutls \
	libsasl2-2 \
	libgpg-error0 \
	&& rm -rf /var/lib/apt/lists/*

Then it worked.
Then I rejoiced.
Thanks Cyberboss!

@ethomson
Copy link
Member

I don't yet have a full understanding of Azure Functions, but: #1574 (comment) indicates that this is indeed possible:

This morning I solved the problem. I used an Azure Function v1 (.NET Framework instead of .NET Standard) with the LibGit2Sharp 0.21.0.176. When you include the native binary in the release it is working!

@jeremymeng
Copy link

There might be some changes in the native dependencies in 2.1 docker images. The above method worked in 2.0 jessie images, but does not work on 2.1 because only stretch-based images are available.

@bording
Copy link
Member

bording commented Jun 12, 2018

#1582 should hopefully be addressing this.

@felixfbecker
Copy link

I have the same issue when trying to run LibGit2Sharp in PowerShell Core on macOS:

> [LibGit2Sharp.Repository]::new($PWD)
Exception calling ".ctor" with "1" argument(s): "The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception."
At line:1 char:1
+ [LibGit2Sharp.Repository]::new($PWD)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException

LibGit2Sharp v0.25.2

@ethomson
Copy link
Member

@felixfbecker That doesn't sound like it has anything to do with this issue, which is regarding Azure Functions.

@felixfbecker
Copy link

I figured I would comment here since it's the same error message and people above also said they are having the same issue on Linux and in Linux Docker containers.
Should I open a new issue?

@ethomson
Copy link
Member

Yeah, this thread has become very messy with similar problems (can't load the native libraries) but very different root causes / platforms. A new issue would be helpful.

@felixfbecker
Copy link

@ethomson filed #1583

@phuongph
Copy link

I have a similar issue building *.msi Windows Installer file. When I include following files to the output folder, it works.
\lib\win32\x64\git2-1196807.dll and \lib\win32\x86\git2-1196807.dll

@yury-kozlov
Copy link

I got similar error when running under target framework of 4.6.1. When I changed target framework to 4.6.2 the problem solved.

@deadman2000
Copy link

Found temporary solution for docker:

export LD_LIBRARY_PATH=/root/.nuget/packages/gitversion.tool/5.1.2/tools/netcoreapp3.0/any/runtimes/debian.9-x64/native/

@mattjohnsonpint
Copy link

mattjohnsonpint commented Jul 20, 2020

An update for those trying to get libgit2sharp to work in Azure Functions. (This is just my own experience. YMMV.)

If you publish your project with default "portable" (i.e. "any" platform), then libgit2sharp will use the the native libgit2 library from the linux-x64 runtimes folder. Unfortunately, that one is compiled against libssl 1.0 which is deprecated in favor of libssl 1.1.

On some Linux environments, libssl 1.0 is pre-installed. However, Linux-based Azure Functions currently uses Debian 9 ("stretch" ) or Debian 10 ("buster") as its runtime. Debian 9 has a libssl 1.0 library available, but not pre-installed. Debian 10 does not have a libssl 1.0 library at all.

Thankfully, libgit2sharp has a Debian 9 native libgit2 library in its runtimes folder, which was built against libssl 1.1. Therefore, all you need to do to work around this issue is publish using the Debian 9 runtime identifier, which is debian.9-x64. That will work with Linux-based Azure Functions on Linux, with or without containers.

If you publish on the command line, add -r debian.9-x64 to your dotnet publish command (in your dockerfile if you use containers).

If you publish from Visual Studio, first create a publishing profile (or edit your existing one), selecting linux-x64 from the target runtime dropdown. Then go edit the .pubxml file for that profile and change to <RuntimeIdentifier>debian.9-x64</RuntimeIdentifier>. (The two steps are needed only because the UI doesn't let you type in other RIDs.)

If you publish from VSCode, edit the .vscode/tasks.json file, find the publish task, and add "--runtime", "debian.9-x64", to the list of args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests