Skip to content

Commit 30378f1

Browse files
authored
Unconditionally add agent in Gradle plugin, add protobuf codegen test (#630)
Also: * Add a protobuf generator testcase to gradle auto-indexing
1 parent 0fe40e9 commit 30378f1

File tree

3 files changed

+110
-53
lines changed

3 files changed

+110
-53
lines changed

semanticdb-agent/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbAgent.java

+46-38
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public class SemanticdbAgent {
2424

2525
public static void premain(String agentArgs, Instrumentation inst) {
2626
// NOTE(olafur): Uncoment below if you want see all the loaded classes.
27-
// PrintStream logger = newLogger();
28-
// inst.addTransformer(
29-
// new ClassFileTransformer() {
30-
// @Override
31-
// public byte[] transform(
32-
// ClassLoader loader,
33-
// String className,
34-
// Class<?> classBeingRedefined,
35-
// ProtectionDomain protectionDomain,
36-
// byte[] classfileBuffer) {
37-
// logger.println(className);
38-
// return classfileBuffer;
39-
// }
40-
// });
27+
// PrintStream logger = newLogger();
28+
// inst.addTransformer(
29+
// new ClassFileTransformer() {
30+
// @Override
31+
// public byte[] transform(
32+
// ClassLoader loader,
33+
// String className,
34+
// Class<?> classBeingRedefined,
35+
// ProtectionDomain protectionDomain,
36+
// byte[] classfileBuffer) {
37+
// logger.println(className);
38+
// return classfileBuffer;
39+
// }
40+
// });
4141
new AgentBuilder.Default()
4242
.disableClassFormatChanges()
4343
.type(
@@ -156,6 +156,7 @@ public static void build(
156156
}
157157

158158
boolean isProcessorpathUpdated = false;
159+
boolean semanticdbAlreadyAdded = false;
159160
String previousOption = "";
160161

161162
ArrayList<String> newOptions = new ArrayList<>();
@@ -172,6 +173,10 @@ public static void build(
172173
case "-Xlint":
173174
break;
174175
default:
176+
if (option.startsWith("-Xplugin:semanticdb")) {
177+
semanticdbAlreadyAdded = true;
178+
break;
179+
}
175180
if (option.startsWith("-Xplugin:ErrorProne")) {
176181
break;
177182
}
@@ -180,33 +185,36 @@ public static void build(
180185
}
181186
previousOption = option;
182187
}
183-
if (!isProcessorpathUpdated) {
184-
newOptions.add("-classpath");
185-
newOptions.add(PLUGINPATH);
186-
}
187-
newOptions.add(
188-
String.format(
189-
"-Xplugin:semanticdb -sourceroot:%s -targetroot:%s", SOURCEROOT, TARGETROOT));
190-
191-
if (DEBUGPATH != null) {
192-
ArrayList<String> debuglines = new ArrayList<>();
193-
debuglines.add("============== Java Home: " + System.getProperty("java.home"));
194-
debuglines.add("============== Old Options");
195-
debuglines.addAll(arguments);
196-
debuglines.add("============== New Options");
197-
debuglines.addAll(newOptions);
188+
if (!semanticdbAlreadyAdded) {
198189

199-
try {
200-
Files.write(
201-
Paths.get(DEBUGPATH),
202-
debuglines,
203-
StandardOpenOption.CREATE,
204-
StandardOpenOption.APPEND);
205-
} catch (IOException e) {
190+
if (!isProcessorpathUpdated) {
191+
newOptions.add("-classpath");
192+
newOptions.add(PLUGINPATH);
206193
}
207-
}
194+
newOptions.add(
195+
String.format(
196+
"-Xplugin:semanticdb -sourceroot:%s -targetroot:%s", SOURCEROOT, TARGETROOT));
197+
198+
if (DEBUGPATH != null) {
199+
ArrayList<String> debuglines = new ArrayList<>();
200+
debuglines.add("============== Java Home: " + System.getProperty("java.home"));
201+
debuglines.add("============== Old Options");
202+
debuglines.addAll(arguments);
203+
debuglines.add("============== New Options");
204+
debuglines.addAll(newOptions);
208205

209-
arguments = newOptions;
206+
try {
207+
Files.write(
208+
Paths.get(DEBUGPATH),
209+
debuglines,
210+
StandardOpenOption.CREATE,
211+
StandardOpenOption.APPEND);
212+
} catch (IOException e) {
213+
}
214+
}
215+
216+
arguments = newOptions;
217+
}
210218
}
211219
}
212220
}

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

+26-15
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
154154
task.getOptions().setFork(true)
155155
task.getOptions().setIncremental(false)
156156

157-
if (compilerPluginAdded)
157+
if (compilerPluginAdded) {
158158
task
159159
.getOptions()
160160
.getCompilerArgs()
@@ -169,21 +169,32 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
169169
s"-Xplugin:semanticdb -targetroot:$targetRoot -sourceroot:$sourceRoot"
170170
).asJava
171171
)
172-
else
173-
agentJar.foreach { agentpath =>
174-
javacPluginJar.foreach { pluginpath =>
175-
val jvmArgs = task.getOptions.getForkOptions.getJvmArgs
176-
177-
jvmArgs.addAll(
178-
List(
179-
s"-javaagent:$agentpath",
180-
s"-Dsemanticdb.pluginpath=$pluginpath",
181-
s"-Dsemanticdb.sourceroot=$sourceRoot",
182-
s"-Dsemanticdb.targetroot=$targetRoot"
183-
).asJava
184-
)
185-
}
172+
}
173+
174+
/**
175+
* In some yet to be understood cases we see that compiler plugin
176+
* can be added successfully, but the correct flags are still not
177+
* propagated.
178+
*
179+
* To work around it, we enable the agent unconditionally, and then
180+
* if necessary deduplicate the arguments.
181+
*
182+
* TODO: figure out why this is necessary
183+
*/
184+
agentJar.foreach { agentpath =>
185+
javacPluginJar.foreach { pluginpath =>
186+
val jvmArgs = task.getOptions.getForkOptions.getJvmArgs
187+
188+
jvmArgs.addAll(
189+
List(
190+
s"-javaagent:$agentpath",
191+
s"-Dsemanticdb.pluginpath=$pluginpath",
192+
s"-Dsemanticdb.sourceroot=$sourceRoot",
193+
s"-Dsemanticdb.targetroot=$targetRoot"
194+
).asJava
195+
)
186196
}
197+
}
187198

188199
}
189200
}

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

