Skip to content

bzlmod: enforce root artifact precedence when version_conflict_policy… - #1556

Closed
davido wants to merge 2 commits into
bazel-contrib:masterfrom
davido:issue_1549
Closed

davido wants to merge 2 commits into
bazel-contrib:masterfrom
davido:issue_1549

Conversation

@davido

@davido davido commented Mar 4, 2026

Copy link
Copy Markdown

…="pinned"

Addresses #1549.

When using rules_jvm_external with bzlmod module layering, multiple modules can contribute artifacts into the same maven.install repository via known_contributing_modules.

If both the root module and a contributing module declare the same group:artifact with different versions, the current merge logic may retain both versions unless the root explicitly sets force_version = True for each artifact (for example using maven.amend_artifact). This results in duplicate artifact entries in the merged artifact list and causes resolution to fail early with "Found duplicate artifact versions", even when the root module sets version_conflict_policy = "pinned".

However, the documented semantics of "pinned" are that user-specified versions should be chosen unconditionally over transitive ones.

This change aligns layered resolution with that expectation by treating all artifacts declared by the root module as if force_version = True when version_conflict_policy = "pinned" is used. This ensures that root-declared versions take precedence during layered dependency merging and prevents duplicate coordinates from reaching the resolver.

As a result, root modules can enforce their dependency versions across layered module contributions without needing to enumerate each artifact with maven.amend_artifact(force_version = ...).

This is particularly useful for applications acting as the root module that depend on libraries contributing their own Maven dependencies via bzlmod layering, and that require deterministic control over dependency versions across the combined dependency graph.

davido added 2 commits July 25, 2026 09:11
Addresses bazel-contrib#1549.

deduplicate_artifacts_with_root_priority() resolves a duplicate
group:artifact between the root module and a contributing module by
popping the non-root artifact and, when the root does not force its
version, selecting the highest version. The selection was one-sided:
when the non-root version won, it was appended to the result, but the
losing root artifact was never removed, so both versions survived
into the merged artifact list. Installs with
duplicate_version_warning = "error" then fail early in
_check_artifacts_are_unique(), before any conflict policy can act:

    Found duplicate artifact versions
        args4j:args4j has multiple versions 2.33, 2.37

The other two branches (root forces, or the root version is highest)
already drop the popped non-root artifact, so only this branch could
ship duplicates.

Rebuild the returned list without root artifacts that lost the
version comparison, so exactly one version per coordinate reaches
the resolver regardless of which side won. The function loses its
leading underscore so the new regression tests can load it.
Addresses bazel-contrib#1549.

apply_root_version_conflict_policy() implements the documented
semantics of version_conflict_policy = "pinned" -- user-specified
versions are chosen unconditionally over transitive ones -- by
treating root-declared artifacts as force_version = True during
layered dependency merging. It was gated to the gradle and maven
resolvers, leaving coursier, the default resolver, on the old
behaviour: a contributing module's higher version outranks (or,
with duplicate_version_warning = "error", collides with) the root
module's deliberately pinned version.

The gate is also internally inconsistent: under "pinned" the
coursier code path already passes --force-version for every declared
artifact at resolution time. Only the bzlmod merge layer ignored the
policy, so the root-vs-contributor conflict was decided (or failed)
before coursier's own pinned handling could run.

Lift the gate: pinned now forces root-declared versions for every
resolver. A root module application depending on libraries that
contribute their own Maven dependencies via bzlmod layering (for
example Gerrit with JGit contributing args4j) no longer needs to
enumerate every overlapping coordinate with
maven.amend_artifact(force_version = ...).

Pinned coursier installs pick up force_version on their root
artifacts, which changes lockfile input hashes; a one-time REPIN
refreshes them. The two affected in-repo fixtures
(policy_pinned_testing, coursier_resolved_with_boms) are repinned
here, migrating them to the current lock format on the way; their
resolved dependency versions are unchanged.
@davido

davido commented Oct 3, 2026

Copy link
Copy Markdown
Author

Superseded by #1631.

@davido davido closed this Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant