question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:30 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
bordingcommented, Aug 25, 2018

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.

1reaction
flibustier7seascommented, Aug 10, 2018

@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")
Read more comments on GitHub >

github_iconTop Results From Across the Web

The type initializer for 'LibGit2Sharp.Core.NativeMethods ...
1) and when running its executable I get the following error: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.
Read more >
The type initializer for 'LibGit2Sharp.Core.NativeMethods' ...
Error: System.TypeInitializationException : The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. —> System.
Read more >
PowerShell connection from MacOS not working
Unfortunately logging in results in an error: Connect-SPOService: The type initializer for 'Microsoft.Win32.Registry' threw an exception ...
Read more >
XAML build service fails with: “The type initializer for ...
The complete stack is given below: Exception Message: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.
Read more >
The type initializer for 'LibGit2Sharp.Core.NativeMethods ...
Coding example for the question The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception-C#.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found