From a1016c29d3adbe59d496433e1e2bc126605b1bcb Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Wed, 25 Nov 2026 20:26:46 +0300 Subject: [PATCH 1/3] Leave out the empty ranges the out-of-process formatters get IntelliJ IDEA can pass an empty range among those it reformats. The native and the separate-JVM services turned [n, n) into "n:n-1", which --character-ranges, whose ranges are closed, rejects with "Invalid range", and Reformat failed. FormatterServiceImpl, in process, already leaves empty ranges out: its TreeRangeSet drops them. The two services now leave them out too, and format nothing when no range is left, as FormatterServiceImpl does: without --character-ranges the command line would format the whole file. formatDiff, which drops the empty range of a blank line, so no longer reformats the whole file when a diff adds only blank lines. Closes #275 --- .../BootstrappingFormatterService.java | 7 +++- .../NativeImageFormatterService.java | 10 +++-- .../javaformat/bootstrap/RangeUtils.java | 16 +++++++ .../bootstrap/FormatterServicesTest.java | 38 +++++++++++++++++ .../bootstrap/JvmFormatterServicesTest.java | 42 +++++++++++++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) diff --git a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java index f9ad74638..488fe1e5d 100644 --- a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java +++ b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java @@ -82,13 +82,18 @@ public String fixImports(String input) throws FormatterException { @SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList") private ImmutableList getFormatReplacementsInternal(String input, Collection> ranges) throws IOException { + var characterRanges = RangeUtils.toStringRanges(ranges); + if (characterRanges.isEmpty()) { + // Nothing to format, as in process: without --character-ranges the command would format the whole file. + return ImmutableList.of(); + } FormatterCliArgs command = new FormatterCliArgs( jdkPath, jvmArgsForVersion(jdkMajorVersion), implementationClassPath, /* fixImportsOnly= */ false, /* outputReplacements= */ true, - ranges.stream().map(RangeUtils::toStringRange).toList()); + characterRanges); Optional output = FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent())); diff --git a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java index 8a7fcbd88..f12342823 100644 --- a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java +++ b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java @@ -45,13 +45,15 @@ public NativeImageFormatterService(Path nativeImagePath) { @Override @SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList") public ImmutableList getFormatReplacements(String input, Collection> ranges) { + var characterRanges = RangeUtils.toStringRanges(ranges); + if (characterRanges.isEmpty()) { + // Nothing to format, as in process: without --character-ranges the command would format the whole file. + return ImmutableList.of(); + } Optional output = Optional.empty(); try { FormatterNativeImageArgs command = new FormatterNativeImageArgs( - nativeImagePath, - /* fixImportsOnly= */ false, - /* outputReplacements= */ true, - ranges.stream().map(RangeUtils::toStringRange).toList()); + nativeImagePath, /* fixImportsOnly= */ false, /* outputReplacements= */ true, characterRanges); output = FormatterCommandRunner.runWithStdin( command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent())); diff --git a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/RangeUtils.java b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/RangeUtils.java index e6be7b854..c7ec62c52 100644 --- a/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/RangeUtils.java +++ b/open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/RangeUtils.java @@ -17,7 +17,10 @@ package com.palantir.javaformat.bootstrap; import com.google.common.collect.BoundType; +import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; +import java.util.Collection; +import java.util.List; public final class RangeUtils { @@ -31,5 +34,18 @@ public static String toStringRange(Range range) { return String.format("%s:%s", lower, higher); } + /** + * Returns the ranges as "--character-ranges" takes them. Its ranges are closed, so an empty range, which IntelliJ + * IDEA passes among the others, has no form there: it is left out, as {@code FormatterServiceImpl} leaves it out + * in process. + */ + static List toStringRanges(Collection> ranges) { + return ranges.stream() + .map(range -> range.canonical(DiscreteDomain.integers())) + .filter(range -> !range.isEmpty()) + .map(RangeUtils::toStringRange) + .toList(); + } + private RangeUtils() {} } diff --git a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java index 2372fb3c0..3acc80bd7 100644 --- a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java +++ b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java @@ -134,6 +134,44 @@ class S { assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput); } + @ParameterizedTest + @MethodSource("getFormatters") + void leaves_out_the_empty_ranges(FormatterService formatterService) throws FormatterException { + String input = """ + class A { + int a; + int b; + } + """; + int a = input.indexOf(" int a"); + int b = input.indexOf(" int b"); + + List replacements = formatterService.getFormatReplacements( + input, + List.of( + Range.closedOpen(a, input.indexOf('\n', a)), + Range.closedOpen(b, b), + Range.closedOpen(input.length(), input.length()))); + + assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(""" + class A { + int a; + int b; + } + """); + } + + @ParameterizedTest + @MethodSource("getFormatters") + void formats_nothing_without_a_range_that_is_not_empty(FormatterService formatterService) + throws FormatterException { + String input = "class A { int a; }\n"; + + assertThat(formatterService.getFormatReplacements(input, List.of(Range.closedOpen(10, 10)))) + .isEmpty(); + assertThat(formatterService.getFormatReplacements(input, List.of())).isEmpty(); + } + private static Stream getFormatters() { return Stream.of( new BootstrappingFormatterService( diff --git a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java index 12b0937d9..640a6391e 100644 --- a/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java +++ b/open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/JvmFormatterServicesTest.java @@ -16,12 +16,16 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.google.common.collect.Range; +import com.palantir.javaformat.Utils; import com.palantir.javaformat.java.FormatterException; import com.palantir.javaformat.java.FormatterService; import com.palantir.javaformat.java.FormatterServiceImpl; +import com.palantir.javaformat.java.Replacement; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -41,6 +45,44 @@ void fixes_only_the_imports(FormatterService formatterService) throws FormatterE assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput); } + @ParameterizedTest + @MethodSource("getFormatters") + void leaves_out_the_empty_ranges(FormatterService formatterService) throws FormatterException { + String input = """ + class A { + int a; + int b; + } + """; + int a = input.indexOf(" int a"); + int b = input.indexOf(" int b"); + + List replacements = formatterService.getFormatReplacements( + input, + List.of( + Range.closedOpen(a, input.indexOf('\n', a)), + Range.closedOpen(b, b), + Range.closedOpen(input.length(), input.length()))); + + assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(""" + class A { + int a; + int b; + } + """); + } + + @ParameterizedTest + @MethodSource("getFormatters") + void formats_nothing_without_a_range_that_is_not_empty(FormatterService formatterService) + throws FormatterException { + String input = "class A { int a; }\n"; + + assertThat(formatterService.getFormatReplacements(input, List.of(Range.closedOpen(10, 10)))) + .isEmpty(); + assertThat(formatterService.getFormatReplacements(input, List.of())).isEmpty(); + } + private static Stream getFormatters() { return Stream.of( new FormatterServiceImpl(), From a31c67b85cfc04a82cf7f94f72218d1b958b72d1 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Wed, 25 Nov 2026 20:39:30 +0300 Subject: [PATCH 2/3] Build macOS on Apple silicon for a release only, on the newest images CI's native job leaves out macOS on Apple silicon, commented out in its matrix; release.yml still builds it. Every native leg moves to the newest image: macos-latest for Apple silicon, and, as arm Linux and Intel macOS have no free -latest label, ubuntu-26.04-arm and macos-26-intel. --- .github/workflows/ci.yml | 9 ++++++--- .github/workflows/release.yml | 9 +++++---- README.md | 2 +- mise.toml | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13890a160..49c224d5f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,11 +170,14 @@ jobs: strategy: fail-fast: false matrix: + # The newest images. Arm Linux and Intel macOS have no free -latest label, so their images are + # named. runner: - ubuntu-latest - - ubuntu-24.04-arm - - macos-26 - - macos-15-intel + - ubuntu-26.04-arm + # macOS on Apple silicon is built for a release only, in release.yml. + # - macos-latest + - macos-26-intel - windows-latest steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 81da1be3c..fec27d11e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -120,7 +120,7 @@ jobs: retention-days: 7 # One job per platform, as in ci.yml, but here each one hands its binary to the deploy job below - # instead of throwing it away. + # instead of throwing it away. macOS on Apple silicon is built here only: ci.yml leaves it out. native-images: name: native image (${{ matrix.platform }}) runs-on: ${{ matrix.runner }} @@ -132,15 +132,16 @@ jobs: strategy: fail-fast: false matrix: + # The newest images, as in ci.yml. include: - platform: linux-x86-64 runner: ubuntu-latest - platform: linux-aarch64 - runner: ubuntu-24.04-arm + runner: ubuntu-26.04-arm - platform: macos-aarch64 - runner: macos-26 + runner: macos-latest - platform: macos-x86-64 - runner: macos-15-intel + runner: macos-26-intel - platform: windows-x86-64 runner: windows-latest diff --git a/README.md b/README.md index 00c244d54..4bd00f752 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ mise run ci:native # actually run the linux-aarch64 native job Two caveats: -- **act cannot containerise macOS**, so the `macos-15-intel` leg has to be checked by building the +- **act cannot containerise macOS**, so the `macos-26-intel` leg has to be checked by building the native image on a Mac. - **On an Apple Silicon host** the `ubuntu-24.04` container runs as arm64, so the job labelled `ubuntu-latest` produces an aarch64 binary locally. Fine for exercising the workflow, not a diff --git a/mise.toml b/mise.toml index 0dc300c06..3acdd0cf3 100644 --- a/mise.toml +++ b/mise.toml @@ -18,7 +18,7 @@ run = "act push --job build --env JAVA_HOME=/opt/hostedtoolcache/Java_Temurin-Ho [tasks."ci:native"] description = "Run the linux-aarch64 native job locally in Docker" -run = "act push --job native --matrix runner:ubuntu-24.04-arm" +run = "act push --job native --matrix runner:ubuntu-26.04-arm" # ---- GitHub Actions secrets ---- # Read from 1Password at apply time; nothing secret is stored in the repo. Requires gh From 198bbd2ca68c9d86da0eaf57c8460cc2f89aede5 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Wed, 25 Nov 2026 22:18:41 +0300 Subject: [PATCH 3/3] List the release's native runners alone, as ci.yml does The platform names in release.yml's matrix only named the jobs and their artifacts: the binaries are named, and their Maven classifiers read, from what Gradle builds, and the deploy jobs collect every native-image-* artifact. The runner label names both now. --- .github/workflows/release.yml | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fec27d11e..d4ac7997e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -122,7 +122,7 @@ jobs: # One job per platform, as in ci.yml, but here each one hands its binary to the deploy job below # instead of throwing it away. macOS on Apple silicon is built here only: ci.yml leaves it out. native-images: - name: native image (${{ matrix.platform }}) + name: native image (${{ matrix.runner }}) runs-on: ${{ matrix.runner }} timeout-minutes: 90 # bash on Windows too, as in ci.yml. @@ -133,17 +133,12 @@ jobs: fail-fast: false matrix: # The newest images, as in ci.yml. - include: - - platform: linux-x86-64 - runner: ubuntu-latest - - platform: linux-aarch64 - runner: ubuntu-26.04-arm - - platform: macos-aarch64 - runner: macos-latest - - platform: macos-x86-64 - runner: macos-26-intel - - platform: windows-x86-64 - runner: windows-latest + runner: + - ubuntu-latest + - ubuntu-26.04-arm + - macos-latest + - macos-26-intel + - windows-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -174,7 +169,7 @@ jobs: - name: Hand the binary to the deploy job uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: native-image-${{ matrix.platform }} + name: native-image-${{ matrix.runner }} path: | open-java-format-native/build/native/nativeCompile/open-java-format-* !open-java-format-native/build/native/nativeCompile/*.txt