Using libgit2sharp with LINQPad?

Putting the dll into a folder that is in your path or adding the location of your dll to your path should work (and works for me)

Perhaps your changes have not taken effect. Try closing and re-opening LinqPad or if that fails, log out and back into windows.


EDIT: LINQPad again supports LibGit2Sharp out of the box (as of LINQPad 5.10.02 - in beta at time of writing). No workarounds are now required.

Unfortunately, this is broken again in LibGit2Sharp v0.22. This package no longer contains the native binaries in a subfolder beneath where LibGit2Sharp.dll is located in /libs. Instead, they are in a separate dependent NuGet package which relies on patching a project file. As LINQPad doesn't use project files, this fails.

You can work around this by adding an initialization method as suggested in other answers to populate the PATH variable with the native folder location. The following code will work without modification on any machine with the current LibGit2Sharp:

void Main()
{
   ... your query here...
}

static UserQuery()
{
   const string pathEnvVariable = "PATH";
   char slash = Path.DirectorySeparatorChar;
   char pathSep = Path.PathSeparator;

   // The correct native binary file is located under a folder ending in
   // "windows\x86" or "windows\amd64" or "win7-x86\native" or "win7-x64\native".
   // This may change in later LibGit2Sharp releases.
   string nativeStem1 = $"{slash}windows{slash}{(IntPtr.Size == 8 ? "amd64" : "x86")}";
   string nativeStem2 = $"{(IntPtr.Size == 8 ? "-x64" : "-x86")}{slash}native";

   // Locate the root folder in the NuGet package. This contains folders for the
   // main package (LibGit2Sharp) plus dependencies (LibGit2Sharp.NativeBinaries).
   var nugetRoot = new FileInfo (typeof (Repository).Assembly.Location)
      .Directory.Parent.Parent.Parent;

   var nativeBinaryPath = nugetRoot
      .GetFiles ("*.dll", SearchOption.AllDirectories)
      .Single (d => d.DirectoryName.EndsWith (nativeStem1, StringComparison.OrdinalIgnoreCase) ||
                    d.DirectoryName.EndsWith (nativeStem2, StringComparison.OrdinalIgnoreCase))
      .Directory
      .FullName;

   string currentPaths = Environment.GetEnvironmentVariable (pathEnvVariable);

   if (!(pathSep + currentPaths + pathSep).ToUpperInvariant().Contains
      (pathSep + nativeBinaryPath.ToUpperInvariant() + pathSep))
   {
      Environment.SetEnvironmentVariable (pathEnvVariable, currentPaths + pathSep + nativeBinaryPath);
   }
}

Note that you don't need to explicitly call this method because it's in a static constructor.

Another workaround would be to publish another LibGit2Sharp package on NuGet that included the native binaries in the expected locations under where LibGit2Sharp.dll is located (NativeBinaries\x86\git2-785d8c4.dll and NativeBinaries\amd86\git2-785d8c4.dll) - download LibGit2Sharp 0.21.0.176 for an example.


I ended up adding the related path at runtime using:

void SetUpNativeBinaries(){
    var appDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    var packageVersion = "LibGit2Sharp.0.14.0.0";// may read from PackageInfo
    var processorArchitecture = IntPtr.Size==8?"amd64":"x86";
    var libgitNativeBinaryPath = Path.Combine(appDataLocal, "LINQPad", "NuGet", "LibGit2Sharp", packageVersion, "NativeBinaries", processorArchitecture);
    libgitNativeBinaryPath.Dump();

    var pathEnvVariable = "PATH";
    var currentPaths = Environment.GetEnvironmentVariable(pathEnvVariable);
    var combinedPaths = String.Format(
        System.Globalization.CultureInfo.InvariantCulture,
        "{0}{1}{2}",
        libgitNativeBinaryPath,
        Path.PathSeparator,
        currentPaths);

    Environment.SetEnvironmentVariable(pathEnvVariable, combinedPaths);
}

Saving that as "C# Program" you can start right away:

void Main()
{
    SetUpNativeBinaries();
    var repoWorkingDir = @"C:\temp\libgit2_repo";
    var repo = new Repository(repoWorkingDir);

    repo.Config.Dump();
    repo.Branches.Dump();
    repo.Index.RetrieveStatus().Dump();
    //...
}

Update There is a closed issue and the fix is shipped with since release 0.14.1.0