Skip to content

MsBuild, Native binaries and AppContext.BaseDirectory. #1498

Closed
@dazinator

Description

@dazinator

I am working on the GitVersion msbuild task. It's distrubuted as a nuget package. The nuget package contains a build of the task assembly for net461 and a build for netstandard1.5.
The corresponding libgit2sharp assembly (which our task depends on) and the /lib folder containing the native binaries that libgit2sharp requires, lives alongside the task assembly, in the nuget package.

When the nuget package is added to a project, and the project is built within VS - this uses the full desktop version of msbuild, and subsequently the net461 assembly containing the GitVersion build task is loaded from the nuget package, and the task runs successfully - LibGit2sharp all works smoothly.

However when dropping to the command line and running dotnet build (I am using version 2.0.2 of the dotnet sdk) this runs msbuild on netcoreapp and thus the netstandard1.5 task assembly from the nuget package is loaded. In this scenario the task msbuild fails and this is the stack trace:

       Using "GetVersion" task from the task factory "NuGetTaskRunnerFactory".
       Task "GetVersion"
         Resolved GitVersionTask, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null located in C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitVersionTask.dll and loaded from C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitVersionTask.dll.
         Resolved GitVersionCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null located in C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitVersionCore.dll and loaded from C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitVersionCore.dll.
         Resolved GitTools.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null located in C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitTools.Core.dll and loaded from C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\GitTools.Core.dll.
         Resolved LibGit2Sharp, Version=0.25.0.0, Culture=neutral, PublicKeyToken=7cbde695407f0333 located in C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\LibGit2Sharp.dll and loaded from C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\LibGit2Sharp.dll.
     1>MSBUILD : warning : WARN [10/24/17 20:59:04:58] Could not determine assembly version: System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-15e1193': The specified module could not be found. (Exception from HRESULT: 0x8007007E)\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods.LoadNativeLibrary()\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods..cctor()\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    --- End of inner exception stack trace ---\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods.git_buf_free(GitBuf buf)\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.Proxy.ConvertPath(Func`2 pathRetriever)\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Repository.Discover(String startingPath)\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at GitVersion.GitPreparer.GetDotGitDirectory() in C:\Users\Darrell\Source\Repos\GitVersion\src\GitVersionCore\GitPreparer.cs:line 139\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at GitVersion.ExecuteCore.ExecuteGitVersion(String targetUrl, String dynamicRepositoryLocation, Authentication authentication, String targetBranch, Boolean noFetch, String workingDirectory, String commitId, Config overrideConfig, Boolean noCache) in C:\Users\Darrell\Source\Repos\GitVersion\src\GitVersionCore\ExecuteCore.cs:line 32\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at GitVersion.ExecuteCore.TryGetVersion(String directory, VersionVariables& versionVariables, Boolean noFetch, Authentication authentication) in C:\Users\Darrell\Source\Repos\GitVersion\src\GitVersionCore\ExecuteCore.cs:line 81 [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
       Done executing task "GetVersion".

From the stack trace, it shows that the libgit2sharp assembly is being loaded from alongside the task assembly: C:\Users\Darrell\.nuget\packages\gitversiontask\0.0.1-alpha0003\build\netstandard1.5\LibGit2Sharp.dll

It also shows this:

System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-15e1193': The specified module could not be found. (Exception from HRESULT: 0x8007007E)\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()\r [C:\Users\Darrell\Documents\visual studio 2017\Projects\ClassLibrary7\ClassLibrary7.csproj]
MSBUILD : warning :    at LibGit2Sharp.Core.NativeMethods.LoadNativeLibrary()\r 

Yet alongside the libgit2sharp assembly I have confirmed that there is a lib folder containing all of the native binaries - including git2-15e1193.dll:

image

Any ideas? @bording ?

If you need to replicate, you can do so by taking my PR branch of gitversion, calling msbuild /t:Pack on the msbuild task project to produce a nuget package containing libgit2sharp, then adding that nuget package to a sample .csproj file that has an initialised .git repository with atleast 1 commit, and building.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions