gomacro icon indicating copy to clipboard operation
gomacro copied to clipboard

Cannot import packages that export C symbols

Open cosmos72 opened this issue 6 years ago • 4 comments

The reason is: to download and scan packages, gomacro uses golang.org/x/tools/go/packages - which is unable to compute the type of C symbols and the value of C constants (for good reasons: you need a C compiler for that). Thus golang.org/x/tools/go/packages returns an error when trying to create the metadata of a package that exports C symbols, gomacro receives that error and bails out.

There is a possibile solution: to generate an import file, gomacro only needs the names of exported symbols, not their types or values (actually, knowing the exact values of untyped constants and the methods of interfaces would be useful too, but not absolutely required).

So, if golang.org/x/tools/go/packages successfully downloads a package, but fails to create its metadata, gomacro could locate the relevant sources inside $GOPATH/pkg/mod, parse them, collect the names of exported symbols, generate a (slightly reduced) import file and compile and load it normally as a plugin.

cosmos72 avatar Dec 21 '19 09:12 cosmos72

Glad you're looking into this. Is this relatively easy to implement?

Keithcat1 avatar Dec 21 '19 18:12 Keithcat1

Probably there is a simpler solution (to be tested):

execute one of the commands GO111MODULE=on go get PACKAGE/FULL/PATH or GO111MODULE=on go install PACKAGE/FULL/PATH before using golang.org/x/tools/go/packages to load the package metadata.

This has a non-zero chance to work because go get and go install invoke the C compiler for packages that import "C"

cosmos72 avatar Jan 09 '20 09:01 cosmos72

I tried: set GO111MODULE=on go install github.com/veandco/go-sdl2/sdl gomacro ... gomacro> import "github.com/veandco/go-sdl2/sdl" // debug: looking for package "github.com/veandco/go-sdl2/sdl" ... error loading package "github.com/veandco/go-sdl2/sdl" metadata: C:\py\go\pkg\mo d\github.com\veandco\[email protected]\sdl\audio.go:29:8: could not import C (no me tadata for C) ...

I think I mensioned this, but it works if I use :options Import.Uses.Module before importing the package that requires C.

You could always rip the code from CGO and use that to get the required info.

Keithcat1 avatar Jan 09 '20 15:01 Keithcat1

I'm also glad you are looking into this @cosmos72. The Gomacro REPL is already very useful and it's quite a pleasure to use. Being able to import packages that export C symbols would be a valuable addition to the use-cases for when Gomacro may be used.

mewmew avatar Mar 21 '20 22:03 mewmew