-
Notifications
You must be signed in to change notification settings - Fork 38
Add bzlmod support #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| "Bazel module definition for bzlmod" | ||
| module( | ||
| name = "rules_buf", | ||
| version = "0.0.0", # Replaced when publishing | ||
| compatibility_level = 1, | ||
| ) | ||
|
|
||
| bazel_dep(name = "platforms", version = "0.0.4") | ||
| # Only needed because rules_proto doesn't provide the protoc toolchain yet. | ||
| # TODO: remove in the future | ||
| bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf") | ||
| bazel_dep(name = "rules_proto", version = "5.3.0-21.7") | ||
|
|
||
| # We depend on gazelle at runtime to generate our proto_library rules | ||
| bazel_dep(name = "gazelle", version = "0.33.0") | ||
|
|
||
| # ... and then reach inside to get the gazelle binary from this repository | ||
| non_module_deps = use_extension("@gazelle//internal/bzlmod:non_module_deps.bzl", "non_module_deps") | ||
| use_repo(non_module_deps, "bazel_gazelle_go_repository_tools") | ||
|
|
||
| ext = use_extension("//buf:extensions.bzl", "buf") | ||
| use_repo(ext, "rules_buf_toolchains") | ||
| register_toolchains("@rules_buf_toolchains//:all") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Define module extensions for using rules_buf with bzlmod. | ||
| See https://bazel.build/docs/bzlmod#extension-definition | ||
| """ | ||
|
|
||
| load("//buf/internal:toolchain.bzl", "buf_download_releases") | ||
| load("//buf/internal:repo.bzl", "buf_dependencies") | ||
|
|
||
| # NB: this should be updated periodically | ||
| _DEFAULT_VERSION = "v1.27.0" | ||
| _DEFAULT_TOOLCHAIN_NAME = "rules_buf_toolchains" | ||
| _DEFAULT_DEPS = "buf_deps" | ||
|
|
||
| dependency = tag_class(attrs = { | ||
| "name": attr.string(doc = "name of resulting deps repo", default = _DEFAULT_DEPS), | ||
| "module": attr.string(doc = "A module name from the Buf Schema Registry, see https://buf.build/docs/bsr/module/manage"), | ||
| }) | ||
|
|
||
| toolchains = tag_class(attrs = { | ||
| "name": attr.string(doc = "name of resulting buf toolchains repo", default = _DEFAULT_TOOLCHAIN_NAME), | ||
| "version": attr.string(doc = "Version of the buf tool, see https://github.com/bufbuild/buf/releases"), | ||
| }) | ||
|
|
||
| def _extension_impl(module_ctx): | ||
| registrations = {} | ||
| dependencies = {} | ||
|
|
||
| # Iterate over the global modules registered either directly by the user | ||
| # or transitively by some other bazel module they use. | ||
| for mod in module_ctx.modules: | ||
|
|
||
| # collect all buf.dependency tags, group by name of resulting buf_dependencies repo | ||
| for dependency in mod.tags.dependency: | ||
| if dependency.name not in dependencies.keys(): | ||
| dependencies[dependency.name] = [] | ||
| dependencies[dependency.name].append(dependency.module) | ||
|
|
||
| # collect all toolchain versions, group by name of toolchain repo | ||
| for toolchains in mod.tags.toolchains: | ||
| if toolchains.name != _DEFAULT_TOOLCHAIN_NAME and not mod.is_root: | ||
| fail("""\ | ||
| Only the root module may override the default name for the buf toolchains. | ||
| This prevents conflicting registrations in the global namespace of external repos. | ||
| """) | ||
| if toolchains.name not in registrations.keys(): | ||
| registrations[toolchains.name] = [] | ||
| registrations[toolchains.name].append(toolchains.version) | ||
|
|
||
| # Don't require that the user manually registers a toolchain | ||
| if len(registrations) == 0: | ||
| registrations = {_DEFAULT_TOOLCHAIN_NAME: [_DEFAULT_VERSION]} | ||
|
|
||
| for name, versions in registrations.items(): | ||
| if len(versions) > 1: | ||
| # TODO: should be semver-aware, using MVS | ||
| selected = sorted(versions, reverse = True)[0] | ||
|
|
||
| # buildifier: disable=print | ||
| print("NOTE: buf toolchains {} has multiple versions {}, selected {}".format(name, versions, selected)) | ||
|
alexeagle marked this conversation as resolved.
|
||
| else: | ||
| selected = versions[0] | ||
| buf_download_releases( | ||
| name = name, | ||
| version = selected, | ||
| ) | ||
|
|
||
| for name, modules in dependencies.items(): | ||
| buf_dependencies( | ||
| name = name, | ||
| modules = modules, | ||
| ) | ||
|
|
||
| buf = module_extension( | ||
| implementation = _extension_impl, | ||
| tag_classes = { | ||
| "dependency": dependency, | ||
| "toolchains": toolchains, | ||
| }, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| common --enable_bzlmod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| load("@rules_buf//buf:defs.bzl", "buf_lint_test") | ||
| load("@rules_proto//proto:defs.bzl", "proto_library") | ||
|
|
||
| exports_files(["buf.yaml"], visibility = ["//visibility:public"]) | ||
|
|
||
| proto_library( | ||
| name = "unused", | ||
| srcs = ["unused.proto"], | ||
| ) | ||
|
|
||
| proto_library( | ||
| name = "foo_proto", | ||
| srcs = ["file.proto"], | ||
| deps = [ | ||
| # imports "validate/validate.proto" | ||
| "@buf_deps//validate:validate_proto", | ||
| ":unused", | ||
| ], | ||
| ) | ||
|
|
||
| buf_lint_test( | ||
| name = "foo_proto_lint", | ||
| targets = [":foo_proto"], | ||
| config = "buf.yaml", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| "Bazel dependencies" | ||
| bazel_dep(name = "rules_buf", dev_dependency = True, version = "0.0.0") | ||
|
|
||
| local_path_override( | ||
| module_name = "rules_buf", | ||
| path = "../..", | ||
| ) | ||
|
|
||
| buf = use_extension("@rules_buf//buf:extensions.bzl", "buf") | ||
|
|
||
| # Override the default version of buf | ||
| buf.toolchains(version = "v1.26.0") | ||
|
|
||
| # See https://buf.build/docs/build-systems/bazel#buf-dependencies | ||
| buf.dependency(module = "buf.build/envoyproxy/protoc-gen-validate:eac44469a7af47e7839a7f1f3d7ac004") | ||
| buf.dependency(module = "buf.build/acme/petapis:7abdb7802c8f4737a1a23a35ca8266ef") | ||
|
|
||
| # Allow references to labels under @buf_deps | ||
| use_repo(buf, "buf_deps") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Marker that this folder is the root of a Bazel workspace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| version: v1 | ||
| lint: | ||
| use: | ||
| - IMPORT_USED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| import "unused.proto"; | ||
| import "validate/validate.proto"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| syntax = "proto3"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.