Skip to content

Commit

Permalink
[bazel] Update rules_closure + fix linter warnings
Browse files Browse the repository at this point in the history
Working towards resolving #744, #575, #550, #700
  • Loading branch information
Yannic committed Mar 3, 2020
1 parent 9ce4ea5 commit ddb574f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 63 deletions.
12 changes: 7 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_closure",
sha256 = "fecda06179906857ac79af6500124bf03fe1630fd1b3d4dcf6c65346b9c0725d",
strip_prefix = "rules_closure-03110588392d8c6c05b99c08a6f1c2121604ca27",
sha256 = "d7a04263cf5b7af90f52d759da1e50c3cfe81c6cb16eec430af86e6bed248098",
strip_prefix = "rules_closure-0e187366b658d1796d2580f8b7e1a8d7e7e1492d",
urls = [
"https://github.com/bazelbuild/rules_closure/archive/03110588392d8c6c05b99c08a6f1c2121604ca27.zip",
"https://github.com/bazelbuild/rules_closure/archive/0e187366b658d1796d2580f8b7e1a8d7e7e1492d.zip",
],
)

load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")
load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies", "rules_closure_toolchains")

closure_repositories()
rules_closure_dependencies()

rules_closure_toolchains()
74 changes: 28 additions & 46 deletions bazel/closure_grpc_web_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@
# |closure_proto_library|, licensed under Apache 2.
# https://github.com/bazelbuild/rules_closure/blob/3555e5ba61fdcc17157dd833eaf7d19b313b1bca/closure/protobuf/closure_proto_library.bzl

load(
"@io_bazel_rules_closure//closure/compiler:closure_js_library.bzl",
"create_closure_js_library",
)
load(
"@io_bazel_rules_closure//closure/private:defs.bzl",
"CLOSURE_JS_TOOLCHAIN_ATTRS",
"unfurl",
)
load(
"@io_bazel_rules_closure//closure/protobuf:closure_proto_library.bzl",
"closure_proto_aspect",
)
"""Starlark rules for using gRPC-Web with Bazel and `rules_closure`."""

load("@io_bazel_rules_closure//closure/compiler:closure_js_library.bzl", "create_closure_js_library")
load("@io_bazel_rules_closure//closure/private:defs.bzl", "CLOSURE_JS_TOOLCHAIN_ATTRS", "unfurl") # buildifier: disable=bzl-visibility
load("@io_bazel_rules_closure//closure/protobuf:closure_proto_library.bzl", "closure_proto_aspect")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")

# This was borrowed from Rules Go, licensed under Apache 2.
# https://github.com/bazelbuild/rules_go/blob/67f44035d84a352cffb9465159e199066ecb814c/proto/compiler.bzl#L72
Expand Down Expand Up @@ -109,41 +102,35 @@ def _closure_grpc_web_library_impl(ctx):
# TODO(yannic): Revisit this restriction.
fail(_error_multiple_deps, "deps")

dep = ctx.attr.deps[0]

proto_info = ctx.attr.deps[0][ProtoInfo]
srcs = _generate_closure_grpc_web_srcs(
actions = ctx.actions,
protoc = ctx.executable._protoc,
protoc_gen_grpc_web = ctx.executable._protoc_gen_grpc_web,
import_style = ctx.attr.import_style,
mode = ctx.attr.mode,
sources = dep[ProtoInfo].direct_sources,
transitive_sources = dep[ProtoInfo].transitive_imports,
sources = proto_info.direct_sources,
transitive_sources = proto_info.transitive_imports,
)

deps = unfurl(ctx.attr.deps, provider = "closure_js_library")
deps += [
ctx.attr._grpc_web_abstractclientbase,
ctx.attr._grpc_web_clientreadablestream,
ctx.attr._grpc_web_error,
ctx.attr._grpc_web_grpcwebclientbase,
]

suppress = [
"misplacedTypeAnnotation",
"unusedPrivateMembers",
"reportUnknownTypes",
"strictDependencies",
"extraRequire",
]

deps.append(ctx.attr._runtime)
library = create_closure_js_library(
ctx = ctx,
srcs = srcs,
deps = deps,
suppress = suppress,
suppress = [
"misplacedTypeAnnotation",
"unusedPrivateMembers",
"reportUnknownTypes",
"strictDependencies",
"extraRequire",
],
lenient = False,
)

# `rules_closure` still uses the legacy provider syntax.
# buildifier: disable=rule-impl-return
return struct(
exports = library.exports,
closure_js_library = library.closure_js_library,
Expand All @@ -169,29 +156,24 @@ closure_grpc_web_library = rule(
values = ["grpcwebtext", "grpcweb"],
),

# internal only
# TODO(yannic): Convert to toolchain.
# Internal only.

# TODO(yannic): Switch to using `proto_toolchain` after
# https://github.com/bazelbuild/rules_proto/pull/25 lands.
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),

# TODO(yannic): Create `grpc_web_toolchain`.
"_protoc_gen_grpc_web": attr.label(
default = Label("//javascript/net/grpc/web:protoc-gen-grpc-web"),
executable = True,
cfg = "host",
),
"_grpc_web_abstractclientbase": attr.label(
default = Label("//javascript/net/grpc/web:abstractclientbase"),
),
"_grpc_web_clientreadablestream": attr.label(
default = Label("//javascript/net/grpc/web:clientreadablestream"),
),
"_grpc_web_error": attr.label(
default = Label("//javascript/net/grpc/web:error"),
),
"_grpc_web_grpcwebclientbase": attr.label(
default = Label("//javascript/net/grpc/web:grpcwebclientbase"),
"_runtime": attr.label(
default = Label("//javascript/net/grpc/web:closure_grpcweb_runtime"),
),
}, **CLOSURE_JS_TOOLCHAIN_ATTRS),
)
32 changes: 27 additions & 5 deletions javascript/net/grpc/web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_test")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library", "closure_js_test")
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "protoc-gen-grpc-web",
srcs = [
"grpc_generator.cc",
],
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:protoc_lib",
],
)

# Runtime for `import_style=closure` and `mode={grpcweb,grpcwebtext}`
closure_js_library(
name = "closure_grpcweb_runtime",
visibility = ["//visibility:public"],
exports = [
":abstractclientbase",
":clientreadablestream",
":error",
":grpcwebclientbase",
],
)

closure_js_library(
name = "abstractclientbase",
srcs = [
"abstractclientbase.js",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
":error",
Expand All @@ -31,13 +42,15 @@ closure_js_library(
srcs = [
"clientreadablestream.js",
],
visibility = ["//visibility:public"],
)

closure_js_library(
name = "error",
srcs = [
"error.js",
],
visibility = ["//visibility:public"],
deps = [
":metadata",
],
Expand All @@ -48,6 +61,7 @@ closure_js_library(
srcs = [
"generictransportinterface.js",
],
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_closure//closure/library/net:xhrio",
"@io_bazel_rules_closure//closure/library/net/streams:nodereadablestream",
Expand All @@ -63,6 +77,7 @@ closure_js_library(
"checkTypes",
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":abstractclientbase",
":grpcwebclientreadablestream",
Expand All @@ -81,6 +96,7 @@ closure_js_library(
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
":clientreadablestream",
":generictransportinterface",
Expand All @@ -105,6 +121,7 @@ closure_js_library(
suppress = [
"reportUnknownTypes",
],
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_closure//closure/library/asserts",
"@io_bazel_rules_closure//closure/library/net/streams:streamparser",
Expand All @@ -116,13 +133,15 @@ closure_js_library(
srcs = [
"metadata.js",
],
visibility = ["//visibility:public"],
)

closure_js_library(
name = "methoddescriptor",
srcs = [
"methoddescriptor.js",
],
visibility = ["//visibility:public"],
deps = [
":methodtype",
],
Expand All @@ -133,20 +152,23 @@ closure_js_library(
srcs = [
"methodtype.js",
],
visibility = ["//visibility:public"],
)

closure_js_library(
name = "status",
srcs = [
"status.js",
],
visibility = ["//visibility:public"],
)

closure_js_library(
name = "statuscode",
srcs = [
"statuscode.js",
],
visibility = ["//visibility:public"],
)

closure_js_test(
Expand Down
1 change: 1 addition & 0 deletions net/grpc/gateway/examples/echo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//bazel:closure_grpc_web_library.bzl", "closure_grpc_web_library")

proto_library(
Expand Down
10 changes: 3 additions & 7 deletions scripts/kokoro.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ cd "${REPO_DIR}"
make clean

# Lint bazel files.
BUILDIFIER_VERSION=0.22.0
BUILDIFIER_VERSION=1.0.0
wget "https://github.com/bazelbuild/buildtools/releases/download/${BUILDIFIER_VERSION}/buildifier"
chmod +x "./buildifier"
./buildifier -version
BAZEL_FILES=$(find "${REPO_DIR}" \
-not -path "${REPO_DIR}/.git/*" -and \
-not -path "${REPO_DIR}/third_party/*" -and \
\( -name "BUILD.bazel" -o -name "*.bzl" \))
./buildifier -mode check ${BAZEL_FILES[@]}
./buildifier --mode=check --lint=warn --warnings=all -r bazel javascript net
rm ./buildifier

# These programs need to be already installed
Expand All @@ -45,7 +41,7 @@ done
docker-compose -f advanced.yml build

# Run all bazel unit tests
BAZEL_VERSION=1.2.1
BAZEL_VERSION=2.2.0
wget https://github.com/bazelbuild/bazel/releases/download/"${BAZEL_VERSION}"/bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh
chmod +x ./bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh
./bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh --user
Expand Down

0 comments on commit ddb574f

Please sign in to comment.