Skip to content

Conversation

@codeaucafe
Copy link
Contributor

@codeaucafe codeaucafe commented Apr 14, 2025

Description

Fixes #3986

Problem

Prior to this change, the Delve debugger's version information (via dlv version command) displays the blob hash of pkg/version/version.go as the a Git ident identifier $Id$. Upon reviewing the code a handful of times I wonder if this is intended, given we search for gitrevision key and try to write its value to Version.Build. From this logic in buildInfoFixBuild I would assume we would want the latest commit hash dlv is built with and not the file blob hash of pkg/version/version.go. That, and many users (myself included) originally though Build should/would represent the commit hash delve was installed/built with (thus the reason for my going down this rabbit hole surmising why its done this way)

If this should in fact be the built git hash then buildInfoFixBuild func in fixbuild.go needs to be updated to search for the git hash in vcs.revision key's value rather than the current implementation searching for gitrevision.

Solution

Add settings search for vcs.revision and if it doesn't find it, we fall back to searching for gitrevision to maintain backwards compatibility (I couldn't determine where that was used previously, but maybe a linker flag from awhile ago?)

Note

If we do want to instead keep the Build Id as the blob hash of pkg/verison/version.go then I'm happy to close this PR, but the semantics of Build generally mean the commit hash the binary was installed/built with.

@codeaucafe codeaucafe changed the title Improve Delve Build Version Identification version: resolve persistent hardcoded build identifier issue Apr 14, 2025
@codeaucafe codeaucafe force-pushed the codeaucafe/3986/fix-build-git-commit-hash branch 2 times, most recently from 3031118 to ce85e17 Compare April 15, 2025 05:44
@codeaucafe
Copy link
Contributor Author

@aarzilli I made changes. I realized the issue was the fact that we were trying to set a struct's field with linker flags doesn't work. Linker flags only work with package-level fields golang/go#26042 (comment)

}
}

// If we didn't find vcs.revision, try the old key for backward compatibility
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving as backwards compatibility fall back

@aarzilli
Copy link
Member

@aarzilli I made changes. I realized the issue was the fact that we were trying to set a struct's field with linker flags doesn't work. Linker flags only work with package-level fields golang/go#26042 (comment)

No we are not setting a struct field with a linker flag. We are setting main.Build with a linker flag which then is copied into a struct field in cmd/dlv/main.go.

@codeaucafe
Copy link
Contributor Author

sorry, I completely misread that when I was reading and debugging/testing it, let me remove and properly review, as well as look further into gitrevision just in case I missed something reading searching git lots/etc. It may just be adding vcs.revision check instead resolves it.

@codeaucafe codeaucafe force-pushed the codeaucafe/3986/fix-build-git-commit-hash branch from ce85e17 to 3faaebd Compare April 17, 2025 05:13
@codeaucafe
Copy link
Contributor Author

codeaucafe commented Apr 17, 2025

@aarzilli should be good to review now. From my further understanding, I believe the fix should simply be the additions I made in fixbuild.go's buildInfoFixBuild function. Please, note I updated the PR description as well since I finally realized the hash number I was wondering about was the blob hash of pkg/verison/version.go and not the actual built/installed binaries commit hash like one would assume should/would be the case with binary's build identifier.

I also updated issue #3986 accordingly too

@codeaucafe codeaucafe force-pushed the codeaucafe/3986/fix-build-git-commit-hash branch from 3faaebd to 6ad547c Compare April 17, 2025 07:01
@codeaucafe codeaucafe changed the title version: resolve persistent hardcoded build identifier issue version: use git hash instead of file blob hash for Version.Build Apr 17, 2025
@codeaucafe codeaucafe force-pushed the codeaucafe/3986/fix-build-git-commit-hash branch 2 times, most recently from f1a8e4f to 45d6689 Compare April 17, 2025 07:20
The build ID `dlv version`command is the blob hash
of file`pkg/version/version.go`. However, generally build identifier
semantics are the built/installed binaries git commit hash.

Updates buildInfoFixBuild to use the git commit has for Version.Build.
Go build info's git revision hash key is `vcs.revision`. So, I added
logic to search for that and attach its value to the version.Build
field. If nothing is found and haven't return its value, then we try
searching for `gitrevision` which maintains backwards compatibility.

Fixes go-delve#3986
@codeaucafe codeaucafe force-pushed the codeaucafe/3986/fix-build-git-commit-hash branch from 45d6689 to 13cc389 Compare April 17, 2025 07:25

func buildInfoFixBuild(v *Version) {
// Return if v.Build already set, but not if it is Git ident expand file blob hash
if !strings.HasPrefix(v.Build, "$Id: ") && !strings.HasSuffix(v.Build, " $") {
Copy link
Contributor Author

@codeaucafe codeaucafe Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically don't need this IF the $Id$ is removed but I think its best to keep as fallback/backwards compatibility

Copy link
Member

@aarzilli aarzilli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

var DelveVersion = Version{
Major: "1", Minor: "24", Patch: "2", Metadata: "",
Build: "$Id$",
Major: VersionMajor,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this change.

Copy link
Member

@derekparker derekparker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@derekparker derekparker merged commit 423b29b into go-delve:master Apr 22, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build ID uses version.go blob hash and not built commit hash

3 participants