Skip to content

Fix Gradle AddDependency for matches only in test sources#6637

Merged
jkschneider merged 1 commit intomainfrom
gradle-add-test-dependency
Jan 29, 2026
Merged

Fix Gradle AddDependency for matches only in test sources#6637
jkschneider merged 1 commit intomainfrom
gradle-add-test-dependency

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

  • Fix AddDependency scanner to only record configurations for source sets where onlyIfUsing actually matches, rather than all source sets in the project
  • Previously, when a project had both main and test sources but the type was only used in test, the dependency was incorrectly added as implementation instead of testImplementation
  • Root cause: configurationsByProject collected all source sets unconditionally, then removeTransitiveConfigurations() dropped testImplementation in favor of implementation
  • Aligns Gradle behavior with the Maven AddDependency which already correctly tracks scope per source set via scopeByProject
  • Add onlyIfUsingTestScopeWithMainSource test covering the previously broken scenario

- Fix `AddDependency` scanner to only record configurations for source sets where `onlyIfUsing` actually matches, rather than all source sets in the project
- Previously, when a project had both main and test sources but the type was only used in test, the dependency was incorrectly added as `implementation` instead of `testImplementation`
- Root cause: `configurationsByProject` collected all source sets unconditionally, then `removeTransitiveConfigurations()` dropped `testImplementation` in favor of `implementation`
- Aligns Gradle behavior with the Maven `AddDependency` which already correctly tracks scope per source set via `scopeByProject`
- Add `onlyIfUsingTestScopeWithMainSource` test covering the previously broken scenario
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jan 29, 2026
@knutwannheden knutwannheden added recipe Requested Recipe gradle labels Jan 29, 2026
@jkschneider jkschneider merged commit bbad994 into main Jan 29, 2026
2 checks passed
@jkschneider jkschneider deleted the gradle-add-test-dependency branch January 29, 2026 13:38
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gradle recipe Requested Recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants