我有一个带有一些服务的单声道存储库(service-0 到 service-4)。有一个 proto 目录,其中存储了 proto 文件。原型文件位于各自的子文件夹中。目录结构如下:
.
├── 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_register_toolchains", "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",
visibility = ["//visibility:public"],
deps = [
"@go_googleapis//google/api:annotations_go_proto",
"//protoc-gen-swagger/options:options_proto",
],
)
go_library(
name = "service-0",
embed = [":0_go_proto"],
importpath = "gitlab.example.de/tool/proto/service-0",
visibility = ["//visibility:public"],
)
现在运行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 文件中,它是如何描述的:
# 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
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,
visibility = ["//visibility:public"],
deps = [":0_proto"],
)
gateway_swagger_compile(
name = "0_gateway_swagger_grpc",
visibility = ["//visibility:public"],
deps = [":0_proto"],
)
gateway_grpc_library(
name = "0_gateway_library",
importpath = "gitlab.example.de/tool/proto/0",
visibility = ["//visibility:public"],
deps = [":0_proto"],
)
运行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