使用bazel生成grpc-gateway,swagger和grpc-go文件

如何解决使用bazel生成grpc-gateway,swagger和grpc-go文件

我有一个包含某些服务(服务0到服务4)的单声道仓库。有一个原始目录,其中存储原始文件。原始文件位于相应的子文件夹中。 目录的结构如下:

.
├── BUILD.bazel
├── gateway
│   ├── .idea
│   ├── BUILD.bazel
│   ├── gateway.iml
│   ├── go.mod
│   ├── go.sum
│   └── main.go
├── gen
│   └── pb-go
├── service-0
│   └── .idea
├── service-1
│   └── .idea
├── service-2
│   └── .idea
├── service-3
│   └── .idea
├── service-4
│   └── .idea
├── Makefile
├── proto
│   ├── service-0
│   │   ├── BUILD.bazel
│   │   └── service-0.proto
│   ├── service-1
│   │   ├── BUILD.bazel
│   │   └── service-1.proto
│   ├── service-2
│   │   ├── BUILD.bazel
│   │   └── service-2.proto
│   ├── service-3
│   │   ├── BUILD.bazel
│   │   └── service-3.proto
│   └── service-4
│       ├── BUILD.bazel
│       └── service-4.proto
├── README.md
├── scripts
│   └── generate-go.sh
├── test
├── ui
│   ├── BUILD.bazel
│   ├── node_modules
│   ├── package.json
│   ├── package-lock.json
│   ├── public
│   ├── README.md
│   ├── src
│   ├── stdout.log
│   └── tsconfig.json
└── WORKSPACE

我的工作区文件如下:

workspace(
    name = "tool",managed_directories = {"@npm": ["ui:node_modules"]},)

load("@bazel_tools//tools/build_defs/repo:http.bzl","http_archive")

