Skip to content

Commit 7749657

Browse files
authored
Fix Gradle builds with -Werror (#670)
The warning in question is only triggered if there are annotation processors in the build
1 parent 29aa9bd commit 7749657

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

build.sbt

+10-12
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,16 @@ lazy val minimizedSettings = List[Def.Setting[_]](
357357
(run / fork) := true,
358358
(Compile / unmanagedSourceDirectories) += minimizedSourceDirectory,
359359
libraryDependencies ++= List("org.projectlombok" % "lombok" % "1.18.22"),
360-
javacOptions ++=
361-
List[String](
362-
s"-Arandomtimestamp=${System.nanoTime()}",
363-
List(
364-
s"-Xplugin:semanticdb",
365-
s"-build-tool:sbt",
366-
s"-text:on",
367-
s"-verbose",
368-
s"-sourceroot:${(ThisBuild / baseDirectory).value}",
369-
s"-targetroot:${(Compile / semanticdbTargetRoot).value}"
370-
).mkString(" ")
371-
)
360+
javacOptions +=
361+
List(
362+
s"-Xplugin:semanticdb",
363+
s"-build-tool:sbt",
364+
s"-text:on",
365+
s"-verbose",
366+
s"-sourceroot:${(ThisBuild / baseDirectory).value}",
367+
s"-targetroot:${(Compile / semanticdbTargetRoot).value}",
368+
s"-randomtimestamp=${System.nanoTime()}"
369+
).mkString(" ")
372370
)
373371

374372
lazy val minimized = project

semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
165165
// TODO: before this plugin is published to Maven Central,
166166
// we will need to revert this change - as it can have detrimental
167167
// effect on people's builds
168-
s"-Arandomtimestamp=${System.currentTimeMillis()}",
169-
s"-Xplugin:semanticdb -targetroot:$targetRoot -sourceroot:$sourceRoot"
168+
s"-Xplugin:semanticdb -targetroot:$targetRoot -sourceroot:$sourceRoot -randomtimestamp=${System.nanoTime()}"
170169
).asJava
171170
)
172171
}

semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbJavacOptions.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public static SemanticdbJavacOptions parse(String[] args, JavacTask task) {
9797
result.verboseEnabled = true;
9898
} else if (arg.equals("-verbose:off")) {
9999
result.verboseEnabled = false;
100+
} else if (arg.startsWith("-randomtimestamp")) {
100101
} else {
101102
result.errors.add(String.format("unknown flag '%s'\n", arg));
102103
}
@@ -126,8 +127,10 @@ private static boolean isSourcerootDefined(SemanticdbJavacOptions options) {
126127
// warning - use of internal API
127128
// requires --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
128129
private static TargetPaths getJavacClassesDir(SemanticdbJavacOptions result, JavacTask task) {
129-
// both Context and BasicJavacTask are internal JDK classes so not exported under >= JDK 17
130-
// com.sun.tools.javac.util.Context ctx = ((com.sun.tools.javac.api.BasicJavacTask)
130+
// both Context and BasicJavacTask are internal JDK classes so not exported
131+
// under >= JDK 17
132+
// com.sun.tools.javac.util.Context ctx =
133+
// ((com.sun.tools.javac.api.BasicJavacTask)
131134
// task).getContext();
132135
// I'm not aware of a better way to get the class output directory from javac
133136
Path classOutputDir = null;

tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala

+32
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ abstract class GradleBuildToolSuite(allGradle: List[String])
5050
gradleVersions = List(Gradle8, Gradle7, Gradle67)
5151
)
5252

53+
checkGradleBuild(
54+
"build-with-Werror",
55+
"""|/build.gradle
56+
|plugins {
57+
| id 'java-library'
58+
|}
59+
|repositories {
60+
| // Use Maven Central for resolving dependencies.
61+
| mavenCentral()
62+
|}
63+
|dependencies {
64+
| compileOnly 'org.immutables:value:2.9.2'
65+
| annotationProcessor 'org.immutables:value:2.9.2'
66+
|}
67+
|compileJava {
68+
| options.compilerArgs << "-Werror"
69+
|}
70+
|/src/main/java/main/bla/ExampleClass.java
71+
|package test;
72+
|import org.immutables.value.Value;
73+
|import java.util.Optional;
74+
|@Value.Immutable
75+
|public abstract class ExampleClass {
76+
| public abstract Optional<String> getWorkflowIdReusePolicy();
77+
|}
78+
""".stripMargin,
79+
// See comment about immutable annotation processor above,
80+
// it explains why we expecte 2 semanticdb files
81+
expectedSemanticdbFiles = 2,
82+
gradleVersions = List(Gradle8, Gradle7, Gradle67)
83+
)
84+
5385
checkGradleBuild(
5486
"publishing",
5587
"""|/build.gradle

0 commit comments

Comments
 (0)