Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions buf/internal/breaking.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ _TOOLCHAIN = str(Label("//tools/protoc-gen-buf-breaking:toolchain_type"))

def _buf_breaking_test_impl(ctx):
proto_infos = [t[ProtoInfo] for t in ctx.attr.targets]
config = json.encode({
config_map = {
"against_input": ctx.file.against.short_path,
"limit_to_input_files": ctx.attr.limit_to_input_files,
"exclude_imports": ctx.attr.exclude_imports,
"input_config": ctx.file.config.short_path,
"error_format": ctx.attr.error_format,
})
}
if ctx.attr.module != "":
config_map["module"] = ctx.attr.module
config = json.encode(config_map)
files_to_include = [ctx.file.against]
if ctx.file.config != None:
files_to_include.append(ctx.file.config)
Expand Down Expand Up @@ -69,6 +72,10 @@ buf_breaking_test = rule(
allow_single_file = True,
doc = """The `buf.yaml` file""",
),
"module": attr.string(
default = "",
doc = "The module to use in v2 config",
),
"limit_to_input_files": attr.bool(
default = False,
doc = """Checks are limited to input files. If a file gets deleted that will not be caught. Please refer to https://docs.buf.build/breaking/protoc-plugin for more details""",
Expand Down
11 changes: 9 additions & 2 deletions buf/internal/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ _TOOLCHAIN = str(Label("//tools/protoc-gen-buf-lint:toolchain_type"))

def _buf_lint_test_impl(ctx):
proto_infos = [t[ProtoInfo] for t in ctx.attr.targets]
config = json.encode({
config_map = {
"input_config": "" if ctx.file.config == None else ctx.file.config.short_path,
"error_format": ctx.attr.error_format,
})
}
if ctx.attr.module != "":
config_map["module"] = ctx.attr.module
config = json.encode(config_map)
files_to_include = []
if ctx.file.config != None:
files_to_include.append(ctx.file.config)
Expand Down Expand Up @@ -62,6 +65,10 @@ buf_lint_test = rule(
allow_single_file = True,
doc = "The `buf.yaml` file",
),
"module": attr.string(
default = "",
doc = "The module to use in v2 config",
),
"error_format": attr.string(
default = "",
doc = "error-format flag for buf lint: https://buf.build/docs/reference/cli/buf/lint#error-format",
Expand Down
2 changes: 2 additions & 0 deletions examples/v2/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Export the buf.yaml to make it available to rules in other packages.
exports_files(["buf.yaml"])
6 changes: 6 additions & 0 deletions examples/v2/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
8 changes: 8 additions & 0 deletions examples/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Workspace

This example demonstrates how to uses `rules_buf` with [workspaces](https://docs.buf.build/reference/workspaces).

- Export each `buf.yaml` ([fooapis](fooapis/BUILD.bazel#L4), [barapis](barapis/BUILD.bazel#L4))
- `proto_library` rules need an additional argument `strip_import_prefix` ([foo/v1](fooapis/foo/v1/BUILD.bazel#L7), [bar/v1](barapis/bar/v1/BUILD.bazel#L7))

Checkout the [gazelle example](../gazelle) for way to generate the rules.
19 changes: 19 additions & 0 deletions examples/v2/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local_repository(
name = "rules_buf",
path = "../..",
)

load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_toolchains")

rules_buf_dependencies()

rules_buf_toolchains(
sha256 = "f7e50b227bf171158ff4a80d22f274d0195487b618e0c4a57b3b0741a52453c2",
version = "v1.32.1",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()
29 changes: 29 additions & 0 deletions examples/v2/barapis/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2021-2023 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_breaking_test")


buf_breaking_test(
name = "barapis_proto_breaking",
# The Image file to check against.
against = "testdata/image.bin",
config = "//:buf.yaml",
module = "barapis",
# The proto_library targets to include.
# Refer to the documentation for more on this: https://docs.buf.build/build-systems/bazel#buf-breaking-test
targets = [
"//barapis/bar/v1:bar_proto",
],
)
30 changes: 30 additions & 0 deletions examples/v2/barapis/bar/v1/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2021-2023 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_lint_test")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "bar_proto",
srcs = ["bar.proto"],
strip_import_prefix = "/barapis",
visibility = ["//visibility:public"],
)

buf_lint_test(
name = "bar_proto_lint",
config = "//:buf.yaml",
module = "barapis",
targets = [":bar_proto"],
)
22 changes: 22 additions & 0 deletions examples/v2/barapis/bar/v1/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package bar.v1;

message Bar {
string id = 1;
string name = 2;
}
Binary file added examples/v2/barapis/testdata/image.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/v2/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by buf. DO NOT EDIT.
version: v2
17 changes: 17 additions & 0 deletions examples/v2/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: v2
modules:
- path: barapis
- path: fooapis
lint:
use:
- DEFAULT
except:
- FIELD_NOT_REQUIRED
- PACKAGE_NO_IMPORT_CYCLE
disallow_comment_ignores: true
breaking:
use:
- FILE
except:
- EXTENSION_NO_DELETE
- FIELD_SAME_DEFAULT
28 changes: 28 additions & 0 deletions examples/v2/fooapis/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2021-2023 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_breaking_test")

buf_breaking_test(
name = "fooapis_proto_breaking",
# The Image file to check against.
against = "testdata/image.bin",
config = "//:buf.yaml",
module = "fooapis",
# The proto_library targets to include.
# Refer to the documentation for more on this: https://docs.buf.build/build-systems/bazel#buf-breaking-test
targets = [
"//fooapis/foo/v1:foo_proto",
],
)
31 changes: 31 additions & 0 deletions examples/v2/fooapis/foo/v1/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021-2023 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_lint_test")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
strip_import_prefix = "/fooapis",
visibility = ["//visibility:public"],
deps = ["//barapis/bar/v1:bar_proto"],
)

buf_lint_test(
name = "foo_proto_lint",
config = "//:buf.yaml",
module = "fooapis",
targets = [":foo_proto"],
)
25 changes: 25 additions & 0 deletions examples/v2/fooapis/foo/v1/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package foo.v1;

import "bar/v1/bar.proto";

message Foo {
string id = 1;
string name = 2;
bar.v1.Bar bar = 3;
}
Binary file added examples/v2/fooapis/testdata/image.bin
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/workspace/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###############################################################################
# Bazel now uses Bzlmod by default to manage external dependencies.
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
#
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
###############################################################################
11 changes: 1 addition & 10 deletions examples/workspace/barapis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_buf//buf:defs.bzl", "buf_breaking_test", "buf_push")
load("@rules_buf//buf:defs.bzl", "buf_breaking_test")

# Export the buf.yaml to make it available to rules in other packages.
exports_files(["buf.yaml"])
Expand All @@ -28,12 +28,3 @@ buf_breaking_test(
"//barapis/bar/v1:bar_proto",
],
)

buf_push(
name = "push_barapis",
config = ":buf.yaml",
lock = "buf.lock",
targets = [
"//barapis/bar/v1:bar_proto",
],
)