Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .palantir/revapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,34 @@ acceptedBreaks:
new: "method java.lang.String com.palantir.javaformat.java.FormatterService::fixImports(java.lang.String)\
\ throws com.palantir.javaformat.java.FormatterException"
justification: "new fixImports only method added to spi"
"2.83.0":
com.palantir.javaformat:gradle-palantir-java-format:
- code: "java.annotation.removed"
old: "method void com.palantir.javaformat.gradle.SpotlessInterop::<init>(java.util.function.Supplier<com.palantir.javaformat.java.FormatterService>)"
new: "method void com.palantir.javaformat.gradle.SpotlessInterop::<init>()"
justification: "Changed method signatures PalantirJavaFormatStep::create and\
\ SpotlessInterop::<init> are not used outside of this project, SpotlessInterop::getWorkerExecutor()\
\ is additive and so should not cause issues and SpotlessInterop is not used\
\ outside of this project"
- code: "java.method.abstractMethodAdded"
new: "method org.gradle.workers.WorkerExecutor com.palantir.javaformat.gradle.SpotlessInterop::getWorkerExecutor()"
justification: "Changed method signatures PalantirJavaFormatStep::create and\
\ SpotlessInterop::<init> are not used outside of this project, SpotlessInterop::getWorkerExecutor()\
\ is additive and so should not cause issues and SpotlessInterop is not used\
\ outside of this project"
- code: "java.method.numberOfParametersChanged"
old: "method void com.palantir.javaformat.gradle.SpotlessInterop::<init>(java.util.function.Supplier<com.palantir.javaformat.java.FormatterService>)"
new: "method void com.palantir.javaformat.gradle.SpotlessInterop::<init>()"
justification: "Changed method signatures PalantirJavaFormatStep::create and\
\ SpotlessInterop::<init> are not used outside of this project, SpotlessInterop::getWorkerExecutor()\
\ is additive and so should not cause issues and SpotlessInterop is not used\
\ outside of this project"
- code: "java.method.parameterTypeChanged"
old: "parameter com.diffplug.spotless.FormatterStep com.palantir.javaformat.gradle.spotless.PalantirJavaFormatStep::create(org.gradle.api.artifacts.Configuration,\
\ ===java.util.function.Supplier<com.palantir.javaformat.java.FormatterService>===)"
new: "parameter com.diffplug.spotless.FormatterStep com.palantir.javaformat.gradle.spotless.PalantirJavaFormatStep::create(org.gradle.api.artifacts.Configuration,\
\ ===org.gradle.workers.WorkerExecutor===)"
justification: "Changed method signatures PalantirJavaFormatStep::create and\
\ SpotlessInterop::<init> are not used outside of this project, SpotlessInterop::getWorkerExecutor()\
\ is additive and so should not cause issues and SpotlessInterop is not used\
\ outside of this project"
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import com.diffplug.gradle.spotless.SpotlessExtension;
import com.google.common.collect.ImmutableList;
import com.palantir.javaformat.java.FormatterService;
import java.util.function.Supplier;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

