Skip to content

Let a build name the version of the native image - #297

Merged
abashev merged 5 commits into
21.X.Xfrom
abashev/native-formatter-version-property
Nov 30, 2026
Merged

abashev merged 5 commits into
21.X.Xfrom
abashev/native-formatter-version-property

Conversation

@abashev

@abashev abashev commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Before this PR

With openjavaformat.native.formatter=true the Gradle plugin formats with open-java-format-native of its own version, as palantir-java-format's does. The version comes from the plugin jar's Implementation-Version. A build can declare another image in palantirJavaFormatNative, but only with the full coordinates, classifier and extension of each platform.

A release line that publishes no native image of its own would have nothing to resolve. That is the case of the line for Java 17 (#293): the native binary does not depend on the user's JDK, so the line formats with the image of a 21.x release of the same output.

After this PR

  • openjavaformat.native.formatter.version, a Gradle property, names the version of open-java-format-native the plugin asks for.
    • By default it is the plugin's own version, so nothing changes for a build that does not set it.
    • The platform's classifier and extension are filled in as before.
    • An image declared in palantirJavaFormatNative still wins over both.
  • A new test in NativeImageResolutionTest, with and without the configuration cache. A build that declares no image and sets the property formats with the image of that version. Without the property the same build fails to resolve open-java-format-native.

The 178 tests of the Gradle plugin pass on JDK 21.

Possible downsides?

  • One more property to document. The Gradle page of openjavaformat.dev should name it next to openjavaformat.native.formatter, in a docs PR of its own.
  • A build that sets a version far from the plugin's formats with that image's output, which is the point of the property but also lets a build mix lines by mistake.

With openjavaformat.native.formatter the Gradle plugin formats with
open-java-format-native of its own version, unless the build declares the
image in palantirJavaFormatNative. A release line that publishes no native
image of its own, as the one for Java 17 (#293), would have nothing to
resolve.

openjavaformat.native.formatter.version names the version the plugin asks
for, the plugin's own by default, so nothing changes for a build that does not
set it. NativeImageResolutionTest formats with the image of the version the
build names, in a build that declares no image; without the property the same
build fails to resolve open-java-format-native.
Gradle's orElse takes no null, and the plugin has no Implementation-Version when TestKit runs it from its
classes: String.valueOf keeps asking for open-java-format-native:null there, so Error Prone's IdentityConversion
is suppressed with that reason. The list of the default dependency is a List.of, as in new code.
Without openjavaformat.native.formatter.version and without an Implementation-Version of its own, as when TestKit
runs it from its classes, the plugin used to ask for open-java-format-native:null. It now leaves the default image
out and warns that it cannot tell the version; the version comes through ProviderFactory.provider, a public API,
in place of String.valueOf.

Only the default image needs the version: the configuration is still registered, so a build that declares its own
image formats with it without one. NativeImageResolutionTest checks both.

Along the way: the notation of the default image is built with String.formatted, FormatDiffTask's Logger is an
instance field, and blank lines set off the log lines from the formatting they announce.
Gradle runs the default dependencies of palantirJavaFormatNative even when it only looks at the root project's
variants, as it does to resolve the formatter's classpath, so a build with the native formatter off was warned
that the plugin could not tell the image's version. The plugin now warns only when formatDiff is to run the image,
by the same NativeImageSupport check that formatDiff makes.

NativeImageResolutionTest checks both: with the native formatter on and nothing naming the version, the warning;
with it off, formatDiff formats with the formatter inside the plugin and says nothing of the version.
The test names openjavaformat.native.formatter.version and the warning about it as text, not through the plugin's
constant: a typo in the constant, or a rename of it, then fails the test instead of passing with it.
@abashev
abashev merged commit a884c7b into 21.X.X Nov 30, 2026
17 checks passed
@abashev
abashev deleted the abashev/native-formatter-version-property branch November 30, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant