From 71e4061ace50c9f8b6a05a8368d15de0c009d87c Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 28 Mar 2024 08:24:50 -0700 Subject: [PATCH] feat: test bzlmod usage on CI (#16319) Closes #16319 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16319 from protocolbuffers:bcr d66845cfcbdbbc69f020e96be10af2846189f906 PiperOrigin-RevId: 619939078 --- .github/workflows/test_bazel.yml | 23 +++++++++++++++++++++++ .gitignore | 3 +++ MODULE.bazel | 21 +++++++++++++++++++++ examples/.bazelversion | 1 + examples/BUILD.bazel | 9 +++++++++ examples/MODULE.bazel | 13 +++++++++++++ examples/WORKSPACE | 9 +++++++++ examples/WORKSPACE.bzlmod | 1 + src/google/protobuf/util/BUILD.bazel | 4 ++-- 9 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test_bazel.yml create mode 100644 examples/.bazelversion create mode 100644 examples/MODULE.bazel create mode 100644 examples/WORKSPACE.bzlmod diff --git a/.github/workflows/test_bazel.yml b/.github/workflows/test_bazel.yml new file mode 100644 index 0000000000..2ba549a1e6 --- /dev/null +++ b/.github/workflows/test_bazel.yml @@ -0,0 +1,23 @@ +name: Bazel + +# Controls when the action will run. +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [main] + pull_request: + branches: [main] + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +concurrency: + # Cancel previous actions from the same PR or branch except 'main' branch. + # See https://docs.github.com/en/actions/using-jobs/using-concurrency and https://docs.github.com/en/actions/learn-github-actions/contexts for more info. + group: concurrency-group::${{ github.workflow }}::${{ github.event.pull_request.number > 0 && format('pr-{0}', github.event.pull_request.number) || github.ref_name }}${{ github.ref_name == 'main' && format('::{0}', github.run_id) || ''}} + cancel-in-progress: ${{ github.ref_name != 'main' }} + +jobs: + test: + uses: bazel-contrib/.github/.github/workflows/bazel.yaml@v6 + with: + folders: '["examples"]' diff --git a/.gitignore b/.gitignore index 37d7aebc74..5daf9c20e7 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,6 @@ compile_commands.json # Ignore GHA NodeJS files .github/**/node_modules/ + +# Ignore Bzlmod lock file until it is more stable +MODULE.bazel.lock diff --git a/MODULE.bazel b/MODULE.bazel index 2d43e46dfe..d972fef84f 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,2 +1,23 @@ # TODO: migrate all dependencies from WORKSPACE to MODULE.bazel # https://github.com/protocolbuffers/protobuf/issues/14313 +module( + name = "protobuf", + compatibility_level = 1, + version = "27.0", +) + +# LOWER BOUND dependency versions. +# Bzlmod follows MVS: +# https://bazel.build/versions/6.0.0/build/bzlmod#version-resolution +# Thus the highest version in their module graph is resolved. +bazel_dep(name = "abseil-cpp", repo_name = "com_google_absl", version = "20230802.0.bcr.1") +bazel_dep(name = "bazel_skylib", version = "1.4.1") +bazel_dep(name = "jsoncpp", version = "1.9.5") +bazel_dep(name = "rules_cc", version = "0.0.9") +bazel_dep(name = "rules_java", version = "4.0.0") +bazel_dep(name = "rules_jvm_external", version = "5.1") +bazel_dep(name = "rules_pkg", version = "0.7.0") +bazel_dep(name = "rules_proto", version = "4.0.0") +bazel_dep(name = "rules_python", version = "0.10.2") +bazel_dep(name = "platforms", version = "0.0.8") +bazel_dep(name = "zlib", version = "1.2.11") diff --git a/examples/.bazelversion b/examples/.bazelversion new file mode 100644 index 0000000000..a3fcc7121b --- /dev/null +++ b/examples/.bazelversion @@ -0,0 +1 @@ +7.1.0 diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index fe22d4c3a5..72186f65c8 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -4,6 +4,7 @@ # the WORKSPACE file in the same directory with this BUILD file for an # example. +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library") load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") @@ -113,6 +114,14 @@ java_binary( deps = [":addressbook_java_lite_proto"], ) +build_test( + name = "test", + targets = [ + ":add_person_cpp", + ":add_person_java", + ], +) + # Files included in all source distributions pkg_files( name = "dist_files", diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel new file mode 100644 index 0000000000..7ce5eec882 --- /dev/null +++ b/examples/MODULE.bazel @@ -0,0 +1,13 @@ +"""Bazel module dependencies""" +bazel_dep(name = "protobuf", version = "0.0.0", dev_dependency = True, repo_name = "com_google_protobuf") + +local_path_override( + module_name = "protobuf", + path = "..", +) + +bazel_dep(name = "bazel_skylib", version = "1.0.3") +bazel_dep(name = "rules_cc", version = "0.0.1") +bazel_dep(name = "rules_java", version = "7.3.0") +bazel_dep(name = "rules_pkg", version = "0.7.0") +bazel_dep(name = "rules_proto", version = "4.0.0") diff --git a/examples/WORKSPACE b/examples/WORKSPACE index e9811efff2..901b344be6 100644 --- a/examples/WORKSPACE +++ b/examples/WORKSPACE @@ -28,6 +28,15 @@ local_repository( path = "..", ) +# Needed because protobuf_deps brings rules_python 0.26.0 which is broken: +# https://github.com/bazelbuild/rules_python/issues/1543 +http_archive( + name = "rules_python", + sha256 = "5868e73107a8e85d8f323806e60cad7283f34b32163ea6ff1020cf27abef6036", + strip_prefix = "rules_python-0.25.0", + url = "https://github.com/bazelbuild/rules_python/releases/download/0.25.0/rules_python-0.25.0.tar.gz", +) + load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() diff --git a/examples/WORKSPACE.bzlmod b/examples/WORKSPACE.bzlmod new file mode 100644 index 0000000000..a2f9edebb8 --- /dev/null +++ b/examples/WORKSPACE.bzlmod @@ -0,0 +1 @@ +# This file shadows WORKSPACE in builds with `--enable_bzlmod` \ No newline at end of file diff --git a/src/google/protobuf/util/BUILD.bazel b/src/google/protobuf/util/BUILD.bazel index 0b894b7fa8..3edbc4aeae 100644 --- a/src/google/protobuf/util/BUILD.bazel +++ b/src/google/protobuf/util/BUILD.bazel @@ -162,8 +162,8 @@ cc_library( hdrs = ["time_util.h"], strip_include_prefix = "/src", visibility = [ - "//:__subpackages__", - "@com_google_protobuf_examples//:__subpackages__", + # Needed when building from within the /examples repository + "//visibility:public", ], deps = [ "//src/google/protobuf",