Bazel: add in_gerrit_tree build setting for Gerrit-tree-only checks Add a typed Bazel build setting @com_googlesource_gerrit_bazlets//flags:in_gerrit_tree and expose tools:in_gerrit_tree_enabled() helper for use with target_compatible_with. This allows plugins to declare tests and guardrails that are meaningful only when built inside the Gerrit source tree (e.g. checks that compare against //:release.war.jars.txt), while remaining automatically SKIPPED in standalone plugin workspaces. The Gerrit tree enables these checks by setting: common --@com_googlesource_gerrit_bazlets//flags:in_gerrit_tree=true in its .bazelrc. Change-Id: I8106fd8576dbe5a66651ff4723337e836850e1ac
diff --git a/MODULE.bazel b/MODULE.bazel index 3c2f98d..7da95a7 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -4,6 +4,7 @@ version = GERRIT_API_VERSION, ) +bazel_dep(name = "bazel_skylib", version = "1.9.0") bazel_dep(name = "rules_java", version = "7.6.1") bazel_dep(name = "rules_python", version = "1.7.0")
diff --git a/README.md b/README.md index 7261f9c..e82fe1b 100644 --- a/README.md +++ b/README.md
@@ -6,6 +6,7 @@ <li><a href="#gerrit_plugin">gerrit_plugin</a></li> <li><a href="#runtime_jars_allowlist_test">runtime_jars_allowlist_test</a></li> <li><a href="#runtime_jars_overlap_test">runtime_jars_overlap_test</a></li> + <li><a href="#gerrit_tree_only_checks">Gerrit-tree-only checks</a></li> </ul> </div> @@ -316,3 +317,39 @@ On failure, the test prints the overlapping normalized jar IDs and exits non-zero. + +<a name="gerrit_tree_only_checks"></a> +## Gerrit-tree-only checks + +Some plugin tests and guardrails are meaningful only when the plugin is built +inside the Gerrit source tree (e.g. checks that compare against +`//:release.war.jars.txt`). Such checks should run when building in-tree, but be +automatically skipped in standalone plugin workspaces. + +Bazlets provides a typed Bazel build setting and helper to support this pattern. + +### Usage in plugin BUILD files + +```python +load( + "@com_googlesource_gerrit_bazlets//tools:in_gerrit_tree.bzl", + "in_gerrit_tree_enabled", +) + +runtime_jars_overlap_test( + name = "no_overlap_with_gerrit", + against = "//:release.war.jars.txt", + target = ":my_plugin", + target_compatible_with = in_gerrit_tree_enabled(), +) +``` + +In the Gerrit source tree, enable these checks by setting the following in +`.bazelrc`: + +``` +common --@com_googlesource_gerrit_bazlets//flags:in_gerrit_tree=true +``` + +Standalone plugin workspaces should not set this flag. In that case, the +corresponding targets are marked incompatible and reported as SKIPPED by Bazel.
diff --git a/flags/BUILD.bazel b/flags/BUILD.bazel new file mode 100644 index 0000000..dfeef05 --- /dev/null +++ b/flags/BUILD.bazel
@@ -0,0 +1,16 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") + +package(default_visibility = ["//visibility:public"]) + +# Set to true by the Gerrit source tree .bazelrc. Defaults to false for +# standalone plugin workspaces. +bool_flag( + name = "in_gerrit_tree", + build_setting_default = False, +) + +config_setting( + name = "in_gerrit_tree_enabled", + flag_values = {":in_gerrit_tree": "true"}, +) +
diff --git a/tools/in_gerrit_tree.bzl b/tools/in_gerrit_tree.bzl new file mode 100644 index 0000000..e878d97 --- /dev/null +++ b/tools/in_gerrit_tree.bzl
@@ -0,0 +1,14 @@ +def in_gerrit_tree_enabled(): + """Constraints for target_compatible_with to enable Gerrit-tree-only targets. + + Use as: + target_compatible_with = in_gerrit_tree_enabled() + + In the Gerrit source tree (flag true), targets remain compatible and run. + In standalone plugin workspaces (flag false), targets are marked + incompatible and are reported as SKIPPED by Bazel. + """ + return select({ + "@com_googlesource_gerrit_bazlets//flags:in_gerrit_tree_enabled": [], + "//conditions:default": ["@platforms//:incompatible"], + })