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

GitException: An object of type commit could not be located at offset 60529 #797

Closed
Frankenleg opened this issue Aug 22, 2022 · 19 comments · Fixed by #816
Closed

GitException: An object of type commit could not be located at offset 60529 #797

Frankenleg opened this issue Aug 22, 2022 · 19 comments · Fixed by #816
Assignees
Milestone

Comments

@Frankenleg
Copy link

Frankenleg commented Aug 22, 2022

Nerdbank GitVersioning has been working for a couple of years with Gitlab (On-site hosted).
We recently upgraded our .net projects from aspnet core 3.2 to .net 6. and Gitlab has not changed, but we are now seeing issues compiling.

We now see an exclamation icon in Visual Studio 2022 under the dependencies section for NerdBank.GitVersioning (3.5.109)
We also get the following error when trying to compile:

Error MSB4018: The "Nerdbank.GitVersioning.Tasks.GetBuildVersion" task failed unexpectedly.
Nerdbank.GitVersioning.GitException: An object of type commit could not be located at offset 60529.
   at Nerdbank.GitVersioning.ManagedGit.GitPackReader.GetObject(GitPack pack, Stream stream, Int64 offset, String objectType, GitPackObjectType packObjectType)
   at Nerdbank.GitVersioning.ManagedGit.GitPack.GetObject(Int64 offset, String objectType)
   at Nerdbank.GitVersioning.ManagedGit.GitPack.TryGetObject(GitObjectId objectId, String objectType, Stream& value)
   at Nerdbank.GitVersioning.ManagedGit.GitRepository.TryGetObjectBySha(GitObjectId sha, String objectType, Stream& value)
   at Nerdbank.GitVersioning.ManagedGit.GitRepository.GetObjectBySha(GitObjectId sha, String objectType)
   at Nerdbank.GitVersioning.ManagedGit.GitRepository.GetCommit(GitObjectId sha, Boolean readAuthor)
   at Nerdbank.GitVersioning.Managed.GitExtensions.<GetCommitHeight>g__TryCalculateHeight|4_0(GitCommit commit, <>c__DisplayClass4_0& )
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetCommitHeight(GitRepository repository, GitCommit startingCommit, GitWalkTracker tracker, Func`2 continueStepping)
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetHeight(ManagedGitContext context, Func`2 continueStepping)
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetVersionHeight(ManagedGitContext context, Version baseVersion)
   at Nerdbank.GitVersioning.Managed.ManagedGitContext.CalculateVersionHeight(VersionOptions committedVersion, VersionOptions workingVersion)
   at Nerdbank.GitVersioning.VersionOracle..ctor(GitContext context, ICloudBuild cloudBuild, Nullable`1 overrideVersionHeightOffset)
   at Nerdbank.GitVersioning.Tasks.GetBuildVersion.ExecuteInner()
   at MSBuildExtensionTask.ContextAwareTask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() (18, 6)
@AArnott
Copy link
Collaborator

AArnott commented Aug 22, 2022

Thanks for reporting.

Can you try setting the NBGV_GitEngine environment variable to LibGit2 to resolve your compile break?
The issue under the dependencies node is a bug in Visual Studio. I think we have an active issue tracking that but I can't find it right now.

@Frankenleg
Copy link
Author

Yes! That worked. Added it as a Windows System Environment Variable and it now compiles again. Thank you so much! It would be really nice if it was an optional json variable so it could be stored in source code.

@AArnott
Copy link
Collaborator

AArnott commented Aug 23, 2022

@Frankenleg Thanks for the follow-up. The env var I gave you is a workaround that utilizes the libgit2 engine instead of our own faster all-managed one. We would like to fix the managed one to work for repos like yours. Is your repo OSS by chance? Any other idea of why your repo may be building its git database in an unusual way?
@qmfrederik FYI

@AArnott AArnott changed the title .net 6 compile issue GitException: An object of type commit could not be located at offset 60529 Aug 23, 2022
@AArnott AArnott reopened this Aug 23, 2022
@KalleOlaviNiemitalo
Copy link

NBGV_GitEngine can be set in Directory.Build.props, right? Judging from this

@AArnott
Copy link
Collaborator

AArnott commented Aug 23, 2022

@KalleOlaviNiemitalo Yes, I think so. Although that would only impact Nerdbank.GitVersioning as it impacts the build. Using the nbgv CLI tool would not regard this msbuild property though, and only the environment variable would be effective.

@Frankenleg
Copy link
Author

I work for a national laboratory and they host an on-site instance of GitLab. Our source code cannot reside offsite or in the public domain. I believe it is the enterprise version of GitLab, so not sure why it would be building the git database in an unusual way.

@AArnott
Copy link
Collaborator

AArnott commented Aug 24, 2022

Then I imagine it's just a newer git version that changed the format, or perhaps a format that was always there but is unusual so our managed git implementation didn't need to support it until now. Hopefully @qmfrederik (who wrote our managed git implementation) can investigate.

@Frankenleg
Copy link
Author

@Frankenleg Thanks for the follow-up. The env var I gave you is a workaround that utilizes the libgit2 engine instead of our own faster all-managed one. We would like to fix the managed one to work for repos like yours. Is your repo OSS by chance? Any other idea of why your repo may be building its git database in an unusual way? @qmfrederik FYI

Just curios why this worked fine with the existing git repo in the previos version of Visual Studio 2019 and ,net core 3.2

@AArnott
Copy link
Collaborator

AArnott commented Aug 26, 2022

The version of VS you're using is probably irrelevant, and what you do within the repo is certainly irrelevant. It's all about the git.exe you used to clone and maintain the repo, and perhaps the git operations you perform within that repo. I couldn't say why one clone you have works and another doesn't other than to say that the git db has multiple ways to store objects (e.g. loose files vs. pack files) and only one of these ways is causing the trouble.

@marcin-krystianc
Copy link
Contributor

Hi, we've encountered a somewhat similar issue. Our error is:

2022-08-24 07:51:57.610 [ERROR] [MsBuild] PrivateP2PCaching.proj: C:\Users\builder-prod-pool-2\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018: The "Nerdbank.GitVersioning.Tasks.GetBuildVersion" task failed unexpectedly.
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018: System.InvalidOperationException: Unable to get version from commit: e7e021712bf243258ed66fc0a0bd505ea6f5ab49
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:  ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.ManagedGit.GitTreeStreamingReader.FindNode(Stream stream, ReadOnlySpan`1 name)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.ManagedGit.GitRepository.GetTreeEntry(GitObjectId treeId, ReadOnlySpan`1 nodeName)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.ManagedVersionFile.GetVersion(GitCommit commit, String repoRelativeProjectDirectory, Dictionary`2 blobVersionCache, String& actualDirectory)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.GitWalkTracker.GetVersion(GitCommit commit)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    --- End of inner exception stack trace ---
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.GitWalkTracker.GetVersion(GitCommit commit)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.CommitMatchesVersion(GitCommit commit, SemanticVersion expectedVersion, Position comparisonPrecision, GitWalkTracker tracker)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.<>c__DisplayClass1_0.<GetVersionHeight>b__0(GitCommit c)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.<GetCommitHeight>g__TryCalculateHeight|4_0(GitCommit commit, <>c__DisplayClass4_0& )
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.GetCommitHeight(GitRepository repository, GitCommit startingCommit, GitWalkTracker tracker, Func`2 continueStepping)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.GetHeight(ManagedGitContext context, Func`2 continueStepping)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.GitExtensions.GetVersionHeight(ManagedGitContext context, Version baseVersion)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Managed.ManagedGitContext.CalculateVersionHeight(VersionOptions committedVersion, VersionOptions workingVersion)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.VersionOracle..ctor(GitContext context, ICloudBuild cloudBuild, Nullable`1 overrideVersionHeightOffset)
2022-08-24 07:51:57.610 [ERROR] [MsBuild] C:\Users\...\AppData\Roaming\Nuget\packages\nerdbank.gitversioning\3.5.108\build\Nerdbank.GitVersioning.Inner.targets(17,5): error MSB4018:    at Nerdbank.GitVersioning.Tasks.GetBuildVersion.ExecuteInner()
2022-08-24 07:51:57.610 [ERROR] [MsBuild] >    <Nerdbank.GitVersioning.Tasks.GetBuildVersion
2022-08-24 07:51:57.610 [ERROR] [MsBuild] >    ^
2022-08-24 07:51:57.814 [ERROR] [MsBuild] PrivateP2PCaching.proj (no warnings, 1 error) FAILED TO BUILD in 0.3s
2022-08-24 07:51:57.814 [ERROR] [MsBuild] ... FAILED TO BUILD in 0.8s

Unfortunately, our repository is not OSS and we cannot share it. What we've been able to confirm so far:

  • libgit2 engine works but managed git implementation doesn't
  • running git gc "fixes" the problem

@AArnott I can try to attach a debugger and analyze the problem, but I don't know what to look for. Can you give some hints, what to check when the error occurs?

@Frankenleg
Copy link
Author

The version of VS you're using is probably irrelevant, and what you do within the repo is certainly irrelevant. It's all about the git.exe you used to clone and maintain the repo, and perhaps the git operations you perform within that repo. I couldn't say why one clone you have works and another doesn't other than to say that the git db has multiple ways to store objects (e.g. loose files vs. pack files) and only one of these ways is causing the trouble.

Update: Issue resolved
I had other team members were not having to add the environment variable to solve this issue. It looked like it was something on my machine. I did two things. I updated git to use the latest version (currently v2.37.3) and I deleted my local repository and cloned the remote for a fresh copy. The issue I was having went away. It must have been something funky in my local repo. Thanks again for the help.

@AArnott
Copy link
Collaborator

AArnott commented Sep 6, 2022

Thanks for the update. Maybe your repo was corrupted. But it's still possible that in working with your repo that an object is created that we can't parse (but git.exe can). If this re-surfaces for you again, please let us know.

@AArnott AArnott closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2022
@marcin-krystianc
Copy link
Contributor

marcin-krystianc commented Sep 19, 2022

Hi @AArnott,

I've managed to reproduce the problem by building a git repo with a synthetic commit history. It appears to me that managed engine has some problems with reading data from pack streams, but I didn't get to the bottom of the problem yet. I can see in the debugger, that occasionally the read operation returns fewer bytes than requestd (read != toRead) in

The "Nerdbank.GitVersioning.Tasks.GetBuildVersion" task failed unexpectedly. [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
System.InvalidOperationException: Unable to get version from commit: 8136a6e4a3243b30b5e13878fa4916e2d624234f [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
 ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.ManagedGit.GitTreeStreamingReader.FindNode(Stream stream, ReadOnlySpan`1 name) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.ManagedGit.GitRepository.GetTreeEntry(GitObjectId treeId, ReadOnlySpan`1 nodeName) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.ManagedVersionFile.GetVersion(GitCommit commit, String repoRelativeProjectDirectory, Dictionary`2 blobVersionCache, String& actualDirectory) [D:\workspace\CodeBin\global-packa
ges\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.GitWalkTracker.GetVersion(GitCommit commit) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   --- End of inner exception stack trace --- [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.GitWalkTracker.GetVersion(GitCommit commit) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.CommitMatchesVersion(GitCommit commit, SemanticVersion expectedVersion, Position comparisonPrecision, GitWalkTracker tracker) [D:\workspace\CodeBin\global-packag
es\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.<>c__DisplayClass1_0.<GetVersionHeight>b__0(GitCommit c) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.<GetCommitHeight>g__TryCalculateHeight|4_0(GitCommit commit, <>c__DisplayClass4_0&) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\Pr
ivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetCommitHeight(GitRepository repository, GitCommit startingCommit, GitWalkTracker tracker, Func`2 continueStepping) [D:\workspace\CodeBin\global-packages\nerdba
nk.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetHeight(ManagedGitContext context, Func`2 continueStepping) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.Managed.GitExtensions.GetVersionHeight(ManagedGitContext context, Version baseVersion) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.pro
j]
   at Nerdbank.GitVersioning.Managed.ManagedGitContext.CalculateVersionHeight(VersionOptions committedVersion, VersionOptions workingVersion) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\b
uild\PrivateP2PCaching.proj]
   at Nerdbank.GitVersioning.VersionOracle..ctor(GitContext context, ICloudBuild cloudBuild, Nullable`1 overrideVersionHeightOffset) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\Priv
ateP2PCaching.proj]
   at Nerdbank.GitVersioning.Tasks.GetBuildVersion.ExecuteInner() [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.pr
oj]
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) [D:\workspace\CodeBin\global-packages\nerdbank.gitversioning\3.5.109\build\PrivateP2PCaching.proj]
    0 Warning(s)
    1 Error(s)

@KalleOlaviNiemitalo
Copy link

Might be related to https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams in ZLibStream used by GitPackReader.

@marcin-krystianc
Copy link
Contributor

Might be related to https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams in ZLibStream used by GitPackReader.

Thanks for that link, it seems that explains the problem.

@AArnott
Copy link
Collaborator

AArnott commented Sep 19, 2022

that occasionally the read operation returns fewer bytes than requestd

That's a very common mistake in using Stream APIs. Streams are obliged to only return 0 bytes when at their end. But they are always allowed to return non-zero bytes, including less than the full length of the supplied buffer. Code that assumes the buffer will be filled is buggy.
Thank you for the repro and the investigation. It looks like you may have found the bug. If someone gets a fix written for it before me, wonderful. Otherwise I'll try to get a fix out in the next few days.

@AArnott
Copy link
Collaborator

AArnott commented Sep 19, 2022

Your test .zip doesn't repro the problem for me. But if it's a Stream.Read problem as you say, that's based more on buffer sizes and race conditions than anything specific in a repo. So I'll look at the Stream.Read issue anyway.

@AArnott
Copy link
Collaborator

AArnott commented Sep 20, 2022

Can someone with a repro please try 3.6.42-alpha-g47b0d281e4 of the nuget package? You can find this version in our CI feed as described in our readme.

@marcin-krystianc
Copy link
Contributor

marcin-krystianc commented Sep 20, 2022

Your test .zip doesn't repro the problem for me. But if it's a Stream.Read problem as you say, that's based more on buffer sizes and race conditions than anything specific in a repo. So I'll look at the Stream.Read issue anyway.
Can someone with a repro please try 3.6.42-alpha-g47b0d281e4 of the nuget package? You can find this version in our CI feed as described in our readme.

I've used the zip file that I previously shared. It was able to reproduce the problem with version 3.5.109, but 3.6.42-alpha-g47b0d281e4 seems to be working fine.

PS: I'm using dotnet 7.0.100-preview.6.22352.1 for my tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants