Skip to content

Commit

Permalink
fix: Deduplicate deps while amending (#1266)
Browse files Browse the repository at this point in the history
* fix: Deduplicate deps while amending

* Add comment

* Remove unnecessary code

* Add test case
  • Loading branch information
honnix authored Nov 13, 2024
1 parent 3ed7d65 commit d78a1c3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ dev_maven.install(
"build.buf:protovalidate:0.1.9",
# https://github.com/bazelbuild/rules_jvm_external/issues/1250
"com.github.spotbugs:spotbugs:4.7.0",
# https://github.com/bazelbuild/rules_jvm_external/issues/1267
"org.mockito:mockito-core:pom:3.3.3",
],
fail_if_repin_required = True,
generate_compat_repositories = True,
Expand Down
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ maven_install(
"build.buf:protovalidate:0.1.9",
# https://github.com/bazelbuild/rules_jvm_external/issues/1250
"com.github.spotbugs:spotbugs:4.7.0",
# https://github.com/bazelbuild/rules_jvm_external/issues/1267
"org.mockito:mockito-core:pom:3.3.3",
],
fail_if_repin_required = True,
generate_compat_repositories = True,
Expand Down
13 changes: 13 additions & 0 deletions private/rules/coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,13 @@ def rewrite_files_attribute_if_necessary(repository_ctx, dep_tree):
# `pinned_maven_install`. Oh well, let's just do this the manual way.
if dep["file"].endswith(".pom"):
jar_path = dep["file"].removesuffix(".pom") + ".jar"

# The same artifact can being depended on via pom and jar at different
# places in the tree. In such case, we deduplicate it so that 2
# entries do not reference the same file, which will otherwise lead
# in symlink error because of existing file down the road.
if is_dep(jar_path, amended_deps):
continue
if repository_ctx.path(jar_path).exists:
dep["file"] = jar_path

Expand All @@ -965,6 +972,12 @@ def rewrite_files_attribute_if_necessary(repository_ctx, dep_tree):

return dep_tree

def is_dep(jar_path, deps):
for dep in deps:
if jar_path == dep.get("file", None):
return True
return False

def remove_prefix(s, prefix):
if s.startswith(prefix):
return s[len(prefix):]
Expand Down
15 changes: 13 additions & 2 deletions tests/bazel_run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function test_transitive_dependency_with_type_of_pom {
bazel query @transitive_dependency_with_type_of_pom//:org_javamoney_moneta_moneta_core >> "$TEST_LOG" 2>&1
}

function test_when_both_pom_and_artifact_are_available_jar_artifact_is_present {
function test_when_both_pom_and_jar_artifact_are_available_jar_artifact_is_present {
# The `maven_coordinates` of the target should be set to the coordinates of the jar
# If the `pom` classifier is asked for, something has gone wrong and no results will
# match
Expand All @@ -268,6 +268,16 @@ function test_when_both_pom_and_artifact_are_available_jar_artifact_is_present {
expect_log "@regression_testing_coursier//:com_github_spotbugs_spotbugs"
}

function test_when_both_pom_and_jar_artifact_are_dependencies_jar_artifact_is_present {
# The `maven_coordinates` of the target should be set to the coordinates of the jar
# If both the `jar` and `pom` classifiers are asked for, something has gone wrong and no results
# will match
bazel query 'attr(tags, "org.mockito:mockito-core:3.3.3", @regression_testing_coursier//:org_mockito_mockito_core)' >> "$TEST_LOG" 2>&1

expect_log "@regression_testing_coursier//:org_mockito_mockito_core"
}


TESTS=(
"test_maven_resolution"
"test_dependency_aggregation"
Expand All @@ -286,7 +296,8 @@ TESTS=(
"test_v1_lock_file_format"
"test_dependency_pom_exclusion"
"test_transitive_dependency_with_type_of_pom"
"test_when_both_pom_and_artifact_are_available_jar_artifact_is_present"
"test_when_both_pom_and_jar_artifact_are_available_jar_artifact_is_present"
"test_when_both_pom_and_jar_artifact_are_dependencies_jar_artifact_is_present"
)

function run_tests() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": 1906932742,
"__RESOLVED_ARTIFACTS_HASH": 1555926687,
"__INPUT_ARTIFACTS_HASH": -1504192575,
"__RESOLVED_ARTIFACTS_HASH": 2071728921,
"artifacts": {
"android.arch.core:common": {
"shasums": {
Expand Down Expand Up @@ -618,9 +618,9 @@
},
"com.nimbusds:nimbus-jose-jwt": {
"shasums": {
"jar": "978cc75ee13ef021e288bf1aa78d8ad9f69cee558119a09a5a64e7fc92085000"
"jar": "388f3fcec4f7ce41557b0271033c978c30f386cc302a6783be181edd599e9dff"
},
"version": "9.41.1"
"version": "9.45"
},
"com.nimbusds:oauth2-oidc-sdk": {
"shasums": {
Expand Down

0 comments on commit d78a1c3

Please sign in to comment.