Skip to content

Commit

Permalink
Fix up logic to work with Bazel 5 too
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Apr 2, 2024
1 parent 166a027 commit ab33bc8
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions private/rules/maven_bom.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ load(":maven_bom_fragment.bzl", "MavenBomFragmentInfo")
load(":maven_publish.bzl", "maven_publish")
load(":maven_utils.bzl", "generate_pom", "unpack_coordinates")

_NON_EXISTENT_LABEL = Label("//:thisdoesnotexistinrulesjvmexternal")

def _is_using_bzlmod():
# There's no easy way in a macro to tell if you're using bzlmod. We can't
# depend on Bazel version number because `--noenable_bzlmod` may have been
# used. Instead, try and stringify a label we know doesn't exist. When
# bzlmod is in play, we'll get `[unknown repo` in the value. When it is not
# we get the label as we expect it to be.
stringified = str(Label("@doesnotexistandneverwillipromiserulesjvmexternal//:foo"))
return "[unknown repo " in stringified
return str(_NON_EXISTENT_LABEL).startswith("@@")

def _label(label_or_string):
if type(label_or_string) == "Label":
Expand Down Expand Up @@ -175,7 +176,19 @@ def maven_bom(
fragments = []
labels = [_label(je) for je in java_exports]

fragments = [l.same_package_label("%s.bom-fragment" % l.name) for l in labels]
# `same_package_label` doesn't exist in Bazel 5, but we still support it
# so we check the version here to call a non-deprecated API in recent
# Bazel versions, or the older (deprecated) API in Bazel 5. Now, normally
# we'd use Skylib's `version` but that needs `native.bazel_version` to be
# present, and that's not available to macros. Instead, see whether what
# we need is present. Pulling in `bazel_features` for this check seems like
# overkill, especially since we'd need to land a patch in it before we
# could check this feature. Doing this the Not Invented Here way for now.
if "same_package_label" in dir(_NON_EXISTENT_LABEL):
fragments = [l.same_package_label("%s.bom-fragment" % l.name) for l in labels]
else:
# TODO: Drop this branch once we drop Bazel 5 support
fragments = [l.relative(":%s.bom-fragment" % l.name) for l in labels]

_maven_bom(
name = name,
Expand Down

0 comments on commit ab33bc8

Please sign in to comment.