Title: Removing an unused import leaves two blank lines, and only a second run removes one · Issue #37 · openjavaformat/open-java-format · GitHub
Open Graph Title: Removing an unused import leaves two blank lines, and only a second run removes one · Issue #37 · openjavaformat/open-java-format
X Title: Removing an unused import leaves two blank lines, and only a second run removes one · Issue #37 · openjavaformat/open-java-format
Description: What Reported upstream as google/google-java-format#598 (2021) and again as google/google-java-format#1436 (2026-08). It reproduces here on main. With the only import unused: package com.example; import static io.grpc.MethodDescriptor.ge...
Open Graph Description: What Reported upstream as google/google-java-format#598 (2021) and again as google/google-java-format#1436 (2026-08). It reproduces here on main. With the only import unused: package com.example; i...
X Description: What Reported upstream as google/google-java-format#598 (2021) and again as google/google-java-format#1436 (2026-08). It reproduces here on main. With the only import unused: package com.example; i...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/37
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Removing an unused import leaves two blank lines, and only a second run removes one","articleBody":"### What\n\nReported upstream as [google/google-java-format#598](https://github.com/google/google-java-format/issues/598) (2021) and again as [google/google-java-format#1436](https://github.com/google/google-java-format/issues/1436) (2026-08). It reproduces here on `main`. With the only import unused:\n\n``` java\npackage com.example;\n\nimport static io.grpc.MethodDescriptor.generateFullMethodName;\n\n/**\n * Javadoc for class.\n */\npublic class TestBug {\n}\n```\n\n`open-java-format TestBug.java` prints two blank lines after the package line, and a run on that output removes one. So `--replace` writes a file that `--dry-run --set-exit-if-changed` then reports as unformatted, and our GitHub Action fails on a file the formatter has just written.\n\nThe Gradle and Spotless step prints one blank line for the same file, so the two entry points also disagree about it.\n\n### Why it happens\n\nThe command line formats first and fixes imports afterwards (`FormatFileCallable.formatFile`); the library entry point that Gradle uses, `Formatter.formatSourceAndFixImports`, fixes imports first and then formats. `RemoveUnusedImports` deletes an import with its line break but leaves the blank lines around it, so an import that sat between two blank lines leaves both behind. On the library path the formatting that follows collapses them; on the command line nothing runs after it.\n\n### Fix\n\nPort [google/google-java-format#1437](https://github.com/google/google-java-format/pull/1437) by arimu1 (open): `RemoveUnusedImports` merges adjacent deleted imports into one range and, when that range sits between blank lines or at the start of the file, deletes one of the blank lines too. That fixes the command line without changing the order of its steps, which `--lines` and `--offset` depend on.\n\n### Done when\n\n- one command-line run on the file above, and on the one from google/google-java-format#598, gives what a second run gives;\n- `RemoveUnusedImportsTest` covers a deleted import between blank lines, a deleted import at the start of the file, and a partial deletion that keeps its blank line;\n- the commit keeps the upstream author and names the upstream PR.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-09-23T07:15:28Z","url":"https://github.com/openjavaformat/open-java-format/issues/37"}
| 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:4d287476-3787-6607-976b-b20b58f794a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A162:1D8DF0:F2EBC:1451D8:6AFBAEE1 |
| html-safe-nonce | 85eb342786df4a7e8de1c0009952951891cc26f193d28b68a715cfb28dc3c087 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTYyOjFEOERGMDpGMkVCQzoxNDUxRDg6NkFGQkFFRTEiLCJ2aXNpdG9yX2lkIjoiNjUwMDI0MjgwMTY2NDE3NTg0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 89d7b1f65f1030b38f7992eed2a7989e5f338ee1c4e4a8621349ab7c793618bf |
| hovercard-subject-tag | issue:5550086019 |
| 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/37/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5e680a34e19702f50c32d3ca9abd62ba90904d4ee50fb95867a7e2345beebf8/openjavaformat/open-java-format/issues/37 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5e680a34e19702f50c32d3ca9abd62ba90904d4ee50fb95867a7e2345beebf8/openjavaformat/open-java-format/issues/37 |
| og:image:alt | What Reported upstream as google/google-java-format#598 (2021) and again as google/google-java-format#1436 (2026-08). It reproduces here on main. With the only import unused: package com.example; i... |
| 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 | e25fbd830d3e6ad0e8935c105cf4fdc70c593dffcd27bb45b790ae4fece5ea55 |
| 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 | 08c8f17f9d27054a108b8b6254e405de0151f660 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width