Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

avoid using go/build to trim prefix #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

benesch
Copy link
Contributor

@benesch benesch commented Jun 5, 2017

go/build will return gopath/goroot paths for the machine running it, which isn't what you want --
the path being stripped are from the build machine. a dumber approach, just looking for /src/ does
almost the same thing without using go/build.

Submitted on behalf of @dt.

go/build will return gopath/goroot paths for the machine running it, which isn't what you want --
the path being stripped are from the build machine. a dumber approach, just looking for /src/ does
almost the same thing without using go/build.
@mattrobenolt
Copy link
Contributor

Is this guaranteed to be true? I feel like there'd be cases where "/src/" doesn't exist in the path.

@dt
Copy link

dt commented Jun 5, 2017

hm pretty sure anything that was in GOPATH will have src: https://github.com/golang/go/wiki/GOPATH#directory-layout

if you somehow got Go to build something that wasn't in GOPATH, well then trimming GOPATH off it wouldn't work anyway.

That said, either way, the current code using go/build is getting the executing machine's values for GOPATH / GOROOT, not the ones from the machine used to build and thus that are in the stacks (except in the special case of running on the build machine), so I think even with its limitations, this naive heuristic is hopefully still a bit more reliable/portable than using go/build.

prefix += string(filepath.Separator)
}
trimPaths = append(trimPaths, prefix)
const src = "/src/"
Copy link

Choose a reason for hiding this comment

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

should this use os.PathSeparator (or filepath.Join, equivalently) instead of hardcoding /?

Copy link
Contributor Author

@benesch benesch Jun 5, 2017

Choose a reason for hiding this comment

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

Welp, what if you're running a binary cross-compiled on Linux for Windows? My point is that I think neither the current behavior nor your proposed alternative is correct. Perhaps we need to look for both /src/ and \src\ on all platforms?

Copy link

Choose a reason for hiding this comment

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

Indeed. In fact, we're completely speculating on this behaviour at the moment. We probably need to experiment (or find the code) to find out how source locations are encoded into Go binaries. See golang/go#19784, for instance, which implies some confusion in this area.

Copy link

@dt dt Jun 5, 2017

Choose a reason for hiding this comment

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

that said, even if /src/ doesn't work for some platform or cross-compilation combinations, it should still probably do better than go/build's user-specific paths, right? so does it make sense to call this an incremental improvement even if there's potentially further we could go?

Copy link

Choose a reason for hiding this comment

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

Indeed. @mattrobenolt?

var trimPaths []string

// Try to trim the GOROOT or GOPATH prefix off of a filename
// trimPath tries to trim the GOROOT or GOPATH prefix off of a filename.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment is now irrelevant, right? Since we don't look at these values at all.

@benesch
Copy link
Contributor Author

benesch commented Aug 4, 2017

Actually, thinking about this more, I think this patch makes things worse. Trimming from the last /src/ is going to break whenever a Go package uses an internal src directory, e.g. github.com/cockroachdb/cockroach/src/.

Seems to me we should just avoid trimming entirely. If users want to trim, they can do so with GOROOT_FINAL and/or

go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH

(I'm not necessarily suggesting that the official go-raven library default to no-trimming, but would be nice to have it exposed as an option. Thoughts, @dt @tamird?)

@tamird
Copy link

tamird commented Aug 4, 2017

TIL about trimpath; indeed, path trimming is fraught. It's too bad you can't ask for the GOROOT/GOPATH that were built into the binary at runtime. +1 to removing this functionality altogether.

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

Successfully merging this pull request may close these issues.

None yet

4 participants