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..d4ac7997e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -120,9 +120,9 @@ 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 }}) + name: native image (${{ matrix.runner }}) runs-on: ${{ matrix.runner }} timeout-minutes: 90 # bash on Windows too, as in ci.yml. @@ -132,17 +132,13 @@ jobs: strategy: fail-fast: false matrix: - include: - - platform: linux-x86-64 - runner: ubuntu-latest - - platform: linux-aarch64 - runner: ubuntu-24.04-arm - - platform: macos-aarch64 - runner: macos-26 - - platform: macos-x86-64 - runner: macos-15-intel - - platform: windows-x86-64 - runner: windows-latest + # The newest images, as in ci.yml. + runner: + - ubuntu-latest + - ubuntu-26.04-arm + - macos-latest + - macos-26-intel + - windows-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -173,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 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 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(),