yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

Can't use external libraries in the code: "unable to find source related to xxxx"

Open g3rzi opened this issue 5 years ago • 9 comments

I am trying to use Yaegi to run Golang code that contains 3rd party library called: github.com/tidwall/pretty

You wrote here https://github.com/containous/yaegi/issues/469 that it is possible:

It's also possible to re-build an interpreter containing a pre-compiled library and its wrapper (generated by goexports), so the package can be imported without the overhead of interpretation, as it is done for the standard library packages in yaegi.

But I didn't understand how can I re-build the interpreter with a pre-compiled library? Ho can I do it?

The following program sample.go triggers a panic:

package main

import (
	"github.com/containous/yaegi/interp"
	"github.com/containous/yaegi/stdlib"
)

const payload = `
package main

import (
	"fmt"
	"github.com/tidwall/pretty"
)

func main(){
	var example2 = "{\"name\": \"hello\"}"
	result := pretty.Pretty([]byte(example2))
	fmt.Println(string(result))
}
`

func main(){

	i := interp.New(interp.Options{})
	i.Use(stdlib.Symbols)

	_, err := i.Eval(payload)
	if err != nil {
		panic(err)
	}
}

Expected result:

{
  "name": "hello"
}

Got:

panic: 6:2: import "github.com/tidwall/pretty" error: unable to find source related to: "github.com/tidwall/pretty"

goroutine 1 [running]:
main.main()
	C:/Users/myuser/go/src/myGoProject/cmd/yaegi_check/check.go:42 +0xbf

g3rzi avatar May 27 '20 11:05 g3rzi

I tried to add the external library as you wrote:

_ "github.com/tidwall/pretty"

To:
https://github.com/containous/yaegi/blob/5d56bac8d0183baa5dcf2565605a72680dc652e8/interp/run.go#L5-L9

But I received the same error.

g3rzi avatar May 27 '20 12:05 g3rzi

I am trying to use Yaegi to run Golang code that contains 3rd party library called: github.com/tidwall/pretty

You wrote here #469 that it is possible:

It's also possible to re-build an interpreter containing a pre-compiled library and its wrapper (generated by goexports), so the package can be imported without the overhead of interpretation, as it is done for the standard library packages in yaegi.

But I didn't understand how can I re-build the interpreter with a pre-compiled library? Ho can I do it?

The tooling for pre-compiling only works for shipping the standard library for now, and not for third-party libraries yet. Support for third-party libraries is on the roadmap in the short to mid term.

The following program sample.go triggers a panic:

package main

import (
	"github.com/containous/yaegi/interp"
	"github.com/containous/yaegi/stdlib"
)

const payload = `
package main

import (
	"fmt"
	"github.com/tidwall/pretty"

	"log"
	"os/exec"
)

func main(){
	var example2 = "{\"name\": \"hello\"}"
	result := pretty.Pretty([]byte(example2))
	fmt.Println(string(result))
}
`

func main(){

	i := interp.New(interp.Options{})
	i.Use(stdlib.Symbols)

	_, err := i.Eval(payload)
	if err != nil {
		panic(err)
	}
}

Expected result:

{
  "name": "hello"
}

Got:

panic: 6:2: import "github.com/tidwall/pretty" error: unable to find source related to: "github.com/tidwall/pretty"

goroutine 1 [running]:
main.main()
	C:/Users/myuser/go/src/myGoProject/cmd/yaegi_check/check.go:42 +0xbf

You are probably getting that error because yaegi does not support go modules yet. Do you still get the error if you make sure to have "github.com/tidwall/pretty" in your GOPATH (or vendored in) ?

mpl avatar May 27 '20 16:05 mpl

I checked,

My GOATPH is:

GOPATH=C:\Users\user1\go

github.com/tidwall/pretty is inside C:\Users\user1\go\src.

I created a vendor folder and copy & paste the github.com/tidwall/pretty folder to the vendor folder and called it as is: github.com/tidwall/pretty.

But I received the same error:

panic: 6:2: import "github.com/tidwall/pretty" error: unable to find source related to: "github.com/tidwall/pretty"

So currently yaegi doesn't support external libraries? Is there some workaround for that?

g3rzi avatar Jun 01 '20 14:06 g3rzi

?

g3rzi avatar Jun 17 '20 09:06 g3rzi

@g3rzi yaegi is unsupported and untested on windows, so you might be running into a problem just because you're on windows, and we cannot help you debug that. However, if you retry on another supported platform and you see the same problem, then we might be able to help.

mpl avatar Jun 17 '20 16:06 mpl

The problem exist also Linux.
I download the same code to my Ubuntu 16.04.3 LTS:

package main

import (
	"github.com/containous/yaegi/interp"
	"github.com/containous/yaegi/stdlib"
)

const payload = `
package main

import (
	"fmt"
	"github.com/tidwall/pretty"
)

func main(){
	var example2 = "{\"name\": \"hello\"}"
	result := pretty.Pretty([]byte(example2))
	fmt.Println(string(result))
}
`

func main(){

	i := interp.New(interp.Options{})
	i.Use(stdlib.Symbols)

	_, err := i.Eval(payload)
	if err != nil {
		panic(err)
	}
}

I go the following error:

GOROOT=/usr/local/go #gosetup
GOPATH=/home/jiren/go #gosetup
/usr/local/go/bin/go build -o /tmp/___go_build_check_go /home/jiren/go/src/gosploit/cmd/yaegi_check/check.go #gosetup
/tmp/___go_build_check_go #gosetup
panic: 6:2: import "github.com/tidwall/pretty" error: unable to find source related to: "github.com/tidwall/pretty"

goroutine 1 [running]:
main.main()
/home/jiren/go/src/gosploit/cmd/yaegi_check/check.go:68 +0xfe

Process finished with exit code 2

Expected:

GOROOT=/usr/local/go #gosetup
GOPATH=/home/jiren/go #gosetup
/usr/local/go/bin/go build -o /tmp/___go_build_payload1_go__2_ /home/jiren/go/src/gosploit/cmd/payload1/payload1.go #gosetup
/tmp/___go_build_payload1_go__2_ #gosetup
{
"name": "hello"
}

g3rzi avatar Jun 18 '20 10:06 g3rzi

You need to specify the GOPATH through the options of the interpreter:

	i := interp.New(interp.Options{
		GoPath: "/home/jiren/go",
	})

mpl avatar Jun 22 '20 16:06 mpl

Any update here ? More than a year from the last comment but nothing seems available to fix this ! @mpl

darkweak avatar Jul 08 '21 23:07 darkweak

Any update here ? More than a year from the last comment but nothing seems available to fix this ! @mpl

any update? go mod still do not support

CheungChan avatar May 05 '22 14:05 CheungChan