Repository navigation
Format inside the IDE's JVM in IntelliJ IDEA, with a separate JVM or the native binary to choose instead - #259
Merged
abashev merged 7 commits intoNov 23, 2026
Conversation
The IntelliJ plugin starts the formatter's JVM on the IDE's own runtime and nothing else. FormatterJdk is a JDK that a project can name for it instead: the IDE's runtime, whichever SDK the project has, or a JDK registered in the IDE, by name. FormatterJdks finds the java executable and the Java version each stands for, and says why one cannot run the formatter: it is older than Java 21, which the formatter is compiled for, the IDE has no JDK of that name, the project has no SDK, or there is no java to start in it, as for a JDK in WSL. Nothing asks for them yet. The settings page lists them and FormatterSelection resolves the chosen one once the formatter can run in more ways than a JVM on the IDE's runtime (#223).
The IntelliJ plugin starts a process for every Reformat and every Optimize imports: 0.3 s to 1 s for a JVM, where the formatter, once loaded and warm, takes 124 ms on a file of 4,026 lines and 9 ms to fix its imports (#223). This is what loads it into the IDE's JVM. Nothing calls it yet. The formatter parses with javac's own classes, in packages that jdk.compiler exports to nobody, and the IDE's JVM is already running. IntelliJ IDEA starts it with --add-opens=java.base/java.lang=ALL-UNNAMED, in 2024.2.6, the oldest IDE the plugin supports, and in 2026.3. With java.lang open JavacAccess can call Module.implAddExports, the method --add-exports ends in: for the formatter's class loader alone, so that nothing else in the IDE reaches javac through it, and nobody edits VM options. The method is not public API, so JavacAccess first says whether it is there to call, and why not if it is not. FormatterClassLoader keeps the formatter apart from the IDE. Its parent is the JDK's platform class loader, which finds javac and nothing of the IDE: the formatter's jars are everything it runs with in a JVM of its own, and the IDE has other versions of the same libraries. Only the types the plugin and the formatter exchange come from the plugin, the classes of open-java-format-spi and Guava, which their signatures name. InProcessFormatters keeps a formatter loaded for as long as its jars stay the same, and is asked for every file, so that a jar rebuilt in place formats the next one, as in a JVM started for it. InProcessFormatterService leaves a file with a syntax error alone, as the formatters that run the command line do: the formatter itself throws, and "Optimize imports on the fly" would show that on every highlighting pass. What it cannot format in this JVM it hands to the service it is given for that: a file the formatter fails on with an Error, a stack overflow for one, and every file once the formatter turns out not to load or link here. The IDE process is the faster way, never the only one. The JVM of the tests has javac exported to everything, as the build compiles the formatter, so JavacAccessTest starts JVMs of its own: one with the IDE's flag alone, where the formatter gives the command line's file and javac stays unexported to the plugin; one with the exports a user of google-java-format adds by hand; and one with neither, which says why it cannot format.
The settings page of the IntelliJ plugin had one box, Use native binary, and without it the plugin started a JVM on the IDE's runtime for every file. It now has three ways to run the formatter, one of them chosen for a project: - In the IDE process: the formatter loaded into the IDE's own JVM, with nothing to start for a file. It is greyed out, with the reason under it, where the formatter cannot reach javac there. - In a separate JVM, as before, on a JDK from a list: the IDE's runtime, which is the default and what the plugin ran on, the project SDK, or a JDK registered in the IDE. One that cannot run the formatter, older than Java 21 for one, is listed in grey and cannot be chosen. - Native binary, as before. The options of the ways not chosen stay on the page, greyed: hidden, they would move the page about with every choice. A project whose settings switch the native binary on keeps it. Every other project moves into the IDE process: a file written before there were modes says only useNativeImage, and formatterMode is new beside it. useNativeImage goes on being written with the mode, for a plugin from before the modes that reads the same .idea/open-java-format.xml. What cannot run falls back, with a warning in idea.log and a line under Active formatter, and no balloon: the IDE process to a separate JVM, a JDK that cannot run the formatter to the IDE's runtime. A missing native binary falls back to a separate JVM with its notification, as before. Inside the IDE, a file the formatter fails on with an Error is formatted in a JVM on the IDE's runtime, and a formatter that does not load or link there switches the IDE process off until the IDE restarts, with one error in idea.log for the plugin's error reports. Apply no longer checks the binary and the JDK while Enable open-java-format is unticked: the page shows neither then, so there was nothing on it to put right. Closes #223.
Why the formatter runs inside the IDE by default, how it reaches javac there without anyone editing VM options, why it is loaded apart from the IDE's classes, and what was rejected: a formatter daemon, asking users for --add-exports, the plugin's class loader as the formatter's parent, and leaving a JVM for each call as the default.
…fter it The settings said how the formatter runs twice: formatterMode, and beside it useNativeImage, written with every mode for a plugin from before the modes. And the JDK of the separate JVM was formatterJdk, which a mode added later, a daemon with a JDK of its own for one, could not tell from its own. formatterMode alone says it now, as in-process, jvm or native: words of the file's own, which stay when a constant is renamed. A file from before the modes has useNativeImage instead. It is read once, when the file is loaded: true switches the project to native, and the setting is emptied, so the IDE writes the file without it from then on. With neither, or with a mode this plugin does not know, a project formats inside the IDE. A setting of one mode has that mode at the start of its name: jvmJdk, which was formatterJdk and was not released under that name, and nativeImagePath, which keeps the name it was released with. A plugin from before the modes that reads a file written since finds no useNativeImage, and formats in a JVM of its own. ADR-005 says so.
abashev
deleted the
abashev/intellij-plugin-format-in-the-ides-jvm-without-a
branch
November 23, 2026 15:55
This was referenced Nov 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #223. Independent of the other open pull requests; based on
21.X.X.The IntelliJ plugin started a process for every Reformat and every Optimize imports: a JVM on the IDE's runtime, 0.3 s to 1 s a file, or the native binary. Optimize imports runs under a read action, and "Optimize imports on the fly" runs it while typing (#253). The plugin now loads the formatter into the IDE's own JVM, where a call takes 124 ms on a file of 4,026 lines and 9 ms to fix its imports once warm (the times of #223), and the settings page offers the two other ways to run it.
The settings page
Use native binary gives way to three radio buttons under Run the formatter, one of them chosen for a project:
The options of the ways not chosen stay on the page, greyed, rather than hidden as the native options were. Active formatter still names what runs once the page is applied, from the same
FormatterSelectionthatFormatterProviderstarts the formatter with.Existing projects
A project whose settings switch the native binary on keeps it. Every other project moves from a JVM for each file into the IDE process.
The settings file
formatterModealone says how a project formats, in.idea/open-java-format.xml, asin-process,jvmornative. With no mode, or one this plugin does not know, a project formats inside the IDE.useNativeImage, which a file from before the modes has, is read once, when the file is loaded:trueswitches the project tonative, and the setting is emptied, so the IDE writes the file without it from then on. The plugin no longer writes it.jvmJdkfor the JDK of the separate JVM, andnativeImagePath, which keeps the name it was released with. A mode added later, such as a daemon with a JDK of its own, then has settings no other mode reads.Inside the IDE
jdk.compilerexports to nobody. IntelliJ IDEA starts its JVM with--add-opens=java.base/java.lang=ALL-UNNAMED: it is in theproduct-info.jsonof 2024.2.6, the oldest IDE the plugin supports, and of 2026.3. Withjava.langopen,JavacAccesscallsModule.implAddExports, the method--add-exportsends in, for the formatter's class loader alone. Nobody edits VM options, and javac stays unexported to the plugin and to the rest of the IDE. The method is not public API:JavacAccess.unavailablesays beforehand whether it can be called, and the page greys the IDE process out when it cannot. A user who did add--add-exportsto the VM options, as google-java-format asks, gets the IDE process without the call.FormatterClassLoader's parent is the JDK's platform class loader, which finds javac and nothing of the IDE. From the plugin come only the types the two exchange: the classes ofopen-java-format-spi, and Guava, which their signatures name.InProcessFormatterskeeps a formatter loaded per set of jars, for every project that formats with them, and compares the jars' size and time for every file, so that a jar rebuilt in place formats the next file, as in a JVM started for it.FormatterCommandRunnerleaves the file alone without a message.InProcessFormatterServicedoes the same: otherwise "Optimize imports on the fly" would show a balloon on every highlighting pass of a file being typed in.Error, a stack overflow for one, is formatted in a JVM on the IDE's runtime. A formatter that does not load or link in the IDE switches the IDE process off until the IDE restarts: the page says why, andidea.loggets one error, for the plugin's error reports. The formatter is loaded with the first file, in the background, not whenFormatterProvideris asked for it, which can be on the UI thread.ADR-005 has the alternatives: a formatter daemon, asking users for
--add-exports, the plugin's class loader as the formatter's parent, and leaving a JVM for each call as the default.What falls back
With a warning in
idea.logand a line under Active formatter, and no balloon:A missing native binary falls back to a separate JVM with its notification, as before.
What changes for users
useNativeImage, and formats in a JVM of its own.openjavaformat/docs, with new screenshots of the settings page.Tests
JavacAccessTeststarts JVMs of its own, since the build gives the JVM of the tests--add-exportsfor javac to everything:A.input, by all three methods ofFormatterService, leaves a syntax error alone, and javac is not exported to the plugin's own module;implAddExports;InProcessFormattersTestFormatterClassLoaderTest: every class in the SPI's jar is among those taken from the plugin, so a class added to the SPI cannot go unshared unnoticed.FormatterJdksTest: the java and the version of the IDE's runtime, the project SDK and a named JDK, and the reason for each JDK that cannot run the formatter.FormatterSelectionTest: every mode with and without what it needs, and that only what would run is looked for.FormatterProviderTest: the service of each mode, and that a change of JDK is not served from the cache.PalantirJavaFormatSettingsTest: a file from before the modes switches tonativeand losesuseNativeImage, withfalseor without it a project formats inside the IDE, the text of each mode, and the names of the modes' settings.PalantirJavaFormatConfigurableTest: the radio buttons, the greyed IDE process with its reason, the list of JDKs with one that cannot be chosen, and what Apply refuses../gradlew checkpasses on JDK 21, and:open-java-format-idea-plugin:checkpasses on each of the first two commits on its own.verifyPlugin: compatible with IC-242.26775.15 and IU-263.7307.200, with no new usage of experimental or deprecated API.To check in a running IDE
Not tried yet. The tests run in the headless test IDE, whose class loader is not the
PluginClassLoadera plugin gets in a running IDE:idea.loghasopen-java-format: using in-process formatterfor the first.Not done here
Found on the way, each with an issue of its own:
formatDifffails with "git diff took too long to terminate" on a diff larger than 64 KB. The diff of this pull request is one, so its files were formatted with the command line.