libgit2 / libgit2/libgit2sharp

Failed to load native git library inside a powershell module

Open
#2,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
3.5k
Forks
925
PR merge metrics
No merged PRs in 30d

Description

I made a class library that uses LibGit2Sharp to perform some actions on a repository.
This class library is loaded in a console app with no problem
I'm trying to create a powershell module that uses the same class library but it seems to fails to load the git native library

I'm not too sure if the issue is coming from a badly configured project or if it's just the powershell module that fails to automatically bind the proper native dll

I get the following exception:
The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.

Stack trace:
at LibGit2Sharp.Core.NativeMethods.git_repository_open(git_repository*& repository, FilePath path)
at LibGit2Sharp.Core.Proxy.git_repository_open(String path) in //LibGit2Sharp/Core/Proxy.cs:line 2552
at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter) in /
/LibGit2Sharp/Repository.cs:line 159
at LibGit2Sharp.Repository..ctor(String path) in /_/LibGit2Sharp/Repository.cs:line 70
at ClassLibrary.Class1.Init(String path) in D:\dev\Perso\ClassLibrary\Class1.cs:line 11
at MyModule.TestSampleCmdletCommand.ProcessRecord() in D:\dev\Perso\MyModule\TestSampleCmdletCommand.cs:line 14
at System.Management.Automation.CommandProcessor.ProcessRecord() in System.Management.Automation\CommandProcessor.cs:line 200

InnerException:
Unable to load DLL 'git2-a418d9d' or one of its dependencies: The specified module could not be found. (0x8007007E)

at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary() in //LibGit2Sharp/Core/NativeMethods.cs:line 148
at LibGit2Sharp.Core.NativeMethods..cctor() in /
/LibGit2Sharp/Core/NativeMethods.cs:line 54

Reproduction steps

LibGit2SharpMiniProject.zip
https://github.com/NephChevsky/LibGit2SharpMiniProject
I created a simple project to reproduce this issue
Just launch the project with the launchSettings.json configuration and type the command: "Test-SampleCmdlet"

Expected behavior

Should find and load the right native library properly

Actual behavior

Could not load native library git2-a418d9d.dll

Version of LibGit2Sharp (release number or SHA1)

0.30.0

Operating system(s) tested; .NET runtime tested

net8
Windows 11 Professional

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the linked LibGit2SharpMiniProject by launching with launchSettings.json and running Test-SampleCmdlet. Start with NativeMethods.InitializeNativeLibrary at NativeMethods.cs line 148 and the PowerShell module loading path, then verify that the correct native library and dependencies load under the module as they do in the console app.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, git, powershell
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.