Skip to content

Fix ChangeDependency routing Gradle build files to updateJavaSourceSet instead of gradleVisitor#7373

Merged
steve-aom-elliott merged 3 commits intomainfrom
fix/changedependency-gradle-routing
Apr 14, 2026
Merged

Fix ChangeDependency routing Gradle build files to updateJavaSourceSet instead of gradleVisitor#7373
steve-aom-elliott merged 3 commits intomainfrom
fix/changedependency-gradle-routing

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

@steve-aom-elliott steve-aom-elliott commented Apr 14, 2026

Summary

  • Fix a bug where ChangeDependency made no changes to Gradle build files when Java source files were also present in the source set
  • The outer TreeVisitor in getVisitor() routes JavaSourceFile instances to updateJavaSourceSet for marker updates, but G.CompilationUnit (Gradle build scripts) also implements JavaSourceFile, causing build files to be misrouted instead of being handled by gradleVisitor
  • Guard updateJavaSourceSet with a check for the JavaSourceSet marker — build scripts don't carry one, so they naturally fall through to gradleVisitor

Context

This was introduced in #7202 which added JavaSourceSet marker updating to ChangeDependency. The isAcceptable method correctly distinguishes Gradle files from Java files, but the visit method's instanceof JavaSourceFile check catches both.

This breaks ChangeDependency for any Gradle project where the scanner finds the old dependency (making hasModulesWithOldDep true), which is the common case. Downstream, this causes test failures in rewrite-spring for RenameDeprecatedStartersManagedVersionsTest, MigrateToModularStartersTest, and Boot3UpgradeTest.

Approach evolution

  1. Initial fix: Added explicit G.CompilationUnit / K.CompilationUnit type checks before the JavaSourceFile branch
  2. Refined: Switched from type checks to source path extension checks, since K.CompilationUnit is used for both .gradle.kts build scripts and regular .kt source files — the type check would incorrectly skip JavaSourceSet updates for Kotlin source files
  3. Final: Replaced path checks with a JavaSourceSet marker presence check — build scripts don't carry this marker, so they fall through to gradleVisitor. This is both simpler and more semantically correct.

Test plan

  • New test changeDependencyWhenJavaSourcesPresent — fails before fix, passes after
  • All existing ChangeDependency tests pass

…esent

The outer TreeVisitor in getVisitor() checks `tree instanceof
JavaSourceFile` to route Java source files to JavaSourceSet marker
updates. However, G.CompilationUnit (Gradle build scripts) also
implements JavaSourceFile, so when the scanner found the old dependency
(making hasModulesWithOldDep true), Gradle files were routed to
updateJavaSourceSet instead of gradleVisitor — causing no dependency
changes.

Add an explicit check for G.CompilationUnit and K.CompilationUnit
before the JavaSourceFile branch to ensure Gradle/Kotlin build scripts
are always handled by gradleVisitor.
K.CompilationUnit is used for both .gradle.kts build scripts and
regular .kt source files. The previous fix using instanceof would
incorrectly skip JavaSourceSet updates for Kotlin source files.
Check the source path extension instead to precisely distinguish
build scripts from source files.
Instead of checking file extensions to distinguish build scripts from
source files, check whether the tree actually has a JavaSourceSet
marker. Build scripts don't carry one, so they fall through to the
gradleVisitor. This is both simpler and more semantically correct.
@steve-aom-elliott steve-aom-elliott added the bug Something isn't working label Apr 14, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Apr 14, 2026
@steve-aom-elliott steve-aom-elliott marked this pull request as ready for review April 14, 2026 18:18
@Test
void changeDependencyWhenJavaSourcesPresent() {
rewriteRun(
spec -> spec.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null, true)),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you check if LST's have this marker in a real example?
If they do, we should probably mock on in here also.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The JavaParser adds the JavaSourceSet marker during parsing, so the java file is going to get it when it gets parsed as part of java(). The Gradle file is not going to get it.

@Jenson3210
Copy link
Copy Markdown
Contributor

1 question, but looks okay to me

@steve-aom-elliott steve-aom-elliott changed the title Fix ChangeDependency skipping Gradle build files when Java sources present Fix ChangeDependency routing Gradle build files to updateJavaSourceSet instead of gradleVisitor Apr 14, 2026
@steve-aom-elliott steve-aom-elliott merged commit a8ac3d3 into main Apr 14, 2026
1 check passed
@steve-aom-elliott steve-aom-elliott deleted the fix/changedependency-gradle-routing branch April 14, 2026 18:56
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants