go-kallax icon indicating copy to clipboard operation
go-kallax copied to clipboard

go 1.11 mod support

Open llonchj opened this issue 7 years ago • 12 comments

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

llonchj avatar Oct 23 '18 01:10 llonchj

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]]

gvs42 avatar Dec 22 '18 10:12 gvs42

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.

ibrt avatar Mar 18 '19 19:03 ibrt

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?

gvs42 avatar Mar 21 '19 07:03 gvs42

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.

efarrer avatar Jun 11 '19 15:06 efarrer

There has been any progress on this?

felipemfp avatar Jun 27 '19 14:06 felipemfp

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

iorlas avatar Jul 02 '19 12:07 iorlas

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.

felipemfp avatar Jul 02 '19 13:07 felipemfp

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

iorlas avatar Jul 02 '19 13:07 iorlas

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

felipemfp avatar Jul 02 '19 16:07 felipemfp

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 avatar Jun 26 '20 08:06 hlubek

@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/[email protected]: parsing go.mod:
        module declares its path as: github.com/zbyte/go-kallax
                but was required as: github.com/networkteam/go-kallax

llonchj avatar Jun 27 '20 20:06 llonchj

@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/[email protected]: 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.

hlubek avatar Jun 30 '20 10:06 hlubek