Expand All @@ -32,10 +30,7 @@ public void apply(Project project) {
Project rootProject = project.getRootProject();
rootProject.getPluginManager().apply(PalantirJavaFormatProviderPlugin.class);

Supplier<FormatterService> memoizedService =
rootProject.getExtensions().getByType(JavaFormatExtension.class)::serviceLoad;

SpotlessInterop spotlessInterop = rootProject.getObjects().newInstance(SpotlessInterop.class, memoizedService);
SpotlessInterop spotlessInterop = rootProject.getObjects().newInstance(SpotlessInterop.class);
project.getPluginManager().withPlugin("java", _javaPlugin -> {
SPOTLESS_PLUGINS.forEach(
spotlessPluginId -> project.getPluginManager().withPlugin(spotlessPluginId, _spotlessPlugin -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import com.diffplug.spotless.FormatterStep;
import com.palantir.javaformat.gradle.spotless.NativePalantirJavaFormatStep;
import com.palantir.javaformat.gradle.spotless.PalantirJavaFormatStep;
import com.palantir.javaformat.java.FormatterService;
import java.util.function.Supplier;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.JavaVersion;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Nested;
import org.gradle.workers.WorkerExecutor;

/**
* Class that exists only to encapsulate accessing spotless classes, so that Gradle can generate a decorated class for
Expand All @@ -36,18 +35,14 @@
public abstract class SpotlessInterop implements Action<JavaExtension> {
private static final Logger logger = Logging.getLogger(SpotlessInterop.class);

private final Supplier<FormatterService> formatterService;

@Nested
protected abstract NativeImageSupport getNativeImageSupport();

@Inject
protected abstract ConfigurationContainer getConfigurations();

@Inject
public SpotlessInterop(Supplier<FormatterService> formatterService) {
this.formatterService = formatterService;
}
protected abstract WorkerExecutor getWorkerExecutor();

@Override
public void execute(JavaExtension java) {
Expand All @@ -62,8 +57,9 @@ private FormatterStep spotlessJavaFormatStep() {
return NativePalantirJavaFormatStep.create(
getConfigurations().getByName(NativeImageFormatProviderPlugin.NATIVE_CONFIGURATION_NAME));
}
logger.info("Using the Java-based formatter {}", JavaVersion.current());
logger.info("Using the Java-based formatter with Worker API {}", JavaVersion.current());
return PalantirJavaFormatStep.create(
getConfigurations().getByName(PalantirJavaFormatProviderPlugin.CONFIGURATION_NAME), formatterService);
getConfigurations().getByName(PalantirJavaFormatProviderPlugin.CONFIGURATION_NAME),
getWorkerExecutor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* (c) Copyright 2025 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.gradle.spotless;

import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.workers.WorkParameters;

public interface FormatJavaParameters extends WorkParameters {

RegularFileProperty getInputFile();

RegularFileProperty getOutputFile();

ConfigurableFileCollection getFormatterClasspath();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* (c) Copyright 2025 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.gradle.spotless;

import com.google.common.base.Suppliers;
import com.palantir.javaformat.java.FormatterService;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.ServiceLoader;
import java.util.function.Supplier;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.workers.WorkAction;

public abstract class FormatJavaWorkAction implements WorkAction<FormatJavaParameters> {

private static final Logger logger = Logging.getLogger(FormatJavaWorkAction.class);

private static final Supplier<FormatterService> formatterService = Suppliers.memoize(() -> {
logger.info("Loading FormatterService in worker daemon");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this locally, does it log this per file or just once per gradle invocation? Not sure is gradle is doing something funky with classloaders even in process isolation that means this may be new each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once per gradle invocation - or at least per spotless invocation:

> Task :foo-api:spotlessJava
Formatting file #1 in worker - classIdentity=269645944, file=palantir-java-format-input-1956597832421626908.java
Loading FormatterService in worker daemon - classLoader=VisitableURLClassLoader(worker-loader), classIdentity=269645944

> Task :foo:spotlessJava
Formatting file #1 in worker - classIdentity=687141933, file=palantir-java-format-input-12790676627999507747.java
Loading FormatterService in worker daemon - classLoader=VisitableURLClassLoader(worker-loader), classIdentity=687141933
Formatting file #2 in worker - classIdentity=269645944, file=palantir-java-format-input-1807148048456068675.java
Formatting file #2 in worker - classIdentity=687141933, file=palantir-java-format-input-12981619963290984761.java
Formatting file #3 in worker - classIdentity=269645944, file=palantir-java-format-input-13174205067034597682.java
Formatting file #3 in worker - classIdentity=687141933, file=palantir-java-format-input-7978830245738795631.java

return ServiceLoader.load(FormatterService.class, FormatJavaWorkAction.class.getClassLoader())
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"No FormatterService found. Ensure palantir-java-format is on the worker classpath."));
});

@Override
public void execute() {
FormatJavaParameters params = getParameters();

try {
File inputFile = params.getInputFile().get().getAsFile();
File outputFile = params.getOutputFile().get().getAsFile();

String input = Files.readString(inputFile.toPath());

logger.debug("Formatting file with worker process: {}", inputFile.getName());

String output = formatterService.get().formatSourceReflowStringsAndFixImports(input);

Files.writeString(outputFile.toPath(), output);

} catch (IOException e) {
throw new UncheckedIOException("Failed to format Java file", e);
} catch (Exception e) {
throw new RuntimeException(
"Formatting failed in worker daemon. This may indicate a bug in palantir-java-format "
+ "or insufficient resources. Original error: " + e.getMessage(),
e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.palantir.javaformat.java.FormatterService;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.function.Supplier;
import org.gradle.api.artifacts.Configuration;
import org.gradle.workers.WorkQueue;
import org.gradle.workers.WorkerExecutor;

public final class PalantirJavaFormatStep {

Expand All @@ -35,10 +37,10 @@ private PalantirJavaFormatStep() {}
private static final String NAME = "palantir-java-format";

/** Creates a step which formats everything - code, import order, and unused imports. */
public static FormatterStep create(Configuration palantirJavaFormat, Supplier<FormatterService> memoizedService) {
public static FormatterStep create(Configuration palantirJavaFormat, WorkerExecutor workerExecutor) {
ensureImplementationNotDirectlyLoadable();
return FormatterStep.createLazy(
NAME, () -> new State(palantirJavaFormat::getFiles, memoizedService), State::createFormat);
NAME, () -> new State(palantirJavaFormat::getFiles, workerExecutor), State::createFormat);
}

static final class State implements Serializable {
Expand All @@ -65,19 +67,19 @@ static final class State implements Serializable {

private final transient Supplier<Iterable<File>> jarsSupplier;

// Transient as this is not serializable.
private final transient Supplier<FormatterService> memoizedFormatter;
private final transient WorkerExecutor workerExecutor;

/**
* Build a cacheable state for spotless from the given jars, that uses the given {@link FormatterService}.
* Build a cacheable state for spotless from the given jars, using the Worker API for process isolation.
*
* @param jarsSupplier Supplies the jars that contain the palantir-java-format implementation. This is only used for caching and
* up-to-dateness purposes.
* @param workerExecutor WorkerExecutor for process isolation.
*/
@SuppressWarnings("for-rollout:NullAway")
State(Supplier<Iterable<File>> jarsSupplier, Supplier<FormatterService> memoizedFormatter) {
State(Supplier<Iterable<File>> jarsSupplier, WorkerExecutor workerExecutor) {
this.jarsSupplier = jarsSupplier;
this.memoizedFormatter = memoizedFormatter;
this.workerExecutor = workerExecutor;
}

@SuppressWarnings("NullableProblems")
Expand All @@ -91,12 +93,55 @@ FormatterFunc createFormat() {
// https://github.com/diffplug/spotless/blob/228eb10af382b19e130d8d9479f7a95238cb4358/lib/src/main/java/com/diffplug/spotless/FileSignature.java#L138-L143
this.jarsSignature = FileSignature.signAsSet(jars);

return memoizedFormatter.get().formatSourceReflowStringsAndFixImports(input);
return formatWithWorker(input, jars);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}

private String formatWithWorker(String input, Iterable<File> jars) throws IOException {
File inputFile =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the best way I could come-up with to get the formatted string in and out

Files.createTempFile("palantir-java-format-input-", ".java").toFile();
File outputFile = Files.createTempFile("palantir-java-format-output-", ".java")
.toFile();

try {
Files.writeString(inputFile.toPath(), input);

WorkQueue workQueue = workerExecutor.processIsolation(workerSpec -> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm failure rate has increased to about 30% I wonder if this is just adding loads of overhead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are creating a temp io file and a new worker for every single file

workerSpec.getClasspath().from(jars);
workerSpec.forkOptions(options -> {
options.jvmArgs(
"--add-exports",
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
});
});

workQueue.submit(FormatJavaWorkAction.class, parameters -> {
parameters.getInputFile().set(inputFile);
parameters.getOutputFile().set(outputFile);
parameters.getFormatterClasspath().from(jars);
});

workQueue.await();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to await to ensure formatting is done


return Files.readString(outputFile.toPath());
} catch (Exception e) {
throw new RuntimeException("Formatting failed using Worker API", e);
} finally {
inputFile.delete();
outputFile.delete();
}
}
}

private static void ensureImplementationNotDirectlyLoadable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,8 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
}
""".stripIndent(true)

// Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
// Worker API now handles JVM args directly, no need to set them in gradle.properties
file('gradle.properties') << '''
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
palantir.jdk.setup.enabled=true
'''.stripIndent(true)
file('gradle.properties') << extraGradleProperties
Expand Down