From 9042f39167882e3904be53f951040f81b2311ae5 Mon Sep 17 00:00:00 2001 From: Peter Lobsinger Date: Sun, 4 Aug 2024 13:54:12 -0700 Subject: [PATCH] Derive jvm_import debug label from explicit attribute The delimiter used in Bzlmod for repo names has never been stable and will be changing soon.[^1] String-parsing to derive shorter, user-friendly names for labels will be broken by this change and is brittle in general. Instead, passing the right values down from the contexts where the true values are known is preferred. [^1]: https://github.com/bazelbuild/bazel/issues/23127 --- private/dependency_tree_parser.bzl | 28 +++++++++++++++++++++++++--- private/rules/jvm_import.bzl | 8 ++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl index 9d622632..e560a3d1 100644 --- a/private/dependency_tree_parser.bzl +++ b/private/dependency_tree_parser.bzl @@ -288,7 +288,29 @@ genrule( target_import_string.append("\tvisibility = [%s]," % (",".join(["\"%s\"" % t for t in target_visibilities]))) alias_visibility = "\tvisibility = [%s],\n" % (",".join(["\"%s\"" % t for t in target_visibilities])) - # 9. Finish the java_import rule. + # 9. Add user-friendly repository name to support diagnostics. + # Only supported by jvm_import. + # + # java_import( + # name = "org_hamcrest_hamcrest_library", + # jars = ["https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar"], + # srcjar = "https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3-sources.jar", + # deps = [ + # ":org_hamcrest_hamcrest_core", + # ], + # tags = [ + # "maven_coordinates=org.hamcrest:hamcrest.library:1.3"], + # "maven_url=https://repo1.maven.org/maven/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar", + # "maven:compile-only", + # ], + # neverlink = True, + # testonly = True, + # visibility = ["//visibility:public"], + # user_provided_repo_name = "maven", + if import_rule == "jvm_import": + target_import_string.append("\tuser_provided_repo_name = \"%s\"," % repository_ctx.attr.user_provided_name) + + # 10. Finish the java_import rule. # # java_import( # name = "org_hamcrest_hamcrest_library", @@ -309,7 +331,7 @@ genrule( to_return.append("\n".join(target_import_string)) - # 10. Create a versionless alias target + # 11. Create a versionless alias target # # alias( # name = "org_hamcrest_hamcrest_library_1_3", @@ -335,7 +357,7 @@ processor_class = "{processor_class}", ), ) - # 11. If using maven_install.json, use a genrule to copy the file from the http_file + # 12. If using maven_install.json, use a genrule to copy the file from the http_file # repository into this repository. # # genrule( diff --git a/private/rules/jvm_import.bzl b/private/rules/jvm_import.bzl index 26292aed..8b28fcb9 100644 --- a/private/rules/jvm_import.bzl +++ b/private/rules/jvm_import.bzl @@ -14,13 +14,10 @@ def _jvm_import_impl(ctx): if len(ctx.files.jars) != 1: fail("Please only specify one jar to import in the jars attribute.") - # With `bzlmod` enabled, workspace names end up being `~` separated. For the - # user-visible workspace name, we need the final part of the name - visible_name = ctx.label.workspace_name.rpartition("~")[2] label = "@{workspace_name}//{package}:{name}".format( name = ctx.label.name, package = ctx.label.package, - workspace_name = visible_name, + workspace_name = ctx.attr.user_provided_repo_name, ) injar = ctx.files.jars[0] @@ -100,6 +97,9 @@ jvm_import = rule( "neverlink": attr.bool( default = False, ), + "user_provided_repo_name": attr.string( + mandatory = True, + ), "_add_jar_manifest_entry": attr.label( executable = True, cfg = "exec",