GitTools / GitVersion

From git log to SemVer in no time
https://gitversion.net/docs/
MIT License
2.87k stars 654 forks source link

[Bug] Object reference not set to an instance of an object during versioning solving #2967

Closed rafek1241 closed 2 years ago

rafek1241 commented 2 years ago

Describe the bug Branch version solving does not works

Expected Behavior

Correct composition of current version

Actual Behavior

System.NullReferenceException: Object reference not set to an instance of an object.
   at GitVersion.GitObject..ctor(GitObject innerGitObject) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\GitObject.cs:line 12
   at GitVersion.Commit..ctor(Commit innerCommit) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\Commit.cs:line 12
   at GitVersion.GitRepository.<>c__DisplayClass27_0.<FindMergeBase>b__0() in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\GitRepository.cs:line 61
   at Polly.Policy`1.<>c__DisplayClass11_0.<Execute>b__0(Context ctx, CancellationToken ct)
   at Polly.Retry.RetryEngine.Implementation[TResult](Func`3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates`1 shouldRetryResultPredicates, Action`4 onRetry, Int32 permittedRetryCount, IEnumerable`1 sleepDurationsEnumerable, Func`4 sleepDurationProvider)
   at Polly.Retry.RetryPolicy`1.Implementation(Func`3 action, Context context, CancellationToken cancellationToken)
   at Polly.Policy`1.Execute(Func`3 action, Context context, CancellationToken cancellationToken)
   at Polly.Policy`1.Execute(Func`1 action)
   at GitVersion.Helpers.RetryAction`2.Execute(Func`1 operation) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Helpers\RetryAction.cs:line 35
   at GitVersion.GitRepository.FindMergeBase(ICommit commit, ICommit otherCommit) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\GitRepository.cs:line 59
   at GitVersion.RepositoryStore.FindMergeBase(IBranch branch, IBranch otherBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\RepositoryStore.cs:line 49
   at GitVersion.RepositoryStore.<>c__DisplayClass30_0.<GetMergeCommitsForBranch>b__2(IBranch otherBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\RepositoryStore.cs:line 454
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
   at System.Linq.OrderedEnumerable`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at GitVersion.RepositoryStore.GetMergeCommitsForBranch(IBranch branch, Config configuration, IEnumerable`1 excludedBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\RepositoryStore.cs:line 438
   at GitVersion.RepositoryStore.FindCommitBranchWasBranchedFrom(IBranch branch, Config configuration, IBranch[] excludedBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\RepositoryStore.cs:line 320
   at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches)
   at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 44
   at GitVersion.GitVersionContextFactory.Create(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionContextFactory.cs:line 39
   at GitVersion.GitVersionCoreModule.<>c__DisplayClass0_0.<RegisterTypes>b__1() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\GitVersionCoreModule.cs:line 37
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at GitVersion.VersionCalculation.NextVersionCalculator.get_context() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 15
   at GitVersion.VersionCalculation.NextVersionCalculator.FindVersion() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 30
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 51
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 66
  INFO [01/18/22 15:50:05:06] Attempting to show the current git graph (please include in issue):
  INFO [01/18/22 15:50:05:06] Showing max of 100 commits
  INFO [01/18/22 15:50:05:13] * 0e9c7d4 49 minutes ago  (HEAD -> feature/XXX, tag: 1.0.0, origin/feature/XXX)

Steps to Reproduce

I used command line execution in my project directory:

dotnet.exe C:\Users\Admin\.nuget\packages\gitversion.tool\5.8.1\tools\net6.0\any\gitversion.dll /nocache

I tried as well with:

dotnet.exe C:\Users\Admin\.nuget\packages\gitversion.tool\5.8.1\tools\netcoreapp3.1\any\gitversion.dll /nocache
dotnet.exe C:\Users\Admin\.nuget\packages\gitversion.tool\5.8.1\tools\net5.0\any\gitversion.dll /nocache
dotnet.exe C:\Users\Admin\.nuget\packages\gitversion.tool\5.6.7\tools\net5.0\any\gitversion.dll /nocache

Your Environment

project that runs .net core 3.1

Branch structure in my repository:

Rider: image

rafek1241 commented 2 years ago

Okay, rebase to master fixed the issue but it could be great to have such custom exception here to be more described.

asbjornu commented 2 years ago

If you are able to provide a public repository that reproduces this problem, it should be easy to fix. We don't want NullReferenceException to be thrown anywhere, as I completely agree they are obscure and unhelpful.

rafek1241 commented 2 years ago

@asbjornu It was my work repository. I will try to prepare some reproduction repository soon.

rafek1241 commented 2 years ago

@asbjornu Repository that reproduces the issue: https://github.com/rafek1241/gitversion-issue-2967-reproduction

davidkeaveny commented 2 years ago

I'm getting this issue with a private repo, so can help validate a fix if needed.

asbjornu commented 2 years ago

@davidkeaveny, can you please clone or fork GitVersion locally and run it against your repository to get a line numbered stack trace? If you're adventurous, it would be awesome if you found the source of the NRE and fixed it too.

asbjornu commented 2 years ago

I think I've managed to find the source of the issue you're experiencing, @rafek1241. As I'm not using Mainline myself, I'm not sure why this code is exactly as it is, but this line in RepositoryStore.GetMainlineBranches() is excluding all branches that don't have a MergeBase:

https://github.com/GitTools/GitVersion/blob/4d12ab0356d4211795874432ee57d190838993c4/src/GitVersion.Core/Core/RepositoryStore.cs#L297

And for the reproduction repository you've provided, no branches have a MergeBase, causing the returned list to become empty. What's worrying is that while commenting that line out still fails on your repository, it fails no tests. So our test coverage on Mainline is obviously not great. 😬

What I think could work here is for Mainline to defer to BaseVersionCalculator if it's unable to find a suitable Mainline branch.

asbjornu commented 2 years ago

I think I've solved this in #3018. I still need to add a test and it should be good to go. I'm not yet confident that I'm able to reproduce the problem in a test, though.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 5.8.3 :tada: The release is available on:

Your GitReleaseManager bot :package::rocket:

0000day commented 4 months ago

This just happend to me again. A rebase had no impact. I'm on a feature-branch that has been squashed down to 1 commit.

System.NullReferenceException: Object reference not set to an instance of an object.
   at LibGit2Sharp.Core.Handles.ObjectHandle.op_Implicit(ObjectHandle handle) in /_/LibGit2Sharp/Core/Handles/Objects.cs:line 509
   at LibGit2Sharp.Core.Proxy.git_commit_author(ObjectHandle obj) in /_/LibGit2Sharp/Core/Proxy.cs:line 289
   at LibGit2Sharp.Core.LazyGroup`1.Dependent`2.LibGit2Sharp.Core.LazyGroup<T>.IEvaluator<TInput>.Evaluate(TInput input) in /_/LibGit2Sharp/Core/LazyGroup.cs:line 88
   at LibGit2Sharp.Core.LazyGroup`1.<Evaluate>b__6_0(T input) in /_/LibGit2Sharp/Core/LazyGroup.cs:line 36
   at LibGit2Sharp.Core.GitObjectLazyGroup.EvaluateInternal(Action`1 evaluator) in /_/LibGit2Sharp/Core/GitObjectLazyGroup.cs:line 20
   at LibGit2Sharp.Core.LazyGroup`1.Evaluate() in /_/LibGit2Sharp/Core/LazyGroup.cs:line 34
   at LibGit2Sharp.Core.LazyGroup`1.Dependent`2.Evaluate() in /_/LibGit2Sharp/Core/LazyGroup.cs:line 80
   at LibGit2Sharp.Core.LazyGroup`1.Dependent`2.get_Value() in /_/LibGit2Sharp/Core/LazyGroup.cs:line 73
   at LibGit2Sharp.Commit.get_Committer() in /_/LibGit2Sharp/Commit.cs:line 87
   at GitVersion.Commit..ctor(Commit innerCommit) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\Commit.cs:line 17
   at GitVersion.Commit.<>c.<.ctor>b__3_0(Commit parent) in D:\a\GitVersion\GitVersion\src\GitVersion.LibGit2Sharp\Git\Commit.cs:line 16
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(Int32 recursions, IBranch targetBranch, BranchConfig branchConfiguration, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 77
   at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfigurationInternal(Int32 recursions, IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 54
   at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Configuration\BranchConfigurationCalculator.cs:line 28
   at GitVersion.VersionCalculation.BaseVersionCalculator.GetBaseVersion()
   at GitVersion.VersionCalculation.NextVersionCalculator.FindVersion() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\VersionCalculation\NextVersionCalculator.cs:line 56
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line [52](_gitlab_url_masked_/-/jobs/4983096#L52)
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 66
  INFO [07/17/24 9:05:56:63] Attempting to show the current git graph (please include in issue): 
  INFO [07/17/24 9:05:56:63] Showing max of 100 commits
  ....

It has been executed on a gitlab-runner. Will get another machine to test just to be sure the issue is not local.

I'm on 5.12.0

arturcic commented 4 months ago

@0000day try the latest beta

0000day commented 4 months ago

Latest beta had too many changes for me. Investigated it on v5.12.0. The issue was that we moved over to Gitlab. Looks like gitlab is doing a shallow-copy of the repository.

before_script:
  - git fetch --unshallow --all

This fixed it for me, maybe it's a help for others.