Skip to content

formatDiff and the Spotless step fail with the configuration cache #193

Closed

Description

@abashev

What happens

With the configuration cache on (--configuration-cache, or org.gradle.configuration-cache=true), two of the Gradle plugin's tasks fail. Reproduced with Gradle 9.3.1, Spotless 8.10.4 and JDK 21, with the plugin built from 21.X.X, whose sources are unchanged since 21.0.1.

formatDiff, with the Java-based and the native formatter alike:

> Task :formatDiff FAILED
1 problem was found storing the configuration cache.
- Task `:formatDiff` of type `com.palantir.javaformat.gradle.PalantirJavaFormatPlugin$FormatDiffTask`: invocation of 'Task.project' at execution time is unsupported with the configuration cache.

The entry is discarded, so every run fails the same way.

spotlessCheck and spotlessApply, with com.diffplug.spotless applied: every file gets a lint error and the task fails.

> Task :spotlessJavaCheck FAILED
> There were 1 lint error(s), they must be fixed or suppressed.
  src/main/java/Foo.java:LINE_UNDEFINED open-java-format(java.lang.NullPointerException) Cannot invoke "java.util.function.Supplier.get()" because "this.jarsSupplier" is null (...)

The next run does not reuse the entry either: configuration cache cannot be reused because an input to unknown location has changed. In the same project without the open-java-format step, Spotless stores its entry and reuses it.

The IntelliJ tasks (updateOpenJavaFormatXml, updateWorkspaceXml, updateExternalDependenciesXml) work: their entry is stored and reused. Gradle turns the cache off when its own idea task runs, which is not compatible with it; that is not the plugin's doing.

Why

  • FormatDiffTask.formatDiff() calls getProject() when it runs, for the project directory and for the root project's JavaFormatExtension, which loads the formatter.
  • PalantirJavaFormatStep.State keeps the formatter jars and the loaded FormatterService in transient fields. The configuration cache serializes the Spotless step, and the step it reads back has both fields null, on the run that stores the entry too.

SupportsCurrentSpotlessTest, the only plugin test with the cache on, runs classes: it creates the Spotless step but never formats with it. Every other plugin test runs with the cache off.

Proposal

  • formatDiff gets what it needs as task properties set at configuration time, the project directory and the formatter classpath, and loads the formatter when it runs, without Project.
  • The Spotless step keeps state that survives serialization: the formatter jar files, from which the formatter is loaded on first use. The jars stay an input, so another formatter version still makes spotlessJava out of date.
  • Tests run formatDiff, spotlessApply, spotlessCheck and the IntelliJ tasks twice with the cache on, and check that the first run stores the entry and the second reuses it.

Until then

Run these tasks with --no-configuration-cache, or set org.gradle.configuration-cache=false in the project's gradle.properties.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions