Title: The routes format the same file differently: formatDiff on the JVM, ranges, and the order of fixing imports · Issue #219 · openjavaformat/open-java-format · GitHub
Open Graph Title: The routes format the same file differently: formatDiff on the JVM, ranges, and the order of fixing imports · Issue #219 · openjavaformat/open-java-format
X Title: The routes format the same file differently: formatDiff on the JVM, ranges, and the order of fixing imports · Issue #219 · openjavaformat/open-java-format
Description: What happens The same file comes out differently depending on the route that formats it. Found by running every route on the same input with 21.0.2 on JDK 21, the native binary for macOS aarch64 included, over 3,865 real files: 3,000 fro...
Open Graph Description: What happens The same file comes out differently depending on the route that formats it. Found by running every route on the same input with 21.0.2 on JDK 21, the native binary for macOS aarch64 in...
X Description: What happens The same file comes out differently depending on the route that formats it. Found by running every route on the same input with 21.0.2 on JDK 21, the native binary for macOS aarch64 in...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/219
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"The routes format the same file differently: formatDiff on the JVM, ranges, and the order of fixing imports","articleBody":"### What happens\n\nThe same file comes out differently depending on the route that formats it. Found by running every route on the same input with 21.0.2 on JDK 21, the native binary for macOS aarch64 included, over 3,865 real files: 3,000 from a set of local repositories and 865 from `java.base` of JDK 21.\n\n**1. `formatDiff` on the JVM does not reflow long strings; with the native binary it does.** A changed line\n\n```java\n String s = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\";\n```\n\nbecomes, without `openjavaformat.native.formatter`,\n\n```java\n String s =\n \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\";\n```\n\nand with it\n\n```java\n String s = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et\"\n + \" dolore magna aliqua.\";\n```\n\nThe first fails the check of the GitHub Action and the pre-commit hook (`--dry-run --set-exit-if-changed` with the native binary); the second passes.\n\n**2. Formatting a range reflows long strings outside it.** IntelliJ IDEA, with the Java-based formatter and the native one alike, and `formatDiff` with the native binary: once a line in the ranges needs wrapping, every long string in the file is reflowed, on lines nobody touched. Only `changed` is on a changed line here:\n\n```diff\n class S {\n- String outside = \"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat sed.\";\n+ String outside = \"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\"\n+ + \" consequat sed.\";\n\n- String changed = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.\";\n+ String changed =\n+ \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et\"\n+ + \" dolore magna.\";\n }\n```\n\nOver every line with a string literal and more than 90 characters in 600 of the files, formatted one line at a time, the reflow changed 11 results, and in 2 of them it changed strings outside the code that formatting the line rewrote.\n\n**3. The Maven plugin and the Spotless step fix the imports before formatting; the command line, the native binary, the GitHub Action, the pre-commit hook and the playground fix them after it.** The result differs when an import that is removed has a comment. From `java/nio/charset/CharsetEncoder.java` in JDK 21, where `import java.nio.charset.CoderMalfunctionError; // javadoc` is removed:\n\n```\nbefore (Maven plugin, Spotless step) after (command line, native binary)\nimport java.lang.ref.WeakReference; import java.lang.ref.WeakReference;\nimport java.nio.Buffer; // javadoc\nimport java.nio.ByteBuffer; import java.nio.Buffer;\nimport java.nio.CharBuffer; import java.nio.ByteBuffer;\n// javadoc import java.nio.CharBuffer;\n```\n\nA comment right under a removed import gets a blank line between it and the class before, and stays on the class after. And `import java.util.List;;` followed by another import, which javac 21 rejects with `--release 21` and accepts with a warning below it: the Maven plugin and the Spotless step drop the semicolon and format the file, the command line fails with `extraneous semicolon`.\n\nThis is rare: 1 of the 3,865 files, as they are and with an unused import added to each. Each order passes its check on what the other wrote, so no build fails on it; it decides what a file looks like after it is first formatted.\n\n### Why\n\n- **1.** The Gradle plugin loads `FormatterServiceImpl` into its own JVM, and its `getFormatReplacements` calls `Formatter.getFormatReplacements`, which never reflows strings. The bootstrapping and native services run `Main --output-replacements`, where `FormatFileCallable` reflows them after the replacements.\n- **2.** That reflow gets the whole file, and its result goes back as one replacement of the whole file. The ranges only decide whether to reflow at all, through `StringWrapper.linesNeedWrapping`, which compares the ranges of the input with the lines of the formatted text.\n- **3.** `Formatter.formatSourceAndFixImports`, behind `FormatterService.formatSourceReflowStringsAndFixImports`, runs `ImportOrderer`, `RemoveUnusedImports`, `formatSource`, `StringWrapper`; `FormatFileCallable.formatFile` runs `formatSource`, `RemoveUnusedImports`, `ImportOrderer`, `StringWrapper`. `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. The command line has to format first: the ranges of `--lines` and `--offset` are offsets into the input.\n\nNo test runs the in-process service against the others: `FormatterServicesTest` and `FormatDiffTest` cover the bootstrapping and native services only.\n\n### Proposal\n\n- One method formats the ranges and reflows the strings, for `--output-replacements` and for `FormatterServiceImpl.getFormatReplacements`.\n- With ranges, `StringWrapper` reflows only the literals in the code that formatting the ranges rewrites, moving the ranges along with each of its passes. Without ranges it stays as it is, so whole-file formatting does not change.\n- `formatSourceAndFixImports` fixes the imports after formatting, as the command line does.\n- Tests hold the in-process service to what the command line prints, and `FormatterServicesTest` and `FormatDiffTest` run the in-process formatter too.\n\n### Not here\n\n- Spotless' own `palantirJavaFormat()` step runs the formatter through Spotless' code, which never reflows strings. That is Spotless' to change; the docs say so.\n- The Eclipse plugin formats with `Formatter.getFormatReplacements` and does not reflow strings either.\n- `open-java-format --lines` without `--output-replacements` still reflows the whole file.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-11-12T20:47:13Z","url":"https://github.com/openjavaformat/open-java-format/issues/219"}
| 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:6d5846fa-d87c-c3f9-ded5-fb53dd3637df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FB8:6188:38B56C:4B234C:6AFBBB5C |
| html-safe-nonce | c675c2626b81e09c8f280b0ba4ad8a54e1299b4ea1804f71e305c9267848f5b2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkI4OjYxODg6MzhCNTZDOjRCMjM0Qzo2QUZCQkI1QyIsInZpc2l0b3JfaWQiOiI0ODg0NzA2MzAyMjcxMTQyNzQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 90574cb02af0be1da1a4ebe9f126d85e6d134e2075565857f69aa4d3191e69e9 |
| hovercard-subject-tag | issue:6358710408 |
| 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/219/issue_layout |
| twitter:image | https://opengraph.githubassets.com/472c528f34980bb701f668d202e694a8d8a6ca40cdc3c68dee2c94c7c95a85b7/openjavaformat/open-java-format/issues/219 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/472c528f34980bb701f668d202e694a8d8a6ca40cdc3c68dee2c94c7c95a85b7/openjavaformat/open-java-format/issues/219 |
| og:image:alt | What happens The same file comes out differently depending on the route that formats it. Found by running every route on the same input with 21.0.2 on JDK 21, the native binary for macOS aarch64 in... |
| 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 | 2b2ef9c1578c18d8ed27027940544c958743213cf4461659f14d3ccdf51e162c |
| 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 | e4cce0e99cf61481a318ff12c2e8580153a6c7c6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width