Skip to content

Fix AddSettingsPluginRepository duplicate pluginManagement and repository handling#6959

Merged
timtebeek merged 3 commits intomainfrom
fix-duplicate-plugin-repo
Mar 12, 2026
Merged

Fix AddSettingsPluginRepository duplicate pluginManagement and repository handling#6959
timtebeek merged 3 commits intomainfrom
fix-duplicate-plugin-repo

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Mar 12, 2026

Summary

  • Fix AddSettingsPluginRepository to correctly detect existing pluginManagement blocks wrapped in J.Return (Kotlin settings files)
  • Add comprehensive test coverage for URL-less repository types (gradlePluginPortal, mavenLocal)

Problem

The AddSettingsPluginRepository recipe has two bugs:

  1. It may add a repository even if it already exists (e.g. gradlePluginPortal() when already present)
  2. It may add an entire duplicate pluginManagement block

The root cause is that addPluginManagementRepos() only checked for J.MethodInvocation as the first statement, but in Kotlin settings files the parser wraps the last expression in J.Return, so the existing pluginManagement block was not recognized and a duplicate was inserted.

Solution

  • Added J.Return unwrapping when detecting existing pluginManagement blocks in addPluginManagementRepos()
  • When writing back modified pluginManagement, re-wraps in J.Return if the original was wrapped
  • Added 20 new test cases covering: skip-when-exists for gradlePluginPortal/mavenLocal (Groovy+KTS), no-duplicate-pluginManagement-block scenarios, idempotency (3 cycles), complex settings files, and adding repos to existing blocks

Test plan

  • All 20 new tests pass

  • All existing tests pass

  • Idempotency verified with 3-cycle tests

  • Fixes moderneinc/customer-requests#1986

Extract `repositoryExists` method for clarity. Handle `J.Return`-wrapped
`pluginManagement` statements in `addPluginManagementRepos`, which can
occur in Kotlin settings files where the last expression is wrapped in
a return node.

Add comprehensive test coverage for `gradlePluginPortal` and `mavenLocal`
repository types with skip-when-exists, idempotency, and complex settings
file scenarios for both Groovy and Kotlin DSL.

Fixes moderneinc/customer-requests#1986
@timtebeek timtebeek force-pushed the fix-duplicate-plugin-repo branch from a5bd6c6 to 3cdb827 Compare March 12, 2026 13:01
@timtebeek timtebeek changed the title Fix AddSettingsPluginRepository duplicate pluginManagement and repository handling Fix AddSettingsPluginRepository duplicate pluginManagement and repository handling Mar 12, 2026
Copy link
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I retuced the number of tests as there where a lot of duplicates.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Mar 12, 2026
@timtebeek timtebeek merged commit 8c09a01 into main Mar 12, 2026
1 check passed
@timtebeek timtebeek deleted the fix-duplicate-plugin-repo branch March 12, 2026 17:03
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants