Skip to content

Commit d579e66

Browse files
committed
Use apparent_repo_name only on repository_ctx
Repurposed `apparent_repo_name` to only work for `repository_ctx` objects, not repository or module names in general. Removed `generated_rule_name` from `repositories.bzl`, since it's no longer necessary. Technically we could eliminate `apparent_repo_name` by making `generated_rule_name` a mandatory attribute of `_jvm_import_external`. However, this feels ultimately clunky and unnecessary. This update to `apparent_repo_name` required removing `_update_external_target_path` and updating `_target_path_by_default_prefixes` to remove `external/<canonical_repo_name>` prefixes. This represents a breaking change for files referencing `external/<repo_name>` paths, but the quick fix is to delete that prefix in the code. This matches the behavior in the same function regarding `resources/` and `java/` prefixes.
1 parent 6bcedbf commit d579e66

File tree

6 files changed

+27
-47
lines changed

6 files changed

+27
-47
lines changed

scala/private/macros/bzlmod.bzl

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
"""Utilities for working with Bazel modules"""
22

3-
_repo_attr = (
4-
"repo_name" if hasattr(Label("//:all"), "repo_name") else "workspace_name"
5-
)
6-
7-
def apparent_repo_name(label_or_name):
8-
"""Return a repository's apparent repository name.
9-
10-
Can be replaced with a future bazel-skylib implementation, if accepted into
11-
that repo.
3+
def apparent_repo_name(repository_ctx):
4+
"""Generates a repository's apparent name from a repository_ctx object.
125
136
Args:
14-
label_or_name: a Label or repository name string
7+
repository_ctx: a repository_ctx object
158
169
Returns:
17-
The apparent repository name
10+
An apparent repo name derived from repository_ctx.name
1811
"""
19-
repo_name = getattr(label_or_name, _repo_attr, label_or_name).lstrip("@")
20-
delimiter_indices = []
12+
repo_name = repository_ctx.name
2113

2214
# Bazed on this pattern from the Bazel source:
2315
# com.google.devtools.build.lib.cmdline.RepositoryName.VALID_REPO_NAME
24-
for i in range(len(repo_name)):
16+
for i in range(len(repo_name) - 1, -1, -1):
2517
c = repo_name[i]
2618
if not (c.isalnum() or c in "_-."):
27-
delimiter_indices.append(i)
28-
29-
if len(delimiter_indices) == 0:
30-
# Already an apparent repo name, apparently.
31-
return repo_name
32-
33-
if len(delimiter_indices) == 1:
34-
# The name is for a top level module, possibly containing a version ID.
35-
return repo_name[:delimiter_indices[0]]
19+
return repo_name[i + 1:]
3620

37-
return repo_name[delimiter_indices[-1] + 1:]
21+
return repo_name

scala/private/resources.bzl

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
load(":macros/bzlmod.bzl", "apparent_repo_name")
2-
31
def paths(resources, resource_strip_prefix):
42
"""Return a list of path tuples (target, source) where:
53
target - is a path in the archive (with given prefix stripped off)
@@ -15,13 +13,7 @@ def paths(resources, resource_strip_prefix):
1513

1614
def _target_path(resource, resource_strip_prefix):
1715
path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource)
18-
return _update_external_target_path(_strip_prefix(path, "/"))
19-
20-
def _update_external_target_path(target_path):
21-
if not target_path.startswith("external/"):
22-
return target_path
23-
prefix, repo_name, rest = target_path.split("/")
24-
return "/".join([prefix, apparent_repo_name(repo_name), rest])
16+
return _strip_prefix(path, "/")
2517

2618
def _target_path_by_strip_prefix(resource, resource_strip_prefix):
2719
# Start from absolute resource path and then strip roots so we get to correct short path
@@ -52,6 +44,13 @@ def _target_path_by_default_prefixes(resource):
5244
if rel_path:
5345
return rel_path
5446

47+
# Looking inside an external repository. Trim off both the "external/" and
48+
# the repository name components. Especially important under Bzlmod, because
49+
# the canonical repository name may change between versions.
50+
(dir_1, dir_2, rel_path) = path.partition("external/")
51+
if rel_path:
52+
return rel_path[rel_path.index("/"):]
53+
5554
# Both short_path and path have quirks we wish to avoid, in short_path there are times where
5655
# it is prefixed by `../` instead of `external/`. And in .path it will instead return the entire
5756
# bazel-out/... path, which is also wanting to be avoided. So instead, we return the short-path if

