Skip to content

Commit

Permalink
Derive jvm_import debug label from explicit attribute
Browse files Browse the repository at this point in the history
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]: bazelbuild/bazel#23127
  • Loading branch information
plobsing committed Aug 4, 2024
1 parent 1c5f2eb commit 9042f39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
28 changes: 25 additions & 3 deletions private/dependency_tree_parser.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions private/rules/jvm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 9042f39

Please sign in to comment.