juju/errors

File paths are not being trimmed when project is outside of GOPATH (vgo)

takeda opened this issue · 6 comments

in path.go there's a function trimGoPath() it relies on GOPATH variable to trim absolute paths when stacktrace is printed. With VGO the go source now can be outside of GOPATH, when that happens paths are no longer trimmed.

I could resolve the issue by setting srcDir to result of:

	_, file, _, _ := runtime.Caller(0)
	path := strings.TrimSuffix(file, "/main.go")

and have it working, unfortunately it is a private variable.

Perhaps there is a better way to get the path of the source code?

It is major problem since Go 1.11+ with modules official recommendation to not set GOPATH at all. Package owners, please state in what general direction you want it solved? I'd work on patch.

Ideas:

  • set trim path via linker -X errors.var="$PWD"
  • private atomic.Value + errors.SetTrimPath()

Maybe you know better way.

I would lean towards SetTrimPath as per your example, but I'll see if I can solicit further opinions.

@manadart does it look fine? https://github.com/temoto/errors/commit/cc113d5238214b20c2f33eaa5d442b86c5c5fb5d

Did not modify tests because I could not understand how they work. Multiple packages in same directory is confusing.

FYI this problem is (being) fixed systematically, in compiler:

The new go build flag -trimpath removes all file system paths from the compiled executable, to improve build reproducibility.
https://tip.golang.org/doc/go1.13#go-command

@temoto Open a PR with the change and we can take it from there.

I'm assuming that the PR fixes this.