Skip to content

Autodetect: fix three bugs in building an import layout - #10231

Merged
timtebeek merged 2 commits into
mainfrom
tim/autodetect-layout-builder-fixes
Dec 1, 2026
Merged

timtebeek merged 2 commits into
mainfrom
tim/autodetect-layout-builder-fixes

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Three bugs in how Autodetect builds an import layout from the chosen block sequence. Each shows up with today's longest-sequence choice, and each gets its own test that fails without its fix.

  • A javax.* block followed by a final java.* block listed java.* twice. The check for a java.* neighbour after javax.* stopped one block short.
  • When no sampled file had imports, the layout listed every block twice. The empty-sequence branch appended a full layout after the code above it had already built one.
  • The last import block of a file overwrote where java.* or javax.* first appeared, while every earlier block kept the first position. That could count a file that puts java.* first as putting javax.* first.

Part of splitting #10217 into smaller PRs, after #10230.

Measured with FindAutodetectedStyleVotes (branch tim/autodetect-style-votes) on dev.moderne.io over the Spring, Maven, MyBatis, Micronaut and Dropwizard organizations (341 repositories), comparing this change with #10230 alone. The layout changes in 11 repositories, 10 in Micronaut plus dropwizard/dropwizard. In each, the old layout listed java.* twice (import javax.* | import java.* | <blank line> | import java.*), and the new one is IntelliJ's. Six more files follow the detected layout, and no repository loses any.

Verified locally: the new tests javaxThenJavaAsLastBlocks, filesWithoutImports and javaBeforeJavaxCountsWhereJavaFirstAppears fail without the fixes and pass with them. AutodetectTest, the Java style, format, OrderImports, AddImport and RemoveImport tests, and the Kotlin and Groovy AutodetectTest pass.

- A javax.* block followed by a final java.* block listed java.* twice.
- When no sampled file had imports, the layout listed every block twice.
- The last import block of a file overwrote where java.* or javax.* first
  appeared, which could flip the detected java/javax order.
…-builder-fixes

# Conflicts:
#	rewrite-java-test/src/test/java/org/openrewrite/java/style/AutodetectTest.java
@timtebeek
timtebeek enabled auto-merge (squash) December 1, 2026 20:33
@timtebeek
timtebeek merged commit 690626b into main Dec 1, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/autodetect-layout-builder-fixes branch December 1, 2026 20:37
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 1, 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.

1 participant