Fix duplicate gradlePluginPortal() in settings.gradle.kts#7230
Merged
Conversation
…g type attribution When method types have an incorrect declaring type (as happens on the platform with KTS-parsed settings.gradle.kts), FindRepository's MethodMatcher fails to detect existing repositories, causing AddSettingsPluginRepository to add duplicates. Reproduces moderneinc/customer-requests#2120
…ect type attribution On the Moderne platform, KTS-parsed settings.gradle.kts files may have incorrect type attribution on method invocations (e.g. wrong declaring type instead of RepositoryHandler). This causes FindRepository's MethodMatcher to fail the strict matching path, missing existing repository entries and leading AddSettingsPluginRepository to add duplicates. Added two defensive checks: 1. In addRepoToRepositoriesBlock: name-based check if the repository already exists in the repositories block before adding 2. In addPluginManagementRepos: detect when pluginManagement exists but no change was needed, to avoid inserting a duplicate block Fixes moderneinc/customer-requests#2120
Jenson3210
approved these changes
Apr 1, 2026
MBoegers
approved these changes
Apr 1, 2026
shanman190
reviewed
Apr 2, 2026
| } | ||
|
|
||
| // Name-based fallback for when MethodMatcher fails due to incorrect type attribution (e.g. rewrite-kotlin) | ||
| private boolean repoAlreadyExists(J.MethodInvocation repos, String repoName) { |
Contributor
There was a problem hiding this comment.
I believe that this fails for any non first party named repository. As an example, multiple maven {} are allowed and this would improperly match on those only allowing for there to be one.
I think the root cause issue here is improper matching in FindRepository which allows us to get this far. If we need to we do have the repositories available as elements in the GradleProject marker that we could utilize as well for match selection much like we do for FindGradleProject.
pstreef
added a commit
that referenced
this pull request
Apr 2, 2026
The name-based fallback added in #7230 incorrectly blocks adding a second `maven {}` or `ivy {}` repository with a different URL, since it only checks the method name. Gate the check on `url == null` so it only applies to uniquely-named repositories like `gradlePluginPortal`, `mavenCentral`, `mavenLocal`, and `google`.
pstreef
added a commit
that referenced
this pull request
Apr 2, 2026
The name-based fallback added in #7230 incorrectly blocks adding a second `maven {}` or `ivy {}` repository with a different URL, since it only checks the method name. Gate the check on `url == null` so it only applies to uniquely-named repositories like `gradlePluginPortal`, `mavenCentral`, `mavenLocal`, and `google`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AddSettingsPluginRepositoryadds a duplicategradlePluginPortal()entry insettings.gradle.ktswhen the existing method invocations have incorrect type attribution (e.g. declaring type resolved tokotlin.Unitinstead ofRepositoryHandler). TheMethodMatchertakes the strict path with a non-null but wrong type and fails to match, soFindRepositorymisses the existing entry.Note: This is a defensive workaround. The root cause is that
GradleParserdrops default Gradle API stubs when an explicit classpath is provided — see #7232 for the fix. This workaround is safe to keep even after the parser is fixed, as the name-based check is redundant (not conflicting) when types are correct.Solution
Two changes:
Name-based fallback in
addRepoToRepositoriesBlock— before inserting a new repository, check if one with the same method name already exists in therepositories { }block usingunwrapMethodCall. This catches duplicates regardless of type attribution quality.Guard in
addPluginManagementRepos— when the name-based check prevents modification insiderepositories { }, the returned list has referential equality with the input, which the existing code interprets as "nopluginManagementfound" and inserts a whole new block. Added a scan for existingpluginManagementcalls before the insert-new-block fallback.