Title: A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format · GitHub
Open Graph Title: A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format
X Title: A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format
Description: A modern, lambda-friendly, 120 character Java formatter. - A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format
Open Graph Description: A modern, lambda-friendly, 120 character Java formatter. - A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format
X Description: A modern, lambda-friendly, 120 character Java formatter. - A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format
Opengraph URL: https://github.com/openjavaformat/open-java-format
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A comment between imports fails the whole file: \"Imports not contiguous\"","articleBody":"### What\n\nReported upstream as [google/google-java-format#424](https://github.com/google/google-java-format/issues/424) (a javadoc between two imports, 2019) and [google/google-java-format#546](https://github.com/google/google-java-format/issues/546) (the same message through a Maven plugin, 2020). It is also one of the samples in [palantir/palantir-java-format#789](https://github.com/palantir/palantir-java-format/issues/789). It reproduces here on `main`:\n\n``` java\nimport b.B;\n\n// why we need A\nimport a.A;\n\nclass T {\n A a;\n B b;\n}\n```\n\n`open-java-format T.java` fails with `T.java:error: Imports not contiguous (perhaps a comment separates them?)` and exit code 2, so nothing in the file gets formatted. A `/* */` or javadoc comment between imports fails the same way. A `//` comment on the line right under an import is accepted: it is kept as that import's trailing comment and moves with it.\n\nThe other sample in palantir#789, a lone `;` between two imports, is not ours to handle: javac 21 rejects it with \"extraneous semicolon\".\n\n### Why it happens\n\n`ImportOrderer.scanImports` reads imports while the next token is `import`. After each one it takes the rest of the line and the `//` comments directly under it. A blank line followed by a comment, or a block comment, ends the scan; `reorderImports` then finds another `import` further down and throws.\n\n### Fix\n\nWhen comments between two imports are followed by another `import`, keep them as the leading text of that import, so they move with it when the imports are sorted. Comments after the last import still belong to what follows, and trailing `//` comments stay as they are.\n\nOnly files that fail today change.\n\n### Done when\n\n- the file above formats, `// why we need A` stays directly above `import a.A;`, and a second run changes nothing;\n- `ImportOrdererTest` covers a line comment, a block comment and a javadoc between imports, and a comment after the last import;\n- a corpus run shows no change for files that format today.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-09-23T07:15:31Z","url":"https://github.com/openjavaformat/open-java-format/issues/39"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:4981102a-64fc-0d63-ef2b-76d9d7ad4a47 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E378:1D8DF0:2C1CE6:3AA368:6AFBB791 |
| html-safe-nonce | 940f34a6403ccea05e5807ceb4de89c2641b3ce66fd4574b142efc1375ba07a3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzc4OjFEOERGMDoyQzFDRTY6M0FBMzY4OjZBRkJCNzkxIiwidmlzaXRvcl9pZCI6IjMzNjQ4MTYzNjY2MjI5Nzk5ODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 56e1c7161df7517ed626b0c5530e81cba46da76776bfb5a3f85eb672a61642b4 |
| hovercard-subject-tag | repository:1288055459 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/openjavaformat/open-java-format/issues/39 |
| twitter:image | https://opengraph.githubassets.com/d68ef11eccab927066826db7c01d5e5330c9474f93b01c7830ce5a188e5db2ee/openjavaformat/open-java-format |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d68ef11eccab927066826db7c01d5e5330c9474f93b01c7830ce5a188e5db2ee/openjavaformat/open-java-format |
| og:image:alt | A modern, lambda-friendly, 120 character Java formatter. - A comment between imports fails the whole file: "Imports not contiguous" · Issue #39 · openjavaformat/open-java-format |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2b2ef9c1578c18d8ed27027940544c958743213cf4461659f14d3ccdf51e162c |
| turbo-cache-control | no-cache |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e4cce0e99cf61481a318ff12c2e8580153a6c7c6 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width