From 2d654621793ef4117dc167631865b0e83e65219a Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Wed, 11 Dec 2024 10:56:08 +0000 Subject: [PATCH 1/2] Make multiple modules contributing to the same maven repo namespace warning less verbose and duplicated --- private/extensions/maven.bzl | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/private/extensions/maven.bzl b/private/extensions/maven.bzl index cbe406479..d033932ba 100644 --- a/private/extensions/maven.bzl +++ b/private/extensions/maven.bzl @@ -145,18 +145,18 @@ def _add_exclusions(exclusions): to_return.append(exclusion) return to_return +# Each bzlmod module may contribute jars to different rules_jvm_external maven repo namespaces. +# We record this mapping of repo_name to the list of modules that contributed to it, and emit a warning +# to the user if there are more than one module that contributed to the same repo name. +# +# This can be typical for the default @maven namespace, if a bzlmod dependency +# wishes to contribute to the users' jars. def _check_repo_name(repo_name_2_module_name, repo_name, module_name): - known_name = repo_name_2_module_name.get(repo_name) - if known_name == None: - repo_name_2_module_name[repo_name] = module_name + known_names = repo_name_2_module_name.get(repo_name, []) + if module_name in known_names: return - - if module_name != known_name: - print("The maven repository '%s' is used in two different bazel modules, originally in '%s' and now in '%s'" % ( - repo_name, - known_name, - module_name, - )) + known_names.append(module_name) + repo_name_2_module_name[repo_name] = known_names def _to_maven_coords(artifact): coords = "%s:%s" % (artifact.get("group"), artifact.get("artifact")) @@ -223,8 +223,10 @@ def maven_impl(mctx): # - ignore_empty_files: Treat jars that are empty as if they were not found. # - additional_coursier_options: Additional options that will be passed to coursier. - # Mapping of `name`s to `bazel_module.name` This will allow us to warn users when more than - # module attempts to update a maven repo (which is normally undesired behaviour) + # Mapping of `name`s to a list of `bazel_module.name`. This will allow us to + # warn users when more than one module attempts to update a maven repo + # (which is normally undesired behaviour, but supported as multiple modules + # can intentionally contribute to the default `maven` repo namespace.) repo_name_2_module_name = {} for mod in mctx.modules: @@ -344,6 +346,13 @@ def maven_impl(mctx): repos[install.name] = repo + for (repo_name, known_names) in repo_name_2_module_name.items(): + if len(known_names) > 1: + print("The maven repository '%s' is used in multiple bzlmod modules: %s" % ( + repo_name, # e.g. "maven" + str(known_names), # e.g. bzl_module_foo, bzl_module_bar + )) + # Breaking out the logic for picking lock files, because it's not terribly simple repo_to_lock_file = {} for mod in mctx.modules: From 9abe79f1a887b36fdd4c9a98dfef2fa02425101f Mon Sep 17 00:00:00 2001 From: Jin Date: Wed, 11 Dec 2024 22:23:43 +0800 Subject: [PATCH 2/2] Update maven.bzl --- private/extensions/maven.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/private/extensions/maven.bzl b/private/extensions/maven.bzl index d033932ba..929b6e89d 100644 --- a/private/extensions/maven.bzl +++ b/private/extensions/maven.bzl @@ -348,9 +348,9 @@ def maven_impl(mctx): for (repo_name, known_names) in repo_name_2_module_name.items(): if len(known_names) > 1: - print("The maven repository '%s' is used in multiple bzlmod modules: %s" % ( + print("The maven repository '%s' has contributions from multiple bzlmod modules, and will be resolved together: %s" % ( repo_name, # e.g. "maven" - str(known_names), # e.g. bzl_module_foo, bzl_module_bar + sorted(known_names), # e.g. bzl_module_bar, bzl_module_bar )) # Breaking out the logic for picking lock files, because it's not terribly simple