scala/scala_maven_import_external.bzl

+5-7
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,20 @@ def _jvm_import_external(repository_ctx):
6565
if (repository_ctx.attr.generated_linkable_rule_name and
6666
not repository_ctx.attr.neverlink):
6767
fail("Only use generated_linkable_rule_name if neverlink is set")
68-
name = (
69-
repository_ctx.attr.generated_rule_name or
70-
apparent_repo_name(repository_ctx.name)
71-
)
68+
repo_name = apparent_repo_name(repository_ctx)
69+
name = repository_ctx.attr.generated_rule_name or repo_name
7270
urls = repository_ctx.attr.jar_urls
7371
if repository_ctx.attr.jar_sha256:
7472
print("'jar_sha256' is deprecated. Please use 'artifact_sha256'")
7573
sha = repository_ctx.attr.jar_sha256 or repository_ctx.attr.artifact_sha256
76-
path = repository_ctx.name + ".jar"
74+
path = repo_name + ".jar"
7775
for url in urls:
7876
if url.endswith(".jar"):
7977
path = url[url.rindex("/") + 1:]
8078
break
8179
srcurls = repository_ctx.attr.srcjar_urls
8280
srcsha = repository_ctx.attr.srcjar_sha256
83-
srcpath = repository_ctx.name + "-src.jar" if srcurls else ""
81+
srcpath = repo_name + "-src.jar" if srcurls else ""
8482
coordinates = repository_ctx.attr.coordinates
8583
for url in srcurls:
8684
if url.endswith(".jar"):
@@ -140,7 +138,7 @@ def _jvm_import_external(repository_ctx):
140138
"",
141139
"alias(",
142140
" name = \"jar\",",
143-
" actual = \"@%s\"," % apparent_repo_name(repository_ctx.name),
141+
" actual = \"@%s\"," % repo_name,
144142
")",
145143
"",
146144
]))

test/src/main/scala/scalarules/test/resources/ScalaLibResourcesFromExternalDepTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ScalaLibResourcesFromExternalDepTest extends SpecWithJUnit {
88
"allow to load resources" >> {
99

1010
val expectedString = String.format("A resource%n"); //Using platform dependent newline (%n)
11-
get("/external/test_new_local_repo/resource.txt") must beEqualTo(expectedString)
11+
get("/resource.txt") must beEqualTo(expectedString)
1212

1313
}
1414
}

test/src/main/scala/scalarules/test/resources/ScalaLibResourcesFromExternalScalaTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ScalaLibResourcesFromExternalScalaTest extends AnyFunSuite {
66

77
test("Scala library depending on resources from external resource-only jar should allow to load resources") {
88
val expectedString = String.format("A resource%n"); //Using platform dependent newline (%n)
9-
assert(get("/external/test_new_local_repo/resource.txt") === expectedString)
9+
assert(get("/resource.txt") === expectedString)
1010
}
1111

1212
private def get(s: String): String =

third_party/repositories/repositories.bzl

+4-5
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ def repositories(
102102
major_scala_version.replace(".", "_"),
103103
))
104104

105-
generated_rule_name = apparent_repo_name(id) + suffix
105+
artifact_repo_name = id + suffix
106106
_scala_maven_import_external(
107-
name = id + suffix,
108-
generated_rule_name = generated_rule_name,
107+
name = artifact_repo_name,
109108
artifact = artifacts[id]["artifact"],
110109
artifact_sha256 = artifacts[id]["sha256"],
111110
licenses = ["notice"],
@@ -123,13 +122,13 @@ def repositories(
123122
# See: https://github.com/bazelbuild/rules_scala/pull/1573
124123
# Hopefully we can deprecate and remove it one day.
125124
if suffix and scala_version == SCALA_VERSION:
126-
_alias_repository(name = id, target = generated_rule_name)
125+
_alias_repository(name = id, target = artifact_repo_name)
127126

128127
def _alias_repository_impl(rctx):
129128
""" Builds a repository containing just two aliases to the Scala Maven artifacts in the `target` repository. """
130129

131130
format_kwargs = {
132-
"name": apparent_repo_name(rctx.name),
131+
"name": apparent_repo_name(rctx),
133132
"target": rctx.attr.target,
134133
}
135134
rctx.file("BUILD", """alias(

0 commit comments

Comments
 (0)