diff --git a/scala/private/macros/bzlmod.bzl b/scala/private/macros/bzlmod.bzl new file mode 100644 index 000000000..7527fae28 --- /dev/null +++ b/scala/private/macros/bzlmod.bzl @@ -0,0 +1,23 @@ +"""Utilities for working with Bazel modules""" + +def apparent_repo_name(label_or_name): + """Return a repository's apparent repository name. + + Can be replaced with a future bazel-skylib implementation, if accepted into + that repo. + + Args: + label_or_name: a Label or repository name string + + Returns: + The apparent repository name + """ + repo_name = getattr(label_or_name, "repo_name", label_or_name) + + # Bazed on this pattern from the Bazel source: + # com.google.devtools.build.lib.cmdline.RepositoryName.VALID_REPO_NAME + for i in range(len(repo_name) - 1, -1, -1): + c = repo_name[i] + if not (c.isalnum() or c in "_-."): + return repo_name[i + 1:] + return repo_name diff --git a/scala/private/resources.bzl b/scala/private/resources.bzl index 1db14a822..086cd5a82 100644 --- a/scala/private/resources.bzl +++ b/scala/private/resources.bzl @@ -1,3 +1,5 @@ +load(":macros/bzlmod.bzl", "apparent_repo_name") + def paths(resources, resource_strip_prefix): """Return a list of path tuples (target, source) where: target - is a path in the archive (with given prefix stripped off) @@ -13,7 +15,13 @@ def paths(resources, resource_strip_prefix): def _target_path(resource, resource_strip_prefix): path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource) - return _strip_prefix(path, "/") + return _update_external_target_path(_strip_prefix(path, "/")) + +def _update_external_target_path(target_path): + if not target_path.startswith("external/"): + return target_path + prefix, repo_name, rest = target_path.split("/") + return "/".join([prefix, apparent_repo_name(repo_name), rest]) def _target_path_by_strip_prefix(resource, resource_strip_prefix): # Start from absolute resource path and then strip roots so we get to correct short path diff --git a/scala/scala_maven_import_external.bzl b/scala/scala_maven_import_external.bzl index e69388ced..7e575b406 100644 --- a/scala/scala_maven_import_external.bzl +++ b/scala/scala_maven_import_external.bzl @@ -35,6 +35,7 @@ the following macros are defined below that utilize jvm_import_external: - java_import_external - to demonstrate that the original functionality of `java_import_external` stayed intact. """ +load("//scala/private:macros/bzlmod.bzl", "apparent_repo_name") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "use_netrc") # https://github.com/bazelbuild/bazel/issues/13709#issuecomment-1336699672 @@ -136,7 +137,7 @@ def _jvm_import_external(repository_ctx): "", "alias(", " name = \"jar\",", - " actual = \"@%s\"," % repository_ctx.name, + " actual = \"@%s\"," % apparent_repo_name(repository_ctx.name), ")", "", ])) diff --git a/third_party/repositories/repositories.bzl b/third_party/repositories/repositories.bzl index c243c8e0b..0e5ce7cae 100644 --- a/third_party/repositories/repositories.bzl +++ b/third_party/repositories/repositories.bzl @@ -1,3 +1,4 @@ +load("//scala/private:macros/bzlmod.bzl", "apparent_repo_name") load( "//third_party/repositories:scala_2_11.bzl", _artifacts_2_11 = "artifacts", @@ -95,8 +96,10 @@ def repositories( default_artifacts = artifacts_by_major_scala_version[major_scala_version] artifacts = dict(default_artifacts.items() + overriden_artifacts.items()) for id in for_artifact_ids: + generated_rule_name = apparent_repo_name(id) + suffix _scala_maven_import_external( name = id + suffix, + generated_rule_name = generated_rule_name, artifact = artifacts[id]["artifact"], artifact_sha256 = artifacts[id]["sha256"], licenses = ["notice"], @@ -111,13 +114,13 @@ def repositories( # See: https://github.com/bazelbuild/rules_scala/pull/1573 # Hopefully we can deprecate and remove it one day. if suffix and scala_version == SCALA_VERSION: - _alias_repository(name = id, target = id + suffix) + _alias_repository(name = id, target = generated_rule_name) def _alias_repository_impl(rctx): """ Builds a repository containing just two aliases to the Scala Maven artifacts in the `target` repository. """ format_kwargs = { - "name": rctx.name, + "name": apparent_repo_name(rctx.name), "target": rctx.attr.target, } rctx.file("BUILD", """alias(