Skip to content

Commit d4a1480

Browse files
committed
Fail spotlessJava when the Gradle JVM does not export javac
The formatter checks that javac's internals are exported to it when it loads, and inside the Spotless step that failure became a lint of every file: spotlessJava succeeded, the build cache kept the lint, and spotlessApply failed on it until the cache was cleared, even after the flags were added. spotlessJava now runs the same check before the step does, as an action of its own, without loading the formatter: the flags export the packages to every unnamed module, so the plugin's module answers for the formatter's. spotlessJava fails with the message formatDiff gives, nothing is stored in the build cache, and once the flags are there the next build formats. A file the formatter cannot parse is still a lint of that file.
1 parent c04a321 commit d4a1480

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/FormatterServiceLoader.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ private static FormatterService loadUncached(Collection<File> jars) {
8686
return Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class, classLoader));
8787
}
8888

89+
/**
90+
* Fails with the fix spelt out when this JVM does not export javac's internals to the formatter, without loading
91+
* it. The flags export them to every unnamed module, so the plugin's own module answers for the formatter's.
92+
*/
93+
public static void checkJavacIsExported() {
94+
checkJavacIsExportedTo(FormatterServiceLoader.class.getClassLoader());
95+
}
96+
8997
/**
9098
* Fails with the fix spelt out when this JVM does not export javac's internals to the formatter's class loader:
9199
* either the native binary, which runs outside the JVM, or the {@code --add-exports} flags on the Gradle JVM.

‎gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatSpotlessPlugin.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import com.diffplug.gradle.spotless.SpotlessExtension;
1919
import com.google.common.collect.ImmutableList;
20+
import org.gradle.api.Action;
2021
import org.gradle.api.Plugin;
2122
import org.gradle.api.Project;
23+
import org.gradle.api.Task;
2224
import org.gradle.api.plugins.JavaPluginExtension;
2325

2426
public abstract class PalantirJavaFormatSpotlessPlugin implements Plugin<Project> {
@@ -49,7 +51,20 @@ public void apply(Project project) {
4951
SpotlessExtension spotlessExtension =
5052
project.getExtensions().getByType(SpotlessExtension.class);
5153
spotlessExtension.java(spotlessInterop);
54+
// Before the step runs: Spotless turns what a step throws into a lint of the file, which
55+
// the build cache would then hand back after the build is fixed.
56+
project.getTasks()
57+
.named("spotlessJava")
58+
.configure(spotlessJava -> spotlessJava.doFirst(new CheckJavacIsExported()));
5259
}));
5360
});
5461
}
62+
63+
/** A class, not a lambda: the configuration cache stores it with the task. */
64+
private static final class CheckJavacIsExported implements Action<Task> {
65+
@Override
66+
public void execute(Task _task) {
67+
FormatterServiceLoader.checkJavacIsExported();
68+
}
69+
}
5570
}

0 commit comments

Comments
 (0)