traefik / yaegi

Yaegi is Another Elegant Go Interpreter
https://pkg.go.dev/github.com/traefik/yaegi
Apache License 2.0
7.17k stars 356 forks source link

Import the redigo package in traefik plugins prompts an error #1356

Open kk-kwok opened 2 years ago

kk-kwok commented 2 years ago

The following program sample.go triggers an unexpected result

package main

import (
    "github.com/gomodule/redigo/redis"
)

Expected result

Can start traefik normally

Got

2022/02/16 17:16:57 traefik.go:79: command traefik error: infra/traefik-plugins/ratelimiter: failed to import plugin code "infra/traefik-plugins/ratelimiter": 1:21: import "infra/traefik-plugins/ratelimiter" error: plugins-local/src/infra/traefik-plugins/ratelimiter/leakybucket.go:5:2: import "github.com/gomodule/redigo/redis" error: plugins-local/src/infra/traefik-plugins/ratelimiter/vendor/github.com/gomodule/redigo/redis/conn.go:236:20: cannot use type func(*net.Dialer, context.Context, string, string) (net.Conn, error) as type func(context.Context,string,string) (net.Conn,error) in assignment

Yaegi Version

github.com/traefik/yaegi v0.11.2

Additional Notes

vim plugins-local/src/infra/traefik-plugins/ratelimiter/leakybucket.go
import "github.com/gomodule/redigo/redis"
experimental:
    rateLimit:
      moduleName: infra/traefik-plugins/ratelimiter
prajithp13 commented 2 years ago

@kk-kwok @mvertes any workaround for this? facing the same problem

luoxinliu commented 2 years ago

i have same problem

VladyslavLukyanenko commented 2 years ago

Same problem here

nferch commented 1 year ago

I am facing a similar problem with a library using net.Dialer.DialContext when trying to import the HashiCorp Vault API.

To repro:

>yaegi -syscall -unsafe -unrestricted github.com/hashicorp/go-cleanhttp
run: /home/nf/go/src/github.com/traefik/plugindemo/vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go:26:16: cannot use type func(*net.Dialer, context.Context, string, string) (net.Conn, error) as type func(context.Context, string, string) (net.Conn, error) in struct literal
Lasica commented 1 year ago

I've also faced a similar issue when trying to develop traefik plugin (which uses yaegi as interpreter for code) and importing "cloud.google.com/go/pubsub". I've got the error:

 error="github.com/Lasica/traefic-pubsub-forwarder: failed to import plugin code \"github.com/Lasica/traefic-pubsub-forwarder\": 1:21: import \"github.com/Lasica/traefic-pubsub-forwarder\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/pubsub_monitoring.go:8:2: import \"cloud.google.com/go/pubsub\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/vendor/cloud.google.com/go/pubsub/iterator.go:27:2: import \"cloud.google.com/go/pubsub/apiv1\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/vendor/cloud.google.com/go/pubsub/apiv1/doc.go:90:2: import \"google.golang.org/api/option\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/vendor/google.golang.org/api/option/option.go:13:2: import \"golang.org/x/oauth2/google\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/vendor/golang.org/x/oauth2/google/default.go:17:2: import \"cloud.google.com/go/compute/metadata\" error: plugins-local/src/github.com/Lasica/traefic-pubsub-forwarder/vendor/cloud.google.com/go/compute/metadata/metadata.go:69:10: cannot use type func(*net.Dialer, string, string) (net.Conn, error) as type func(string, string) (net.Conn, error) in struct literal"
mvertes commented 1 year ago

will look at it.

Lasica commented 1 year ago

I've noticed that while traefic and the library I was trying to import both use golang in version 1.19, the yaegi that traefik uses in version 0.15 uses golang in version 1.18. I've checked that the api in question (net.Dialer.Dial) was changed in 1.19. That's the likely casue of this problem:

https://github.com/traefik/traefik/blob/v2.9.8/go.mod https://github.com/googleapis/google-cloud-go/blob/b5b7cdec59df6a746ed7ffef0aad667ced8cbc87/go.mod https://github.com/traefik/yaegi/blob/v0.15.0/go.mod https://tip.golang.org/doc/go1.19

