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

fix: handle builds with trimpath enabled #120

Merged
merged 4 commits into from
Feb 8, 2025
Merged

fix: handle builds with trimpath enabled #120

merged 4 commits into from
Feb 8, 2025

Conversation

gkampitakis
Copy link
Owner

Closing #118 🤞

snaps/utils.go Outdated
func trimPathBuild() bool {
goFlags := strings.Split(os.Getenv("GOFLAGS"), " ")

return slices.Contains(goFlags, "-trimpath")
Copy link
Contributor

Choose a reason for hiding this comment

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

Golang accepts both -trimpath and --trimpath - and I know that due to old habits I tend to use -- interactively. Maybe it would be worth checking for both.

Copy link
Owner Author

Choose a reason for hiding this comment

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

ohhhh thanks for pointing this out 🙇 . Btw not sure but I can't find a "good" way to identify if build is trimpath when you run your tests like this

go test -trimpath ./...

if you have any ideas feel free to share them 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Checking the $GOFLAGS is definitely more of an idea than I could up so far. My best idea was to go rooting around in the golang codebase in the search of the trimpath implementation and taking a look if it produces a side effect that one could measure. So far I figured out that the BuildTrimpath is relevant, but I haven't looked through the occurrences yet: https://github.com/search?q=repo%3Agolang/go%20BuildTrimpath&type=code

Copy link
Contributor

Choose a reason for hiding this comment

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

One thing I did turn up: golang collects information about the build, and among the information recorded is if trimpath was enabled: https://github.com/golang/go/blob/e81f7155154c0f5d40363e84a8f24a5b559b5eed/src/cmd/go/internal/load/pkg.go#L2428

This recorded BuildSetting is then exposed to us with the runtime/debug package.

A trivial test looks like this:

package main

import (
        "runtime/debug"
        "fmt"
)

func main() {
        bi, ok := debug.ReadBuildInfo()
        if ! ok {
                fmt.Println("failed to fetch build info")
                return
        }
        fmt.Println(bi.Settings)
}

The result then is as following:

kandre@W-PF4PY6FH(pts/5) ~/go/src/test % go run main.go                                                                                               
[{-buildmode exe} {-compiler gc} {CGO_ENABLED 1} {CGO_CFLAGS } {CGO_CPPFLAGS } {CGO_CXXFLAGS } {CGO_LDFLAGS } {GOARCH amd64} {GOOS linux} {GOAMD64 v1}]

kandre@W-PF4PY6FH(pts/5) ~/go/src/test % GOFLAGS="" go run main.go
[{-buildmode exe} {-compiler gc} {CGO_ENABLED 1} {CGO_CFLAGS } {CGO_CPPFLAGS } {CGO_CXXFLAGS } {CGO_LDFLAGS } {GOARCH amd64} {GOOS linux} {GOAMD64 v1}]

kandre@W-PF4PY6FH(pts/5) ~/go/src/test % GOFLAGS="-trimpath" go run main.go
[{-buildmode exe} {-compiler gc} {-trimpath true} {CGO_ENABLED 1} {GOARCH amd64} {GOOS linux} {GOAMD64 v1}]

kandre@W-PF4PY6FH(pts/5) ~/go/src/test % go run -trimpath main.go
[{-buildmode exe} {-compiler gc} {-trimpath true} {CGO_ENABLED 1} {GOARCH amd64} {GOOS linux} {GOAMD64 v1}]

Copy link
Owner Author

Choose a reason for hiding this comment

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

I found this but unfortunately this information isn't always captured.

The previous commit on this pr was using the ReadBuildInfo, but when I tried running unit tests with trimpath it was not showing any details which is weird😞
7929c46 (#120)

This is what ReadBuildInfo doc states

// ReadBuildInfo returns the build information embedded
// in the running binary. The information is available only
// in binaries built with module support.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm . I think if build info is present it would than still be the best way to use it, falling back to checking the env var.

I wonder what is causing that - does your go env point at anything specific? I'll have a try when I get some time near a proper PC.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I have updated the function and on my testing

  1. captures all cases, GOFLAGS, flag trimpath either from a main_test or package
  2. don't see any regression on what already exists.

@gkampitakis gkampitakis marked this pull request as ready for review February 1, 2025 10:54
Co-authored-by: Andre Klärner <kandre+github@ak-online.be>
@gkampitakis gkampitakis merged commit 8740883 into main Feb 8, 2025
4 checks passed
@gkampitakis gkampitakis deleted the trim_path branch February 8, 2025 08:44
@klaernie
Copy link
Contributor

klaernie commented Feb 8, 2025

Thanks for all the work you put in and for getting a solution so swiftly!

@gkampitakis
Copy link
Owner Author

Thank you for the reproduction and the clear explanation of the issue 🙇

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.

2 participants