libgit2 / libgit2sharp

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

Unable to load shared library 'git2-8a0dc67' or one of its dependencies #1914

Closed dradovic closed 2 years ago

dradovic commented 2 years ago

Reproduction steps

We sporadically experience the following exception in our Bitbucket CI environment, when a test tries to use the LibGit2Sharp library:

System.TypeInitializationException : The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---- System.DllNotFoundException : Unable to load shared library 'git2-8a0dc67' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgit2-8a0dc67: cannot open shared object file: No such file or directory 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 LibGit2Sharp.Repository..ctor(String path)

Version of LibGit2Sharp (release number or SHA1)

<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0119" />

Operating system(s) tested; .NET runtime tested

The tests are running against the mcr.microsoft.com/dotnet/sdk:5.0.202 image.

dradovic commented 2 years ago

I want to add that I was never able to reproduce this issue in complete isolation. Even in the CI. It occurs only sporadically, when all tests run (many of them in parallel). However, there's only one test that uses the LibGit2Sharp nuget.

So I'm wondering what (race) conditions might cause that library not to be found.

I've also compared the build output directories of a successful with a failed run, and in both cases I see the libgit2-8a0dc67.* files in the same runtimes subfolders. So the building is consistent. It's definitely a runtime problem.

pattyr commented 2 years ago

Hello!

We got the same error (with different sha) in a Lambda function recently.

Found the that it didn't like the linux-x64 version of the libgit2-*.so dotnet publish provided.

To mitigate, the libgit2-*.so for runtime rhel-x64 was copied and put alongside the project files.

Then added that libgit2-*.so to be copied to output directory in the *.csproj.

Here's an example to reproduce the issue. It includes the "fix" described.

Hope that can help some! 😃

dradovic commented 2 years ago

I've found the what was causing the instability of our CI: it seems the culprit was a test that was using the Buildalyzer.Workspaces package (v3.2.3) which in turn uses Roslyn (Microsoft.CodeAnalysis.CSharp.Workspaces) to compute certain code statistics. Once I had deactivated that test, the problem never appeared again.