Prajithp commented 1 year ago

Any update on this?

leonlyu1996 commented 11 months ago

Any update on this?

hsmade commented 5 months ago

I'm still seeing this same issue with traefik 3.0.2 (which is on 1.22), which includes yaegi 0.16.1 (which is on 1.21). When trying to load a plugin which imports the consulcatalog provider (which I wish to extend with a feature), treafik fails to load the plugin:

..../vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go:26:16: cannot use type func(*net.Dialer, context.Context, string, string) (net.Conn, error) as type func(context.Context, string, string) (net.Conn, error) in struct literal"

Which comes from the following code:

func DefaultPooledTransport() *http.Transport {
    transport := &http.Transport{
        Proxy: http.ProxyFromEnvironment,
        DialContext: (&net.Dialer{
            Timeout:   30 * time.Second,
            KeepAlive: 30 * time.Second,
            DualStack: true,
        }).DialContext,

While this API has changed way before Go version 1.21. DialContext /does/ return context.Context, string, string. When debugging traefik, I do see that the interpreter context's GOROOT is ..../go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.darwin-amd64

Sadly, when I try to run yaegi on cli, and import my package, I get an even weirder error:

$ GOPATH=plugins-local/ yaegi
> import "github.com/hsmade/traefik-consul-az-provider"
1:21: import "github.com/hsmade/traefik-consul-az-provider" error: plugins-local/src/github.com/hsmade/traefik-consul-az-provider/plugin.go:9:2: import "github.com/traefik/genconf/dynamic" error: plugins-local/src/github.com/hsmade/traefik-consul-az-provider/vendor/github.com/traefik/genconf/dynamic/marshaler.go:9:1: cannot define new methods on non-local type struct {}

The 'type struct' it refers to isn't a struct, but a custom type.

duj4 commented 2 months ago

Met similar issue in Traefik 3.0.4 while using DialContext to set Transport, any update on this one?

duj4 commented 2 months ago

I'm still seeing this same issue with traefik 3.0.2 (which is on 1.22), which includes yaegi 0.16.1 (which is on 1.21). When trying to load a plugin which imports the consulcatalog provider (which I wish to extend with a feature), treafik fails to load the plugin:

..../vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go:26:16: cannot use type func(*net.Dialer, context.Context, string, string) (net.Conn, error) as type func(context.Context, string, string) (net.Conn, error) in struct literal"

Which comes from the following code:

func DefaultPooledTransport() *http.Transport {
  transport := &http.Transport{
      Proxy: http.ProxyFromEnvironment,
      DialContext: (&net.Dialer{
          Timeout:   30 * time.Second,
          KeepAlive: 30 * time.Second,
          DualStack: true,
      }).DialContext,

While this API has changed way before Go version 1.21. DialContext /does/ return context.Context, string, string. When debugging traefik, I do see that the interpreter context's GOROOT is ..../go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.darwin-amd64

Sadly, when I try to run yaegi on cli, and import my package, I get an even weirder error:

$ GOPATH=plugins-local/ yaegi
> import "github.com/hsmade/traefik-consul-az-provider"
1:21: import "github.com/hsmade/traefik-consul-az-provider" error: plugins-local/src/github.com/hsmade/traefik-consul-az-provider/plugin.go:9:2: import "github.com/traefik/genconf/dynamic" error: plugins-local/src/github.com/hsmade/traefik-consul-az-provider/vendor/github.com/traefik/genconf/dynamic/marshaler.go:9:1: cannot define new methods on non-local type struct {}

The 'type struct' it refers to isn't a struct, but a custom type.

hello @hsmade , do you have any workaround on this issue? I am facing the same one for now, though the version is 3.0.4.

hsmade commented 2 months ago

Sadly I don't. I never managed to get past this issue

0xSecure commented 2 months ago

From 2022 to the current time they haven't fixed such problems, amazing. How to use redis for more flexible plugins or similar things is not even close to clear, well unsafe is bad, but it turns out that no version of redis libraries hardly work.