google / martian

Martian is a library for building custom HTTP/S proxies
Apache License 2.0
2k stars 247 forks source link

removing flag parsing from module init #335

Closed phaus closed 2 years ago

phaus commented 2 years ago

At the moment, there is a clash if two modules are imported, that use identical go flags (e.g. v)

You can test this while running go test ./... in e.g. https://github.com/inspired-consulting/oathkeeper/tree/x_update

/var/folders/1k/5r4705w512x0wsjtqt0cm6yh0000gn/T/go-build2931154656/b827/api.test flag redefined: v
panic: /var/folders/1k/5r4705w512x0wsjtqt0cm6yh0000gn/T/go-build2931154656/b827/api.test flag redefined: v

goroutine 1 [running]:
flag.(*FlagSet).Var(0x140000b0120, {0x103e469e0, 0x1048d2b68}, {0x103a6a999, 0x1}, {0x103636948, 0x14})
        /opt/homebrew/Cellar/go/1.18/libexec/src/flag/flag.go:879 +0x2a4
flag.Var(...)
        /opt/homebrew/Cellar/go/1.18/libexec/src/flag/flag.go:894
github.com/golang/glog.init.0()
        /Users/philipp/go/pkg/mod/github.com/golang/glog@v1.0.0/glog.go:401 +0xd4
FAIL    github.com/ory/oathkeeper/api   0.959s
/var/folders/1k/5r4705w512x0wsjtqt0cm6yh0000gn/T/go-build2931154656/b838/cmd.test flag redefined: v
panic: /var/folders/1k/5r4705w512x0wsjtqt0cm6yh0000gn/T/go-build2931154656/b838/cmd.test flag redefined: v

One Solution is, to delete https://github.com/google/martian/blob/master/init.go completly and move the flag initialization into the main function.

I did that one in this PR but don't know, if there are side effects to this.