importer.Default() and GOROOT

68 views
Skip to first unread message

quas...@gmail.com

unread,
Oct 18, 2021, 12:29:12 PM10/18/21
to golang-tools

Hi friends.

I was struggling to use `go/importer` related things for quite a while now, but I had some kludges that worked. Until today.

When we compile a release binary, it looks like build context like GOROOT is saved there. If we run a binary on a system that doesn't override a GOROOT via env var, saved GOROOT will be used (from the system that was used to compile a binary). This makes importer created by `importer.Default()` (and other importers really) fail to import/locate stdlib package. How can I fix that so importers use the host GOROOT that can be discovered by `go env GOROOT` instead of the one I have on a system used to build the tool?

Some more context:
* The underlying issue comes from the ruleguard project
* Here is the code that is used to do the "importing" https://github.com/quasilyte/go-ruleguard/blob/master/ruleguard/importer.go

I found no way to get an importer object from the `go/analysis` framework, so I had to create an analyzer importer object to circumvent that. Maybe I can try to use `go/packages` there, but I really just need an `.Import()` function that would give the types information (`types.Package` and filled `types.Info`).

I will gladly share more info. It's hard to decide which info would be relevant and what would only add unnecessary noise for this.
Reply all
Reply to author
Forward
0 new messages