无法运行gqlgen generate,因为缺少go.sum文件中的模块条目。

huangapple go评论78阅读模式
英文:

Unable to run gqlgen generate due to missing go.sum entry for module

问题

我正在按照https://blog.logrocket.com/gqlgen-build-faster-graphql-server/上的教程进行操作,gqlgen init命令运行正常。

根据指南,我尝试使用go run github.com/99designs/gqlgen generate生成新的模式和解析器(添加了Dog类型),但该命令立即失败。

示例:

go run github.com/99designs/gqlgen generate
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/main.go:20:2: 缺少提供 github.com/urfave/cli/v2 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen 导入);添加:
	go get github.com/99designs/gqlgen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:16:2: 缺少提供 golang.org/x/text/cases 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen/codegen 导入);添加:
	go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:17:2: 缺少提供 golang.org/x/text/language 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen/codegen 导入);添加:
	go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:15:2: 缺少提供 golang.org/x/tools/go/ast/astutil 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen/internal/imports 导入);添加:
	go get github.com/99designs/gqlgen/internal/imports@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/code/packages.go:11:2: 缺少提供 golang.org/x/tools/go/packages 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen/codegen/config 导入);添加:
	go get github.com/99designs/gqlgen/codegen/config@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:16:2: 缺少提供 golang.org/x/tools/imports 包的模块的 go.sum 条目(由 github.com/99designs/gqlgen/internal/imports 导入);添加:
	go get github.com/99designs/gqlgen/internal/imports@v0.17.26

所以我按照这个顺序运行了所有这些缺少模块的go get命令,然后尝试重新运行generate命令,它成功了!很好!👍👍

但是,当我尝试运行下一个generate命令时,相同的命令失败,并出现相同的错误和缺少的包。

为什么初始设置时缺少所有这些导入,然后被删除,并且为什么它不能直接运行?

英文:

I'm following the tutorial at https://blog.logrocket.com/gqlgen-build-faster-graphql-server/ and gqlgen init runs fine.

Following the guide, I'm trying to generate new schemas and resolvers (adding the Dog type) using go run github.com/99designs/gqlgen generate, but the command instantly fails.

Example:

go run github.com/99designs/gqlgen generate
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/main.go:20:2: missing go.sum entry for module providing package github.com/urfave/cli/v2 (imported by github.com/99designs/gqlgen); to add:
	go get github.com/99designs/gqlgen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:16:2: missing go.sum entry for module providing package golang.org/x/text/cases (imported by github.com/99designs/gqlgen/codegen); to add:
	go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/codegen/field.go:17:2: missing go.sum entry for module providing package golang.org/x/text/language (imported by github.com/99designs/gqlgen/codegen); to add:
	go get github.com/99designs/gqlgen/codegen@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:15:2: missing go.sum entry for module providing package golang.org/x/tools/go/ast/astutil (imported by github.com/99designs/gqlgen/internal/imports); to add:
	go get github.com/99designs/gqlgen/internal/imports@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/code/packages.go:11:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by github.com/99designs/gqlgen/codegen/config); to add:
	go get github.com/99designs/gqlgen/codegen/config@v0.17.26
../../../go/pkg/mod/github.com/99designs/gqlgen@v0.17.26/internal/imports/prune.go:16:2: missing go.sum entry for module providing package golang.org/x/tools/imports (imported by github.com/99designs/gqlgen/internal/imports); to add:
	go get github.com/99designs/gqlgen/internal/imports@v0.17.26

So I run all of these go get for those missing modules in that order, and try to re-run generate and it works! Nice! 👏👏

But when I try to run generate again the very next entered command, the same command fails with the same errors and missing packages.

Why are all those imports missing by initial setup, then deleted and why isn't this working out of the box?

答案1

得分: 2

好的,以下是翻译好的内容:

嗯,(愚蠢的)答案是按照 gqlgengithub 仓库 中的指南,并创建一个单独的 tools.go 文件,像这样:

将 github.com/99designs/gqlgen 添加到你的项目的 tools.go 文件中

printf '// +build tools\npackage tools\nimport ("github.com/99designs/gqlgen"\n "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go

然后执行最后的 go mod tidy

作为楼主,我(非常)“有主见”地认为,在初始导入之后,init 命令完全可以正常工作,所以这不应该是一个问题。但是嘿...就是这样。

英文:

Well, the (stupid) answer is to follow the guide at the gqlgen github repo and create a separate tools.go file, like so:

Add github.com/99designs/gqlgen to your project's tools.go

printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go

And then do a final go mod tidy.

The very "opinionated" conclusion from me (the OP) is that this shouldn't be a thing since the init command works just fine after the initial import. But hey.. it is what it is.

huangapple
  • 本文由 发表于 2023年3月15日 22:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定