99designs/gqlgen

package github.com/99designs/gqlgen: no Go files in

coderste opened this issue ยท 12 comments

What happened?

When running go mod vendor and then adding //go:generate go run -mod=vendor github.com/99designs/gqlgen to the top of my resolver.go file when I try and run go generate -mod=vendor ./... I get the following error

package github.com/99designs/gqlgen: no Go files in /Users/shi03/code/projects/gs/common-api/vendor/github.com/99designs/gqlgen

What did you expect?

I expected the go generate -mod=vendor ./... to re-generate the gql generated files.

Minimal graphql.schema and models to reproduce

versions

  • gqlgen version? v0.9.1
  • go version? go version go1.12.6 darwin/amd64
  • dep or go modules? go modules

Unsure if this is an error on my end. But because I vendor all my dependancies when running any Go commands I add the -mod=vendor flag[1] to every command so that Go using the dependancies inside the vendor folder rather than the cache/downloading them.

[1] - https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away

go run behaves a bit weird in vendor mode, I wrote gorunpkg to work around this behaviour. I haven't updated it to packages yet but if your still vendoring it might just work?

We ended up moving away from it in the getting started guide in favour of a local binary entry point.

https://gqlgen.com/getting-started-dep/

I've tried install gorunpkg and adding //go:generate gorunpkg github.com/99designs/gqlgen to the top of my file.

Unfortunately I still get the same error:

pkg: go/build: importGo github.com/99designs/gqlgen: exit status 1
can't load package: package github.com/99designs/gqlgen: no Go files in /code/project/vendor/github.com/99designs/gqlgen

I've opened an issue on the Go repo golang/go#33240 to see what they recommend.

As this isn't an issue with the gqlgen package I'm happy for you to close this issue :)

i've been using gohack, not sure if it would work for you

So just thought I'd come back and post the solution to this.

Based on information from golang/go#29516 what we can is the following:

Create a gqlgen file inside a scripts folder and add the following code

// +build tools

package main

import "github.com/99designs/gqlgen/cmd"

func main() {
	cmd.Execute()
}

See here how we use // +build tools instead of // +build ignore?

That's because build ignore will not work when trying to vendor dependancies. For example having build ignore will leave out the cmd folder of the 99designs/gqlgen package when using go mods.

What build tools will do though is pull in the dependancies into the vendor directory but will still ignore them dependancies when running go build as to not bloat your application.

Then inside the resolver.go file we can just have

//go:generate go run ../../scripts/gqlgen.go

This will then have go generate work with vendored modules

As a workaround I did rm -rf vendor then ran go run github.com/99designs/gqlgen generate and then vendor again

jesus no matter what i do none of this solutions works for me, i'm already feeling dumb.

@leandroruel just follow this tutorial https://gqlgen.com/getting-started/
This will work 100%

thanks @khanakia now i fixed it. \o/

@leandroruel how exactly did you fix this ? Can you leave some information here so it will be helpful for other users in case they face the same issue in future.

HERE's THE SOLUTION:

create a new file called gqlgen.go and place this code

// +build tools

package main

import (
	"fmt"

	"github.com/99designs/gqlgen/cmd"
)

func main() {
	fmt.Println("Building Graphql schema")
	cmd.Execute()
}

now run this commands

go mod vendor
go run ./gqlgen.go

OR you can used the command as generator by placing this code in your main.go

//go:generate go run ./gqlgen.go

after that you can simply run go generate command

I'm running into the same issue here. After trying the solution, I'm getting the following error:

$ go run ./gqlgen.go
gqlgen.go:9:2: no required module provides package github.com/99designs/gqlgen/cmd; to add it:
        go get github.com/99designs/gqlgen/cmd

$ go get github.com/99designs/gqlgen/cmd
go: module github.com/99designs/gqlgen@upgrade found (v0.17.22), but does not contain package github.com/99designs/gqlgen/cmd

My go.mod is showing gqlgen version v0.17.22. After looking at the current source code, I don't think the "cmd/" directory still exists. Does anyone know a to date solution to resolve this "no Go files" circular dependency issue between go mod tidy and go generate ./...?

@renxinhe executing go get github.com/99designs/gqlgen@v0.17.40 did it for me