Skip to content

Commit 1bcdaea

Browse files
authored
Require force_version for root module artifact to take precedence (#1423)
This addresses concerns raised by owners of popular bazel modules that their inputs to the default namespace would be overridden by accident.
1 parent c97cd6a commit 1bcdaea

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,11 @@ dev_maven.install(
876876
lock_file = "//tests/custom_maven_install:root_wins_install.json",
877877
resolver = "maven",
878878
)
879+
dev_maven.amend_artifact(
880+
name = "root_wins",
881+
coordinates = "io.netty:netty-buffer",
882+
force_version = True,
883+
)
879884

880885
bazel_dep(name = "root_wins_layer", version = "0.0.0", dev_dependency = True)
881886
local_path_override(

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
1616
maven.artifact(
1717
artifact = "guava",
1818
exclusions = ["com.google.code.findbugs:jsr305"],
19+
force_version = True,
1920
group = "com.google.guava",
2021
version = "31.1-jre",
2122
)

examples/bzlmod/maven_install.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": -1307306091,
4-
"__RESOLVED_ARTIFACTS_HASH": -1663972293,
3+
"__INPUT_ARTIFACTS_HASH": -909275114,
4+
"__RESOLVED_ARTIFACTS_HASH": 2025923254,
55
"conflict_resolution": {
6-
"com.google.errorprone:error_prone_annotations:2.5.1": "com.google.errorprone:error_prone_annotations:2.18.0",
7-
"com.google.guava:guava:31.1-jre": "com.google.guava:guava:32.0.1-jre"
6+
"com.google.errorprone:error_prone_annotations:2.5.1": "com.google.errorprone:error_prone_annotations:2.18.0"
87
},
98
"artifacts": {
109
"biz.aQute.bnd:biz.aQute.bnd.util": {
@@ -156,10 +155,10 @@
156155
},
157156
"com.google.guava:guava": {
158157
"shasums": {
159-
"jar": "bd7fa227591fb8509677d0d1122cf95158f3b8a9f45653f58281d879f6dc48c5",
160-
"sources": "9105dfc522fb440b39ff8da07cc56aacf65a9f765044c7803a9f32e36e05a22b"
158+
"jar": "a42edc9cab792e39fe39bb94f3fca655ed157ff87a8af78e1d6ba5b07c4a00ab",
159+
"sources": "8ab1853cdaf936ec88be80c17302b7c20abafbd4f54d4fb54d7011c529e3a44a"
161160
},
162-
"version": "32.0.1-jre"
161+
"version": "31.1-jre"
163162
},
164163
"com.google.guava:guava-testlib": {
165164
"shasums": {
@@ -858,7 +857,6 @@
858857
"org.checkerframework:checker-compat-qual"
859858
],
860859
"com.google.guava:guava": [
861-
"com.google.code.findbugs:jsr305",
862860
"com.google.errorprone:error_prone_annotations",
863861
"com.google.guava:failureaccess",
864862
"com.google.guava:listenablefuture",

private/extensions/maven.bzl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,36 @@ def _find_duplicate_artifacts_across_submodules(non_root_artifacts, root_maven_m
261261
return duplicates
262262

263263
def _deduplicate_artifacts_with_root_priority(name, root_artifacts, non_root_artifacts):
264-
"""Deduplicate artifacts, giving priority to root module artifacts."""
264+
"""Deduplicate artifacts, giving priority to root module artifacts with force_version set."""
265265

266-
# Collect maven modules from root artifacts (handle mixed types)
267-
root_maven_modules = []
266+
# Collect maven modules from root artifacts that have force_version = True
267+
root_maven_modules_with_force_version = []
268+
for artifact in root_artifacts:
269+
if getattr(artifact, "force_version", False):
270+
maven_module = _get_maven_module(artifact)
271+
if maven_module not in root_maven_modules_with_force_version:
272+
root_maven_modules_with_force_version.append(maven_module)
273+
274+
# Collect all maven modules from root artifacts (for duplicate detection)
275+
all_root_maven_modules = []
268276
for artifact in root_artifacts:
269277
maven_module = _get_maven_module(artifact)
270-
if maven_module not in root_maven_modules:
271-
root_maven_modules.append(maven_module)
278+
if maven_module not in all_root_maven_modules:
279+
all_root_maven_modules.append(maven_module)
272280

273281
# Find duplicates across sub-modules that aren't overridden by root
274282
duplicate_submodule_artifacts = _find_duplicate_artifacts_across_submodules(
275283
non_root_artifacts,
276-
root_maven_modules,
284+
all_root_maven_modules,
277285
)
278286

279-
# Filter non-root artifacts that conflict with root artifacts
287+
# Filter non-root artifacts that conflict with root artifacts that have force_version = True
280288
filtered_non_root = []
281289
for artifact in non_root_artifacts:
282290
maven_module = _get_maven_module(artifact)
283-
if not maven_module in root_maven_modules:
291+
292+
# Only exclude if root module has force_version = True for this coordinate
293+
if not maven_module in root_maven_modules_with_force_version:
284294
filtered_non_root.append(artifact)
285295

286296
# Log detailed warning for duplicate sub-module artifacts
@@ -293,7 +303,7 @@ def _deduplicate_artifacts_with_root_priority(name, root_artifacts, non_root_art
293303
warning_parts.append(maven_module)
294304

295305
print("WARNING: The following coordinates from `%s` appear in multiple sub-modules with potentially different versions. " % name +
296-
"Consider adding one of these to your root module to ensure consistent versions:\n\t%s" %
306+
"Consider adding one of these to your root module to ensure consistent versions and setting `force_version = True` on that artifact:\n\t%s" %
297307
"\n\t".join(sorted(warning_parts)))
298308

299309
return root_artifacts + filtered_non_root

tests/custom_maven_install/root_wins_install.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": -1723917414,
3+
"__INPUT_ARTIFACTS_HASH": -935842665,
44
"__RESOLVED_ARTIFACTS_HASH": -1272363274,
55
"artifacts": {
66
"io.netty:netty-buffer": {

0 commit comments

Comments
 (0)