99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.94k stars 1.16k forks source link

Fails when built with -trimpath #910

Open tv42 opened 5 years ago

tv42 commented 5 years ago

What happened?

$ GOFLAGS='-trimpath' go build github.com/99designs/gqlgen && ./gqlgen
modelgen: locating templates: lstat github.com/99designs/gqlgen@v0.10.1/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

$ 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.

stale[bot] commented 4 years ago

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.

tv42 commented 4 years ago

Bugs don't disappear just because time passes!

vektah commented 4 years ago

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.

tv42 commented 4 years ago

Trimpath is a very nice default.

stale[bot] commented 4 years ago

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.

sateeshpnv commented 4 years ago

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.

sagikazarmark commented 2 years ago

but its trading off a lot of developer experience to fix your build environment.

How about using the embed feature introduced in Go 1.16?