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

The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. #446

Closed
joaoferreira opened this issue Jun 6, 2013 · 20 comments

Comments

@joaoferreira
Copy link

I'm build a Windows Form application and each time I try to make a clone of a repository I get this error The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. without any further detail.

Anyone knows what could be causing this, I'm getting crazy trying to figure it out.

@nulltoken
Copy link
Member

This reminds me of issue #288.

The LibGit2Sharp assembly expects to find next to it a directory structure as follows

NativeBinaries+
              |___amd64+
                       |___git2.dll (or git2-{shortsha}.dll depending of your version)
                       |___git2.pdb (or git2-{shortsha}.pdb depending of your version)
              |_____x86+
                       |___git2.dll (or git2-{shortsha}.dll depending of your version)
                       |___git2.pdb (or git2-{shortsha}.pdb depending of your version)
  • If you've build LibGit2Sharp by yourself, from the repository, you'll have to modify your the .csproj of your own project to make this happen in order to add a MSBuild AfterBuild target. You can take some inspiration from the implementation of the LibGit2Sharp.Tests project which implements this pattern.
  • If you've relied on the NuGet package manager, this shouldn't happen. Indeed the package will take care of recreating this structure in your project output directory each time you build your project thanks to a post-build event.

@joaoferreira
Copy link
Author

I have used the NuGet package.

I was able to get further details about the error:
Unable to load DLL 'git2-9d9fff3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Have check and the files are in the folders like the.

@nulltoken
Copy link
Member

I cannot reproduce this. Can you please share a .zip file of a stripped down version of your project so that I can investigate?

@joaoferreira
Copy link
Author

You can download the project from the link bellow
https://dl.dropboxusercontent.com/u/103265141/BindTuning.zip

This is a visual studio addin, when you run it a new instance of the visual studio will open and you should go to Tools -> BindTuning to open the form.

@nulltoken
Copy link
Member

Hmmm. Sadly I'm not a VSIX expert. 😿

However, you may get some inspiration from @xpaulbettsx's SaveAllTheTime Visual Studio extension which also relies on LibGit2Sharp.

More specifically, it looks like the .csproj file specifically includes as content the .dll and .pdb files and relies on the IncludeInVSIX directive.

@anaisbetts
Copy link
Contributor

Yes, that's the trick:

  1. Add Existing File
  2. Browse to git2.dll/git2.pdb
  3. Click the dropdown next to the Add button, make it "Add as Link"
  4. On those linked files, hit F4 to see Properties
  5. Change it to "Copy If Newer" and for git2.dll, "IncludeInVSIX" is True

@nulltoken
Copy link
Member

@xpaulbettsx Thanks for giving a hand. You ROCK, mate!

@joaoferreira Closing this now. Feel free to add a comment if the problem isn't fixed by @xpaulbettsx's guidance

@laurentkempe
Copy link

Exactly what I was looking for for GitDiffMargin. Thanks @xpaulbettsx

@aloon
Copy link

aloon commented Jun 11, 2014

I have the same problem but in mono. What file I have to include?
..../packages/LibGit2Sharp.0.18.1.0/lib/net40/NativeBinaries/amd64/git2-90befde.dll
not runs

@anaisbetts
Copy link
Contributor

@aloon You most likely need to add a DLLMap section to your app config, to map git2-90befde to your system libgit2 (or whatever libgit2 you intend to use): http://www.mono-project.com/Interop_with_Native_Libraries

@aloon
Copy link

aloon commented Jun 11, 2014

@paulcbetts I have not tried yet but it sure is the solution for me. thank you very much

@nulltoken
Copy link
Member

@aloon In order to avoid side effects, I suggest you to always rely on the exact libgit2 version that is pointed at by the submodule. Take a look at build.libgit2sharp.sh to get a quick glimpse at the recommended CMake options to build the native binary.

Then, provided the libgit2 path is added to [DY]LD_LIBRARY_PATH, LibGit2Sharp should be able to directly bind to it.

As far as I know, we've been able to work like this without relying on the <dllmap/> mechanism.

If your use case requires a <dllmap/> section, that'd awesome if you could share it with us!

/cc @Therzok

@DavidHooper
Copy link

In case anyone comes across this again. Here is how I solved the issue of native binaries not being copied across.

Have a project property file that references LibGit2Sharp
LibGit2Sharp.props

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <None Include="path-to\git2-e0902fb.dll">
            <Link>NativeBinaries\amd64\git2-e0902fb.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Include="path-to\git2-e0902fb.pdb">
            <Link>NativeBinaries\amd64\git2-e0902fb.pdb</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Include="path-to\x86\git2-e0902fb.dll">
            <Link>NativeBinaries\x86\git2-e0902fb.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Include="path-to\x86\git2-e0902fb.pdb">
            <Link>NativeBinaries\x86\git2-e0902fb.pdb</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>
</Project>

Reference it in your library.
MyProject.csproj

<Import Project="LibGit2Sharp.props" Condition="Exists('LibGit2Sharp.props')" />

In your run project (the main project). Add the following.
MyExecutable.csproj

<ItemGroup>
    <Content Include="$(OutputPath)\NativeBinaries\**\*.dll">
      <Link>NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)</Link>
      <Visible>false</Visible>
      <PublishState>Include</PublishState>
    </Content>
  </ItemGroup>

@chrisRedwine
Copy link

chrisRedwine commented Jul 29, 2016

New user - I created a simple console application in VS2015, installed LibGit2Sharp 0.22.0 via Nuget, and got the same error as above (The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception) when running the following hello world example:

using System;

namespace GitTest
{
    class Program
    {
        static void Main(string[] args)
        {
            LibGit2Sharp.Repository.Clone(@"https://github.com/libgit2/libgit2sharp.git", @"H:\");
        }
    }
}

I wonder why a basic Nuget Install doesn't work for me for this package? @nulltoken? None of the above solutions work...

@bording
Copy link
Member

bording commented Jul 29, 2016

@chrisRedwine Does your project also include the LibGit2Sharp.NativeBinaries package?

@chrisRedwine
Copy link

@bording Yes, installing LibGit2Sharp v0.22.0 also installed LibGit2Sharp.NativeBinaries v1.0.129 for me.

@bording
Copy link
Member

bording commented Jul 29, 2016

@chrisRedwine Do you have your example online anywhere that I could see if I can repro the problem locally?

@chrisRedwine
Copy link

chrisRedwine commented Jul 29, 2016

@bording Not sure if this will work for you, but I added it as a dummy repo to my profile (https://github.com/chrisRedwine/GitTest), with no gitignore. Third commit is before adding LibGit2Sharp, fourth commit is after. Thanks!

@bording
Copy link
Member

bording commented Jul 29, 2016

@chrisRedwine Everything looks fine with that project, and once I adjusted for the non-existent H: drive on my machine, it runs without errors.

@chrisRedwine
Copy link

@bording Interesting. Well, that's helpful info - thanks so much for looking into it.

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

8 participants