Skip to content

Commit 8388bef

Browse files
authored
Ensure that the pom classifier isn't always downloaded (#1251)
Closes #1250
1 parent 3df00a6 commit 8388bef

File tree

5 files changed

+477
-37
lines changed

5 files changed

+477
-37
lines changed

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ dev_maven.install(
552552
"androidx.arch.core:core-testing:aar:2.1.0",
553553
# https://github.com/bazelbuild/rules_jvm_external/issues/1028
554554
"build.buf:protovalidate:0.1.9",
555+
# https://github.com/bazelbuild/rules_jvm_external/issues/1250
556+
"com.github.spotbugs:spotbugs:4.7.0",
555557
],
556558
fail_if_repin_required = True,
557559
generate_compat_repositories = True,

WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ maven_install(
276276
"androidx.arch.core:core-testing:aar:2.1.0",
277277
# https://github.com/bazelbuild/rules_jvm_external/issues/1028
278278
"build.buf:protovalidate:0.1.9",
279+
# https://github.com/bazelbuild/rules_jvm_external/issues/1250
280+
"com.github.spotbugs:spotbugs:4.7.0",
279281
],
280282
fail_if_repin_required = True,
281283
generate_compat_repositories = True,

private/rules/coursier.bzl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,43 @@ def make_coursier_dep_tree(
925925
if (exec_result.return_code != 0):
926926
fail("Error while fetching artifact with coursier: " + exec_result.stderr)
927927

928-
return deduplicate_and_sort_artifacts(
928+
dep_tree = deduplicate_and_sort_artifacts(
929929
json.decode(repository_ctx.read(repository_ctx.path("dep-tree.json"))),
930930
artifacts,
931931
excluded_artifacts,
932932
_is_verbose(repository_ctx),
933933
)
934+
return rewrite_files_attribute_if_necessary(repository_ctx, dep_tree)
935+
936+
def rewrite_files_attribute_if_necessary(repository_ctx, dep_tree):
937+
# There are cases where `coursier` will download both the pom and the
938+
# jar but will include the path to the pom instead of the jar in the
939+
# `file` attribute. This differs from both gradle and maven. Massage the
940+
# `file` attributes if necessary.
941+
# https://github.com/bazelbuild/rules_jvm_external/issues/1250
942+
amended_deps = []
943+
for dep in dep_tree["dependencies"]:
944+
if not dep.get("file", None):
945+
amended_deps.append(dep)
946+
continue
947+
948+
# You'd think we could use skylib here to do the heavy lifting, but
949+
# this is a dependency of `maven_install`, which is loaded in the
950+
# `repositories.bzl` file. That means we can't rely on anything that
951+
# comes from skylib yet, since the repo isn't loaded. If we could
952+
# call `maven_install` from `setup.bzl`, we'd be fine, but we can't
953+
# do that because then there'd be nowhere to call the
954+
# `pinned_maven_install`. Oh well, let's just do this the manual way.
955+
if dep["file"].endswith(".pom"):
956+
jar_path = dep["file"].removesuffix(".pom") + ".jar"
957+
if repository_ctx.path(jar_path).exists:
958+
dep["file"] = jar_path
959+
960+
amended_deps.append(dep)
961+
962+
dep_tree["dependencies"] = amended_deps
963+
964+
return dep_tree
934965

935966
def remove_prefix(s, prefix):
936967
if s.startswith(prefix):
@@ -1032,7 +1063,7 @@ def _coursier_fetch_impl(repository_ctx):
10321063
# This file comes from maven local, so handle it in two different ways depending if
10331064
# dependency pinning is used:
10341065
# a) If the repository is unpinned, we keep the file as is, but clear the url to skip it
1035-
# b) Otherwise, we clear the url and also simlink the file from the maven local directory
1066+
# b) Otherwise, we clear the url and also symlink the file from the maven local directory
10361067
# to file within the repository rule workspace
10371068
print("Assuming maven local for artifact: %s" % artifact["coord"])
10381069
artifact.update({"url": None})

tests/bazel_run_tests.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ function test_transitive_dependency_with_type_of_pom {
259259
bazel query @transitive_dependency_with_type_of_pom//:org_javamoney_moneta_moneta_core >> "$TEST_LOG" 2>&1
260260
}
261261

262+
function test_when_both_pom_and_artifact_are_available_jar_artifact_is_present {
263+
# The `maven_coordinates` of the target should be set to the coordinates of the jar
264+
# If the `pom` classifier is asked for, something has gone wrong and no results will
265+
# match
266+
bazel query 'attr(tags, "com.github.spotbugs:spotbugs:4.7.0", @regression_testing_coursier//:com_github_spotbugs_spotbugs)' >> "$TEST_LOG" 2>&1
267+
268+
expect_log "@regression_testing_coursier//:com_github_spotbugs_spotbugs"
269+
}
270+
262271
TESTS=(
263272
"test_maven_resolution"
264273
"test_dependency_aggregation"
@@ -277,6 +286,7 @@ TESTS=(
277286
"test_v1_lock_file_format"
278287
"test_dependency_pom_exclusion"
279288
"test_transitive_dependency_with_type_of_pom"
289+
"test_when_both_pom_and_artifact_are_available_jar_artifact_is_present"
280290
)
281291

282292
function run_tests() {

0 commit comments

Comments
 (0)