web-ridge / gqlgen-sqlboiler

This is a plugin for gqlgen to generate converts + filter queries and resolvers for sqlboiler
MIT License
74 stars 13 forks source link

Panics when building outside of GOPATH #29

Closed randallmlough closed 4 years ago

randallmlough commented 4 years ago

Piggy backing off my issue on your examples: https://github.com/web-ridge/gqlgen-sqlboiler-examples/issues/2

and this issue: https://github.com/web-ridge/gqlgen-sqlboiler/issues/3

Is this library able to work outside of the GOPATH while using modules?

I created a simple example that attempts to use postgres. Unfortunately, I haven't been able to get it to work at all. It panics everytime I go go run convert_plugin.go

go run convert_plugin.go
[convert] get boiler models
[convert] get extra's from schema
[convert] get model with information
[convert] render preload.gotpl
renderError template.gotpl: template: template.gotpl:26:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render convert.gotpl
renderError template.gotpl: template: template.gotpl:27:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render convert_input.gotpl
renderError template.gotpl: template: template.gotpl:27:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render filter.gotpl
renderError template.gotpl: template: template.gotpl:26:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
panic: recursive or concurrent call to RenderToFile detected

Would love some insight here.

Example prpoject: https://github.com/randallmlough/gql-boil-psql

RichardLindhout commented 4 years ago

It's because of this code if I'm not mistaken

func getGoImportFromFile(dir string) string {
    longPath, err := filepath.Abs(dir)
    if err != nil {
        fmt.Println("error while trying to convert folder to gopath", err)
    }
    // src/Users/.../go/src/gitlab.com/.../app/backend/graphql_models
    return strings.TrimPrefix(pathRegex.FindString(longPath), "src/")
}

https://github.com/web-ridge/gqlgen-sqlboiler/blob/master/convert.go#L151

RichardLindhout commented 4 years ago

I need import in golang

backend := gbgen.Config{
        Directory:   "models",
        PackageName: "models",
    }

From "models" -> to import path in file e.g "github.com/web-ridge/some-app/models"

We probably need this approach https://stackoverflow.com/questions/18537257/how-to-get-the-directory-of-the-currently-running-file

RichardLindhout commented 4 years ago

I think it's hard to detect import path if it's not in GOPATH we may need to let user explicitly set it via config.

RichardLindhout commented 4 years ago

Thanks you for this amazing PR!