Gqlgen: Fails when built with -trimpath

Created on 25 Oct 2019  路  6Comments  路  Source: 99designs/gqlgen

What happened?

$ GOFLAGS='-trimpath' go build github.com/99designs/gqlgen && ./gqlgen
modelgen: locating templates: lstat github.com/99designs/[email protected]/plugin/modelgen: no such file or directory
[EXIT:3] $ 

What did you expect?

Success, like without -trimpath:

$ GOFLAGS=' ' go build github.com/99designs/gqlgen  && ./gqlgen
$ 

Minimal graphql.schema and models to reproduce

Just about anything, really.

versions

  • gqlgen version?
  • go version?
  • dep or go modules?
$ go list -m github.com/99designs/gqlgen
github.com/99designs/gqlgen v0.10.1
$ go version
go version go1.13.3 linux/amd64

And obviously modules.

It seems gqlgen is using build-time paths to try to locate its template files, and trimpath confuses that. Perhaps the templates should be bundled into the executable.

accepted bug

All 6 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Bugs don't disappear just because time passes!

So don't use trimpath? gqlgen doesnt get shipped to your servers, its a tool used in your ci/dev environment.

We might start inlining templates again, but its trading off a lot of developer experience to fix your build environment.

Trimpath is a very nice default.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

bazel uses trimpath to achieve reproducible builds. We use gqlgen/api to generate code. And modelgen fails in locating templates. The following patch works for us.

diff -urN a/codegen/templates/templates.go b/codegen/templates/templates.go
--- a/codegen/templates/templates.go 2020-05-26 15:48:27.000000000 -0700
+++ b/codegen/templates/templates.go 2020-05-26 15:48:28.000000000 -0700
@@ -75,6 +75,12 @@
        roots = append(roots, "template.gotpl")
    } else {
        // load all the templates in the directory
+       // In a bazel environment, that uses trimpath to achieve reproducible builds, template files are under ${runfiles}/__main__/${rootDir} and not {rootDir} during runtime
+       // Hence prefix RUN_PATH if set.
+       // Ex: bazel-out/host/bin/src/code/generate.runfiles/__main__/external/com_github_99designs_gqlgen/codegen
+       if runFilesPath := os.Getenv("RUN_PATH"); runFilesPath != "" {
+           rootDir = filepath.Join(runFilesPath, rootDir)
+       }
        err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err

Update:

Instead of patching gqlgen code, changing directory to the runtime path helped. No patch required in gqlgen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sumanthakannantha picture sumanthakannantha  路  3Comments

msmedes picture msmedes  路  4Comments

jszwedko picture jszwedko  路  3Comments

itsbalamurali picture itsbalamurali  路  4Comments

imiskolee picture imiskolee  路  3Comments