libgit2sharp: Error under macOS in PowerShell Core: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception

Reproduction steps

  • Download LibGit2Sharp with the dotnet CLI
  • Import it into PowerShell Core with Import-Module
  • Run [LibGit2Sharp.Repository]::new($PWD) in a git repository

Expected behavior

No error

Actual behavior

Error

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

Version of LibGit2Sharp (release number or SHA1)

v0.25.2

Operating system(s) tested; .NET runtime tested

> $PSVersionTable

Name                           Value                                                                                                                                                                     
----                           -----                                                                                                                                                                     
PSVersion                      6.0.2                                                                                                                                                                     
PSEdition                      Core                                                                                                                                                                      
GitCommitId                    v6.0.2                                                                                                                                                                    
OS                             Darwin 17.6.0 Darwin Kernel Version 17.6.0: Tue May  8 15:22:16 PDT 2018; root:xnu-4570.61.1~1/RELEASE_X86_64                                                             
Platform                       Unix                                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                   
PSRemotingProtocolVersion      2.3                                                                                                                                                                       
SerializationVersion           1.1.0.1                                                                                                                                                                   
WSManStackVersion              3.0  
> dotnet --info    
.NET Command Line Tools (2.1.4)

Product Information:
 Version:            2.1.4
 Commit SHA-1 hash:  5e8add2190

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.12-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.4/

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.5
  Build    : 17373eb129b3b05aa18ece963f8795d65ef8ea54

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 30 (12 by maintainers)

Most upvoted comments

BTW: What is the intention of the commit I pointed to?

That code was an attempt to address some of the complexities involved in resolving the correct native libraries, but it turns out it only really works on Windows, so it’s not as helpful as it was thought it would be when the PR that added it was merged.

I see. Thanks for the response. Are there any samples for the AssemblyLoadContext that does this that you can suggest perhaps?

You might take a look at https://github.com/dotnet/sourcelink/tree/master/src/Microsoft.Build.Tasks.Git for ideas.

@felixfbecker

Do you see any way for me to depend on LibGit2Sharp in a PowerShell module? Maybe install the package on first run, or bundle all runtimes and move files around to pick the right one on first run?

I was getting the same error: “The type initializer for ‘LibGit2Sharp.Core.NativeMethods’ threw an exception” under Windows. I saw a solution in the PSGit repository.

# Add paths to native libraries

$nativeBinariesPackage = Get-Package LibGit2Sharp.NativeBinaries -RequiredVersion 1.0.217
$nativeBinariesPath = Split-Path $nativeBinariesPackage.Source

# Copy-paste from https://github.com/PoshCode/PSGit/blob/dev/src/PSGit.psm1#L34
${;} = [System.IO.Path]::PathSeparator
switch -Wildcard (Get-ChildItem -Path "$nativeBinariesPath" -Recurse -Filter '*git2-6311e88.*') {
    "*.so"   { $env:LD_LIBRARY_PATH = "" + $_.Directory + ${;} + $Env:LD_LIBRARY_PATH }
    "*.dll"  { $env:PATH = "" + $_.Directory + ${;} + $Env:PATH }
    "*.dyld" { $env:DYLD_LIBRARY_PATH = "" + $_.Directory + ${;} + $Env:DYLD_LIBRARY_PATH }
}

# Load LibGit2Sharp assembly

$package = Get-Package LibGit2Sharp -RequiredVersion 0.25.2
$assemblyPath = (Get-ChildItem -Path (Split-Path $package.Source)  -Recurse  -Filter "*LibGit2Sharp.dll").FullName

Import-Module $assemblyPath

$repositoryPath = [LibGit2Sharp.Repository]::Init("F:\NewRepository")