src-d/go-kallax

go 1.11 mod support

llonchj opened this issue · 12 comments

kallax gen does not work when in a module outside GOPATH.

gvs42 commented

for me it does not work inside any gomod enabled src directory.
When GO111MODULE=off is set, then "kallax gen" generates code correctly. Otherwise the generated kallax.go is only an empty template.
With modules enabled, it does not list any Models found.

Output with modules enabled (GO111MODULE=on and go.mod in the src path):

Package: models

Output with modules disabled (GO111MODULE=off):

Package: models
Model: "ProductFeature" [59 Field(s)] [Events: [BeforeSave]]

ibrt commented

Sad workaround:

  1. Create a gen.sh file in your models package dir:
#!/usr/bin/env bash

set -e

rm -f "gen.go"

for PKG in $(go list -f '{{ join .Imports "\n" }}' | grep '\.'); do
    (cd "$HOME/go"; GOPATH="$(pwd)" go get -u "$PKG")
done

GO111MODULE=off kallax gen -e "gen.go" --output "gen.go"
  1. Use this go:generate comment:
//go:generate ./gen.sh

May or may not work with your setup, but you get the idea. Tested with Go 1.12.1 on macOS. The go get stuff is needed so that the code is compilable even if module support is off, by searching for deps in the default GOPATH.

gvs42 commented

Thanks for the workaround. Had basically the same thing in place, but I did not know about the Imports / go get part.

Any plans about supporting modules?

It looks like kallax uses https://github.com/src-d/go-parse-utils for parsing go files. The go-parse-utils library exposes GOPATH as a first class concept parsutils.GoPath, parseutils.DefaultGoPath so I think a required step for making kallax support go modules is to create a version of go-parse-utils that abstracts away the concept of GOPATH and go modules. This work would likely break compatibility with the current library.

There has been any progress on this?

Was so eager to try it out, but faces this problem. Would be awesome if we could support go mod.

Was so eager to try it out, but faces this problem. Would be awesome if we could support go mod.

I've been using a variation of @ibrt script:

#!/usr/bin/env bash

set -e

export GO111MODULE=off
export GOPATH=/tmp/kallax

FILE=kallax.go

mkdir -p $GOPATH

if [ -f "$FILE" ]; then
    mv $FILE $FILE.old
fi

for PKG in $(go list -f '{{ join .Imports "\n" }}' | grep '\.'); do
    (cd "$GOPATH"; go get "$PKG")
done

if [ -f "$FILE.old" ]; then
    mv $FILE.old $FILE
fi

kallax $@

And it works pretty well and don't pollute my default GOPATH.

And it works pretty well and don't pollute my default GOPATH.

It gives
go: cannot find main module; see 'go help modules' models/kallax_init.go:3: running "./kallax.sh": exit status 1

I'm not sure what's wrong, will take a look later today

Do you have kallax installed and available on your PATH?

go get -u gopkg.in/src-d/go-kallax.v1/...

You may also want to omit the mv kallax.go kallax.go.old (and vice versa), if it's the first time.

The go:generate instruction would look like:

//go:generate $PATH_TO_SCRIPT/kallax.sh gen

See https://github.com/networkteam/go-kallax for an updated fork (of a fork) that supports Go modules and also makes the tests with Go modules green again.

@hlubek, it appears that some import references should be fixed

$ go get -u github.com/networkteam/go-kallax/...
go: downloading github.com/networkteam/go-kallax v1.3.9
go: found github.com/networkteam/go-kallax/... in github.com/networkteam/go-kallax v1.3.9
go get: github.com/networkteam/go-kallax@v1.3.9: parsing go.mod:
        module declares its path as: github.com/zbyte/go-kallax
                but was required as: github.com/networkteam/go-kallax

@hlubek, it appears that some import references should be fixed

$ go get -u github.com/networkteam/go-kallax/...
go: downloading github.com/networkteam/go-kallax v1.3.9
go: found github.com/networkteam/go-kallax/... in github.com/networkteam/go-kallax v1.3.9
go get: github.com/networkteam/go-kallax@v1.3.9: parsing go.mod:
        module declares its path as: github.com/zbyte/go-kallax
                but was required as: github.com/networkteam/go-kallax

Hi @llonchj,

maybe the pre-release tag wasn't fetched. For me it worked when I used go get github.com/networkteam/go-kallax in a project using Go modules. I just created a v1.4.0 tag to hopefully fix this.