Closed jfrech closed 1 month ago
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Change https://go.dev/cl/603097 mentions this issue: cmd/compile/internal/types2: change inference error message
Thank you for the report.
What is happening is that types
was formatting an error message using [link]:
err.addf(posn, "cannot infer %s (%v)", obj.name, obj.pos)
cmd/go rewrites the output of gc in Shell.reportCmd. Excerpt from internal documentation:
// reportCmd formats the output as "# desc" followed by the given output. The // output is expected to contain references to 'dir', usually the source // directory for the package that has failed to build. reportCmd rewrites // mentions of dir with a relative path to dir when the relative path is // shorter. This is usually more pleasant.
When this happens is decided by replacePrefix. Docs:
// replacePrefix is like strings.ReplaceAll, but only replaces instances of old // that are preceded by ' ', '\t', or appear at the beginning of a line.
This manifests as rewriting the output /tmp/leaks.go:6:3: cannot infer S (/tmp/leaks.go:3:8)
to ./leaks.go:6:3: cannot infer S (/tmp/leaks.go:3:8)
. The first instance begins a line and matches. The second is preceded by a (
so it does not match.
I think we should be consistent with the output here and have a relative path in both locations, e.g. ./leaks.go:6:3: cannot infer S (declared on ./leaks.go:3:8)
. This is being done for convenience, and we should try to get this consistent on a best effort basis. We can fix these as they are reported for now. I don't think there is a need for a more systematic review yet.
@timothy-king I was wondering why play.go.dev didn't exhibit the same. Thanks for the writeup.
Cf. #68737
Go version
go version go1.22.3 linux/amd64
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
In its error message, the compiler leaks the absolute path of "./leaks.go".
What did you expect to see?
I would have expected the error's source's source path to be presented relative to the current working directory (or relative to the current module's root). Such is the case on the playground: https://go.dev/play/p/890kN4mzITD