## go rules
http_archive(
    name = "io_bazel_rules_go",sha256 = "08369b54a7cbe9348eea474e36c9bbb19d47101e8860cec75cbf1ccd4f749281",urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.0/rules_go-v0.24.0.tar.gz","https://github.com/bazelbuild/rules_go/releases/download/v0.24.0/rules_go-v0.24.0.tar.gz",],)

## gazelle
http_archive(
    name = "bazel_gazelle",sha256 = "d4113967ab451dd4d2d767c3ca5f927fec4b30f3b2c6f8135a2033b9c05a5687",urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.0/bazel-gazelle-v0.22.0.tar.gz","https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.0/bazel-gazelle-v0.22.0.tar.gz",)

load("@io_bazel_rules_go//go:deps.bzl","go_register_toolchains","go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl","gazelle_dependencies")

go_rules_dependencies()

go_register_toolchains()

gazelle_dependencies()

####### Protobuf rules
http_archive(
    name = "rules_proto_grpc",urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/1.0.2.tar.gz"],sha256 = "5f0f2fc0199810c65a2de148a52ba0aff14d631d4e8202f41aff6a9d590a471b",strip_prefix = "rules_proto_grpc-1.0.2",)

load("@rules_proto_grpc//:repositories.bzl","rules_proto_grpc_toolchains","rules_proto_grpc_repos")
rules_proto_grpc_toolchains()
rules_proto_grpc_repos()
########

####### go grpc rules

load("@rules_proto_grpc//go:repositories.bzl",rules_proto_grpc_go_repos="go_repos")

rules_proto_grpc_go_repos()
#####################

################# GRPC-GATEWAY
load("@rules_proto_grpc//:repositories.bzl","bazel_gazelle","io_bazel_rules_go")

io_bazel_rules_go()

load("@io_bazel_rules_go//go:deps.bzl","go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains()

bazel_gazelle()

load("@bazel_gazelle//:deps.bzl","gazelle_dependencies")

gazelle_dependencies()

load("@rules_proto_grpc//github.com/grpc-ecosystem/grpc-gateway:repositories.bzl",rules_proto_grpc_gateway_repos="gateway_repos")

rules_proto_grpc_gateway_repos()

load("@grpc_ecosystem_grpc_gateway//:repositories.bzl","go_repositories")

go_repositories()
###############################

service-0.proto文件具有以下内容:

syntax = "proto3";
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
import "protoc-gen-swagger/options/annotations.proto";

option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = {
  info: {
    title: "Tool";
    version: "1.0";
    contact: {
      name: " Tool project";
      url: "https://gitlab.example.de/tool";
      email: "example@test.de";
    };
    license: {
      name: "Apache License 2.0";
      url: "https://gitlab.example.de/tool/-/blob/master/LICENSE";
    };
  };
  // Overwriting host entry breaks tests,so this is not done here.
  external_docs: {
    url: "https://github.com/grpc-ecosystem/grpc-gateway";
    description: "More about gRPC-Gateway";
  }
  schemes: HTTPS;
  consumes: "application/json";
  produces: "application/json";
  security_definitions: {
    security: {
      key: "Bearer";
      value: {
        type: TYPE_API_KEY;
        in: IN_HEADER;
        name: "Authorization";
      }
    }
  }
  responses: {
    key: "403";
    value: {
      description: "Returned when the user does not have permission to access the resource.";
    }
  }
  responses: {
    key: "404";
    value: {
      description: "Returned when the resource does not exist.";
      schema: {
        json_schema: {
          type: STRING;
        }
      }
    }
  }
};

service JWTService {
  option (grpc.gateway.protoc_gen_swagger.options.openapiv2_tag) = {
    description: "JWT Service CRUD."
  };
  rpc CreateJWTToken(CreateJWTTokenRequest) returns (CreateJWTTokenResponse) {}
  rpc UpdateJWTToken(google.protobuf.Empty) returns (google.protobuf.Empty) {
    option (google.api.http) = {
      post: "/v1/jwt/update"
      body: "*"
    }; 
  }
  rpc DeleteJWTToken(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}

message CreateJWTTokenRequest {
  string username = 1;
  string password = 2;
}
message CreateJWTTokenResponse {
  string jwt = 1;
}

我正在使用bazel创建BUILD文件。因此,运行bazel run //:gazelle在proto / service-0目录中创建以下BUILD.bazel文件:

load("@rules_proto//proto:defs.bzl","proto_library")
load("@io_bazel_rules_go//go:def.bzl","go_library")
load("@io_bazel_rules_go//proto:def.bzl","go_proto_library")

proto_library(
    name = "0_proto",srcs = ["service-0.proto"],visibility = ["//visibility:public"],deps = [
        "@com_google_protobuf//:empty_proto","@go_googleapis//google/api:annotations_proto","//protoc-gen-swagger/options:options_proto",)

go_proto_library(
    name = "0_go_proto",compilers = ["@io_bazel_rules_go//proto:go_grpc"],importpath = "gitlab.example.de/tool/proto/service-0",proto = ":0_proto",deps = [
        "@go_googleapis//google/api:annotations_go_proto",)

go_library(
    name = "service-0",embed = [":0_go_proto"],)

现在运行bazel build //proto/service-0:service-0会出现以下错误:

ERROR: /home/Documents/tool/proto/service-0/BUILD.bazel:16:1: no such package 'protoc-gen-swagger/options': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.

因此,我将以下行添加到根BUILD.bazel文件中,其描述方式here

# gazelle:resolve proto protoc-gen-swagger/options/annotations.proto @grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:options_proto

让瞪羚生成@grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:options_proto而不是//protoc-gen-swagger/options:options_proto 这可以正常工作。

现在如何使用开发服务的bazel创建go-grpc文件?

运行bazel build //proto/service-0:0_go_proto会出现以下错误:

ERROR: /home/Documents/tool/proto/service-0/BUILD.bazel:16:1: in deps attribute of go_proto_library rule //proto/service-0:0_go_proto: '@grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:options_proto' does not have mandatory providers: 'GoLibrary'

因此,为了进行开发,应该可以使用bazel生成go-grpc文件,以便将生成的grpc文件存储在proto / service-x /目录中。

此外,我正在使用grpc-gateway。因此,我在BUILD.bazel目录中的service-0文件中添加了以下几行here的描述方式:

load("@rules_proto_grpc//github.com/grpc-ecosystem/grpc-gateway:defs.bzl","gateway_grpc_compile","gateway_grpc_library","gateway_swagger_compile")

gateway_grpc_compile(
    name = "0_gateway_grpc",verbose = 1,deps = [":0_proto"],)

gateway_swagger_compile(
    name = "0_gateway_swagger_grpc",)

gateway_grpc_library(
    name = "0_gateway_library",importpath = "gitlab.example.de/tool/proto/0",)

运行bazel build :0_gateway_swagger_grpc得到以下输出:

INFO: Invocation ID: 3b88f546-f09a-49d7-b238-0d41d98b9aa6
INFO: Analyzed target //proto/service-0:0_gateway_swagger_grpc (0 packages loaded,0 targets configured).
INFO: Found 1 target...
Target //proto/service-0:0_gateway_swagger_grpc up-to-date:
  dist/bin/proto/service-0/0_gateway_swagger_grpc/proto/service-0/service-0.swagger.json
  dist/bin/proto/service-0/0_gateway_swagger_grpc/protoc-gen-swagger/options/annotations.swagger.json
  dist/bin/proto/service-0/0_gateway_swagger_grpc/protoc-gen-swagger/options/openapiv2.swagger.json
INFO: Elapsed time: 0.092s,Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully,1 total action

这可以正常工作。但是,当我尝试使用bazel build :0_gateway_grpc生成网关文件时,出现以下错误:

INFO: Analyzed target //proto/service-0:0_gateway_grpc (3 packages loaded,17 targets configured).
INFO: Found 1 target...
INFO: From Compiling protoc outputs for grpc_gateway_plugin plugin:
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
    A future release of golang/protobuf will delete this package,which has long been excluded from the compatibility promise.

INFO: From Compiling protoc outputs for grpc_gateway_plugin plugin:
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
    A future release of golang/protobuf will delete this package,which has long been excluded from the compatibility promise.

ERROR: /home/.cache/bazel/_bazel_/81e1d15aef6baed1975edd8b4c490e5b/external/grpc_ecosystem_grpc_gateway/protoc-gen-swagger/options/BUILD.bazel:20:1: output 'external/grpc_ecosystem_grpc_gateway/protoc-gen-swagger/options/options_proto/gateway_grpc_compile_aspect_verb1/protoc-gen-swagger/options/annotations.pb.gw.go' was not created
ERROR: /home/.cache/bazel/_bazel_/81e1d15aef6baed1975edd8b4c490e5b/external/grpc_ecosystem_grpc_gateway/protoc-gen-swagger/options/BUILD.bazel:20:1: output 'external/grpc_ecosystem_grpc_gateway/protoc-gen-swagger/options/options_proto/gateway_grpc_compile_aspect_verb1/protoc-gen-swagger/options/openapiv2.pb.gw.go' was not created
ERROR: /home/.cache/bazel/_bazel_/81e1d15aef6baed1975edd8b4c490e5b/external/grpc_ecosystem_grpc_gateway/protoc-gen-swagger/options/BUILD.bazel:20:1: not all outputs were created or valid
Target //proto/service-0:0_gateway_grpc failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.243s,Critical Path: 0.11s
INFO: 2 processes: 2 linux-sandbox.
FAILED: Build did NOT complete successfully

那我该如何解决呢?修复此问题后,如何使bazel也可以在proto / service-x目录中创建/复制要开发的文件?

我正在使用bazelisk和Bazel版本3.0.0

解决方法

不确定您是否已经找到了解决方案,但我正在尝试做类似的事情,您的笔记对我很有帮助。

关于这个错误:

ERROR: /home/Documents/tool/proto/service-0/BUILD.bazel:16:1: in deps attribute of go_proto_library rule //proto/service-0:0_go_proto: '@grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:options_proto' does not have mandatory providers: 'GoLibrary'

我发现您可以使用包含 gazelle:resolve 参数的第二个 import-lang 指令来解决此问题。具体:

 gazelle:resolve proto go protoc-gen-openapiv2/options/annotations.proto @grpc_ecosystem_grpc_gateway//protoc-gen-openapiv2/options:options_go_proto

我没有遇到您描述的其他错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-