+38
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,44 @@ abstract class GradleBuildToolSuite(allGradle: List[String])
157157
)
158158
}
159159

160+
checkGradleBuild(
161+
"protobuf-generator",
162+
"""|/build.gradle
163+
|plugins {
164+
| id "java"
165+
| id "com.google.protobuf" version "0.9.4"
166+
|}
167+
|dependencies {
168+
| implementation 'com.google.protobuf:protobuf-javalite:3.8.0'
169+
|}
170+
|protobuf {
171+
| protoc {
172+
| artifact = 'com.google.protobuf:protoc:3.23.4'
173+
| }
174+
| generateProtoTasks {
175+
| all().configureEach { task ->
176+
| task.builtins {
177+
| java {
178+
| option "lite"
179+
| }
180+
| }
181+
| }
182+
| }
183+
|}
184+
|/src/main/proto/message.proto
185+
|syntax = "proto3";
186+
|message SearchRequest {
187+
| string query = 1;
188+
| int32 page_number = 2;
189+
| int32 results_per_page = 3;
190+
|}
191+
|/src/main/java/Example.java
192+
|public class Example {}
193+
|""".stripMargin,
194+
expectedSemanticdbFiles = 2,
195+
gradleVersions = List(Gradle8, Gradle7, Gradle67)
196+
)
197+
160198
checkGradleBuild(
161199
"explicit",
162200
"""|/build.gradle

0 commit comments

Comments
 (0)