Title: Fix the imports before the layout or after it? · Issue #224 · openjavaformat/open-java-format · GitHub
Open Graph Title: Fix the imports before the layout or after it? · Issue #224 · openjavaformat/open-java-format
X Title: Fix the imports before the layout or after it? · Issue #224 · openjavaformat/open-java-format
Description: The question Should the imports be fixed before the code is laid out, or after it? Every route has to do it in the same order, or the same file comes out differently (#219). #220 makes them all fix the imports after formatting, as the co...
Open Graph Description: The question Should the imports be fixed before the code is laid out, or after it? Every route has to do it in the same order, or the same file comes out differently (#219). #220 makes them all fix...
X Description: The question Should the imports be fixed before the code is laid out, or after it? Every route has to do it in the same order, or the same file comes out differently (#219). #220 makes them all fix...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/224
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fix the imports before the layout or after it?","articleBody":"### The question\n\nShould the imports be fixed before the code is laid out, or after it? Every route has to do it in the same order, or the same file comes out differently (#219). #220 makes them all fix the imports after formatting, as the command line, the native binary and the GitHub Action already did. google-java-format has just gone the other way: google/google-java-format@633ce6be (2026-10-20) runs unused-import removal, modifier reordering and import sorting as one pass before the layout, on the command line too, and remaps the requested character ranges through each of its edits.\n\n### What depends on the order\n\n`ImportOrderer` moves a comment along with its import, and `RemoveUnusedImports` leaves the comment of an import it removes behind, so the order decides where that comment ends up. It also decides whether an extraneous semicolon between imports is dropped or fails the file. Over 3,865 real files, as they are and with an unused import added to each, the two orders differ in one, `CharsetEncoder` of JDK 21; each order passes its check on what the other wrote. Four cases, ready for `test-cases/imports` in #225, pin the differences: `commentOfARemovedImportGoesWithTheImportAboveIt`, `commentOfARemovedOwnPackageImportGoesWithTheImportAboveIt`, `commentUnderARemovedImportStaysOnTheClass` and `semicolonAfterTheLastImportStaysOnALineOfItsOwn`.\n\n### For and against\n\n| | **Before the layout** (google-java-format now, `formatSourceAndFixImports` before #220) | **After the layout** (#220, the command line, the native binary, the GitHub Action) |\n|---|---|---|\n| Output of the command line and the GitHub Action | changes in the rare cases above | stays as it is |\n| Porting from google-java-format | follows its `Cleanups` | conflicts with it |\n| `import a.B;;` followed by another import | formatted: `ImportOrderer` drops the semicolon | fails with `extraneous semicolon`, as javac 21 does with `--release 21` |\n| A comment under a removed import | a blank line stays between it and the class | stays on the class |\n| Reformat Code with \"Optimize imports\" in IntelliJ IDEA (#222) | the same order: the IDE runs the optimizer first | differs, unless a whole-file Reformat runs in one pass |\n| Ranges (`--lines`, `--output-replacements`, IntelliJ IDEA, `formatDiff`) | the ranges have to be remapped through the import edits, as google-java-format does | the ranges are offsets into the input and work as they are |\n| Work | `ImportOrderer` and `RemoveUnusedImports` return replacements instead of text; the import commit of #220 is redone | done in #220 |\n\n### Proposal\n\nKeep \"after\" for now: the command line is what every check compares with, and it does not change. Revisit when porting google-java-format's `Cleanups`, which would bring \"before\" together with the range remapping it needs.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-11-13T20:36:08Z","url":"https://github.com/openjavaformat/open-java-format/issues/224"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e5363dbd-e1ea-9e9a-ff9d-14a3aa347e01 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF52:12B577:22875:2E21E:6AFBD808 |
| html-safe-nonce | 983940bc284344e0b71c75f26e8c6d59934c0805ca5091acc063ada73375c9ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjUyOjEyQjU3NzoyMjg3NToyRTIxRTo2QUZCRDgwOCIsInZpc2l0b3JfaWQiOiI1NTM5NTYyMjEyMTYwMTY1ODk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2a5b7253e2897cbe9d22fd330b04fa3a7878b4936f73bbc00abbfa48c70de625 |
| hovercard-subject-tag | issue:6379511346 |
| 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/_view_fragments/issues/show/openjavaformat/open-java-format/224/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e8ffc6aee54be65d2f933c238ef0696c64e5778a1f777918877f3a8fb407dd04/openjavaformat/open-java-format/issues/224 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e8ffc6aee54be65d2f933c238ef0696c64e5778a1f777918877f3a8fb407dd04/openjavaformat/open-java-format/issues/224 |
| og:image:alt | The question Should the imports be fixed before the code is laid out, or after it? Every route has to do it in the same order, or the same file comes out differently (#219). #220 makes them all fix... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 18742cb872ec73544356e60359266b67aafece70c4ce008042e06227924cdd07 |
| turbo-cache-control | no-preview |
| 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 | 820be804f18e1ef3940bd74a00d41d73d02e2b91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width