Skip to content

Commit 2091e95

Browse files
committed
Gate annotation processor configs independently, add test
1 parent abf808f commit 2091e95

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

scip-gradle-plugin/src/main/java/org/scip_code/scip_java/gradle/ScipGradlePlugin.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,8 @@ private void configureProject(Project project) {
3737
triggers.add("compileJava");
3838
triggers.add("compileTestJava");
3939

40-
boolean hasAnnotationPath;
41-
try {
42-
Configuration apConfig = project.getConfigurations().getByName("annotationProcessor");
43-
hasAnnotationPath = apConfig.isCanBeResolved() && !apConfig.getDependencies().isEmpty();
44-
} catch (Exception exc) {
45-
hasAnnotationPath = false;
46-
}
47-
4840
Object javacPluginDep = project.files(requiredExtra(extraProperties, "javacPluginJar"));
49-
boolean pluginAdded = tryAddJavacPlugin(project, javacPluginDep, hasAnnotationPath);
41+
boolean pluginAdded = tryAddJavacPlugin(project, javacPluginDep);
5042

5143
project
5244
.getTasks()
@@ -113,13 +105,32 @@ private void configureProject(Project project) {
113105
project.getTasks().create("scipPrintDependencies", WriteDependencies.class);
114106
}
115107

116-
private static boolean tryAddJavacPlugin(
117-
Project project, Object javacPluginDep, boolean hasAnnotationPath) {
108+
/**
109+
* javac discovers {@code -Xplugin:} plugins from the annotation processor path when {@code
110+
* -processorpath} is set, and only falls back to the classpath when it isn't. Gradle populates
111+
* {@code -processorpath} from the {@code annotationProcessor} (main) and {@code
112+
* testAnnotationProcessor} (test) configurations, so whenever one of them declares a processor we
113+
* must add the SCIP javac plugin to that same configuration or the corresponding compile task
114+
* fails with "plug-in not found: scip". The two configurations are independent (test does not
115+
* extend main), so each is checked separately.
116+
*/
117+
private static boolean hasAnnotationProcessors(Project project, String configurationName) {
118+
try {
119+
Configuration config = project.getConfigurations().getByName(configurationName);
120+
return config.isCanBeResolved() && !config.getDependencies().isEmpty();
121+
} catch (Exception exc) {
122+
return false;
123+
}
124+
}
125+
126+
private static boolean tryAddJavacPlugin(Project project, Object javacPluginDep) {
118127
try {
119128
project.getDependencies().add("compileOnly", javacPluginDep);
120129
project.getDependencies().add("testCompileOnly", javacPluginDep);
121-
if (hasAnnotationPath) {
130+
if (hasAnnotationProcessors(project, "annotationProcessor")) {
122131
project.getDependencies().add("annotationProcessor", javacPluginDep);
132+
}
133+
if (hasAnnotationProcessors(project, "testAnnotationProcessor")) {
123134
project.getDependencies().add("testAnnotationProcessor", javacPluginDep);
124135
}
125136
return true;

scip-java/src/test/kotlin/tests/GradleBuildToolTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ class GradleBuildToolTest : BuildToolHarness() {
5555
expectedScipFiles = 2,
5656
)
5757
)
58+
// Regression test: annotation processors declared only for the test source
59+
// set (testAnnotationProcessor) make Gradle pass -processorpath to
60+
// compileTestJava, so the SCIP javac plugin must be added to that same
61+
// configuration or plugin discovery fails with "plug-in not found: scip".
62+
add(
63+
checkGradleBuild(
64+
"test-annotation-path",
65+
"gradle/test-annotation-path",
66+
// The generated immutable class is compiled alongside the original
67+
// test source, so two SCIP shards are produced.
68+
expectedScipFiles = 2,
69+
)
70+
)
5871
add(
5972
checkGradleBuild("build-with-Werror", "gradle/build-with-Werror", expectedScipFiles = 2)
6073
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
id 'java'
3+
}
4+
repositories {
5+
mavenCentral()
6+
}
7+
dependencies {
8+
testCompileOnly 'org.immutables:value:2.9.2'
9+
testAnnotationProcessor 'org.immutables:value:2.9.2'
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test;
2+
import org.immutables.value.Value;
3+
import java.util.Optional;
4+
@Value.Immutable
5+
public abstract class WorkflowOptions {
6+
public abstract Optional<String> getWorkflowIdReusePolicy();
7+
}

0 commit comments

Comments
 (0)