Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
g3rzi opened this issue May 27, 2020 · 12 comments
Closed
Labels

Comments

@g3rzi
Copy link

g3rzi commented May 27, 2020

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 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
Copy link
Author

g3rzi commented May 27, 2020

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.

@mpl mpl added the question Further information is requested label May 27, 2020
@mpl
Copy link
Collaborator

mpl commented May 27, 2020

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 mpl self-assigned this May 27, 2020
@g3rzi
Copy link
Author

g3rzi commented Jun 1, 2020

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
Copy link
Author

g3rzi commented Jun 17, 2020

?

@mpl
Copy link
Collaborator

mpl commented Jun 17, 2020

@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.

@g3rzi
Copy link
Author

g3rzi commented Jun 18, 2020

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"
}

@mpl
Copy link
Collaborator

mpl commented Jun 22, 2020

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

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

@darkweak

This comment has been minimized.

@CheungChan

This comment was marked as off-topic.

@PatriotBo

This comment was marked as off-topic.

@mvertes
Copy link
Member

mvertes commented Feb 28, 2023

no response after a long time.

@mvertes mvertes closed this as completed Feb 28, 2023
@caius-kong
Copy link

  1. go mod init xxx
  2. go get .
  3. go mod vendor
  4. copy package folder (under vendor folder) to $GOPATH/src
  5. yaegi run ./sample.go

it's ok

fty4 added a commit to taskmedia/ddns-allowlist that referenced this issue Oct 1, 2024
github.com/traefik/yaegi/issues/656#issuecomment-1449633582
fty4 added a commit to taskmedia/ddns-allowlist that referenced this issue Oct 1, 2024
* chore: update yaegi v0.16.1 and golangci-lint v1.61.0

* fix(ci): copy go vendor to gopath

github.com/traefik/yaegi/issues/656#issuecomment-1449633582

* fix(ci): use gopath setup in step in advance

* chore(ci): disable yaegi testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants