Nerdbank.gitversioning: GetBuildVersion fails with NullReferenceException when git clone depth is 1

Created on 3 Jan 2020  路  6Comments  路  Source: dotnet/Nerdbank.GitVersioning

For reproduction purpose consider this commit: https://github.com/c80k/capnproto-dotnetcore/commit/5208531ef2f2abc3612e9116449c702f25dd370e
which seems to fix the problem.

Background: The project is built inside an AppVeyor CI. Without the former commit, you'll see something like this in the log:

C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018: The "Nerdbank.GitVersioning.Tasks.GetBuildVersion" task failed unexpectedly. [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018: System.NullReferenceException: Object reference not set to an instance of an object. [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at LibGit2Sharp.Core.Proxy.git_commit_tree_id(ObjectHandle obj) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at LibGit2Sharp.Commit.<>c__DisplayClass11_0.<.ctor>b__0(ObjectHandle obj) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at LibGit2Sharp.Core.GitObjectLazyGroup.<>c__DisplayClass3_0`1.<Singleton>b__0() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at System.Lazy`1.CreateValue() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at System.Lazy`1.LazyInitValue() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at System.Lazy`1.get_Value() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at LibGit2Sharp.Core.LazyGroup`1.LazyWrapper`1.LibGit2Sharp.Core.ILazy<TType>.get_Value() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at LibGit2Sharp.Commit.get_Tree() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.VersionFile.GetVersion(Commit commit, String repoRelativeProjectDirectory) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.CommitMatchesVersion(Commit commit, SemanticVersion expectedVersion, Position comparisonPrecision, String repoRelativeProjectDirectory) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.<>c__DisplayClass3_0.<GetVersionHeight>b__0(Commit c) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.GetCommitHeight(Commit commit, Dictionary`2 heights, Func`2 continueStepping) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.<>c__DisplayClass30_0.<GetCommitHeight>b__0(Commit p) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at System.Linq.Enumerable.Max(IEnumerable`1 source) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.GetCommitHeight(Commit commit, Dictionary`2 heights, Func`2 continueStepping) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.GetHeight(Commit commit, Func`2 continueStepping) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.GitExtensions.GetVersionHeight(Commit commit, String repoRelativeProjectDirectory, Version baseVersion) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.VersionOracle.CalculateVersionHeight(String relativeRepoProjectDirectory, Commit headCommit, VersionOptions committedVersion, VersionOptions workingVersion) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.VersionOracle..ctor(String projectDirectory, Repository repo, Commit head, ICloudBuild cloudBuild, Nullable`1 overrideVersionHeightOffset, String projectPathRelativeToGitRepoRoot) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.VersionOracle.Create(String projectDirectory, String gitRepoDirectory, ICloudBuild cloudBuild, Nullable`1 overrideBuildNumberOffset, String projectPathRelativeToGitRepoRoot) [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Nerdbank.GitVersioning.Tasks.GetBuildVersion.ExecuteInner() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at MSBuildExtensionTask.ContextAwareTask.Execute() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
C:\Users\appveyor\.nuget\packages\nerdbank.gitversioning\3.0.28\build\Nerdbank.GitVersioning.targets(71,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj]
Done Building Project "C:\projects\capnproto-dotnetcore\Capnp.Net.Runtime\Capnp.Net.Runtime.csproj" (Build target(s)) -- FAILED.

The fix just removes the clone_depth property from appveyor.yml.

I understand that cloning the complete repository (i.e. not limiting the clone depth) is essential for calculating the correct version info. My problem was that I totally forgot about the clone depth restriction being set, and that I had a hard time figuring out that this was the actual cause. A meaningful error message instead of letting GetBuildVersion fail unexpectedly would be nice. :-)

Most helpful comment

Just a +1 - experienced a similar issue with actions/checkout@v2 with default parameters. For reference, this was the fix:

      - name: Checkout source
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

All 6 comments

Agreed. I'll see what we can do to improve the error message here.

Just a +1 - experienced a similar issue with actions/checkout@v2 with default parameters. For reference, this was the fix:

      - name: Checkout source
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

Let's see if libgit2sharp can fix it: https://github.com/libgit2/libgit2sharp/issues/1775

Just a +1 - experienced a similar issue with actions/checkout@v2 with default parameters. For reference, this was the fix:

      - name: Checkout source
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

Just to note, setting fetch-depth: 0 will fetch the full git history during CI, which, for large repositories, can take quite some time.

If you change your minor version at least sometimes, then setting this to a reasonable restricted value, like 100 or 1000 (depending on your minor versioning frequency), will also work.

I've experienced different, but related error caused by fetch-depth of 1 (default of actions/checkout@v2), that was fixed by set fetch-depth to 0.

Related build log info:

/home/runner/.nuget/packages/nerdbank.gitversioning/3.3.37/build/Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018: The "Nerdbank.GitVersioning.Tasks.GetBuildVersion" task failed unexpectedly.
/home/runner/.nuget/packages/nerdbank.gitversioning/3.3.37/build/Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018: System.InvalidOperationException: Unable to get version from commit: db84405f6054a394ffd6a5e058c93d076760bab0
/home/runner/.nuget/packages/nerdbank.gitversioning/3.3.37/build/Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:  ---> System.NullReferenceException: Object reference not set to an instance of an object.
/home/runner/.nuget/packages/nerdbank.gitversioning/3.3.37/build/Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at LibGit2Sharp.Core.Handles.ObjectHandle.op_Implicit(ObjectHandle handle)

Full job run info here
Hope this help anyone else stumbling into this particular problem.

Duplicate of #92

Was this page helpful?
0 / 5 - 0 ratings