Bazel构建、protobuf和代码补全

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

Bazel build, protobuf and code completion

问题

当使用Bazel构建包含.proto文件的项目时,我们可以使用go_binarygo_proto_library规则来创建一个二进制文件。生成的二进制文件将按预期工作,但是从.proto文件生成的代码不在工作目录中。

结果是,编辑器和集成开发环境中的代码补全功能将无法正常工作。更糟糕的是,它们经常会打印有关无效导入语句的错误。

这是一个使用协议缓冲区的示例,但可能还有其他类似的情况,比如使用第三方依赖项。

通常如何处理这种情况?

英文:

When using Bazel to build a project that contains .proto files, we can use, for example, go_binary and go_proto_library rules to create a binary. The generated binary will work as expected, but the code generated from the .proto files is not in the working directory.

As a result, code completion in editors and IDEs will not work. Even worse, they will often print errors about invalid import statements.

This is an example using protocol buffers, but there are probably other similar cases, such as using third party dependencies.

How is this usually handled?

答案1

得分: 5

我是负责Bazel的Go规则的工程师之一。很遗憾,我现在没有一个好的答案,但这个问题已经引起我们的关注,Go团队正在努力寻找一个长期解决方案。我在我们的GitHub仓库上提交了bazelbuild/rules_go#512来跟踪这个问题,我们在有更好的解决方案时会更新这个答案。

问题在于Bazel将生成的代码和其他文件存储在自己的内部目录中,而编辑器并不知道这一点。我们正在设计和构建一个新的工作空间抽象机制,使工具(编辑器、IDE、guru等)能够理解存储库中的所有源代码、生成的文件、包、导入和其他元数据,而无需直接集成Bazel或其他构建系统。

作为一个临时解决方法,您可以尝试使用protoc手动生成.pb.go文件。您可以将这些文件检入您的存储库,但不要在BUILD文件中包含它们。这样可以让编辑器和IDE意识到生成的文件,但在编译时仍会生成新的、最新版本的文件。

英文:

I'm one of the engineers working on the Go rules for Bazel. Unfortunately, I don't have a good answer for you right now, but this issue is on our radar, and the Go team is working on a general long-term solution. I've filed bazelbuild/rules_go#512 on our GitHub repo to track this issue, and I'll update this answer when we have a better solution.

The problem is Bazel stores generated code and other artifacts in its own internal directory that editors aren't aware of. We're designing and building a new workspace abstraction mechanism that will allow tools (editors, IDEs, tools like guru) to understand all of sources, generated files, packages, imports, and other metadata in a repository without needing to integrate directly with Bazel or other build systems.

As a temporary workaround, try generating the .pb.go files manually with protoc. You can check those files into your repository but not include them in your BUILD files. This should make editors and IDEs aware of the generated files, but you'll still produce new, up-to-date versions of them when you compile.

答案2

得分: 0

我在使用Buck时也遇到了同样的问题,并实施了一个解决方法,将生成的代码复制到vendor文件夹中。

不过,我还是期待Go语言中的工作空间抽象机制。

英文:

I ran into the same issue with Buck too, and implemented a workaround to have Buck copying generated code to vendor folder.

Looking forward to the workspace abstraction mechanism in Go though.

huangapple
  • 本文由 发表于 2017年6月6日 02:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/44375665.html
匿名

发表评论

匿名网友

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

确定