Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sourcegraph/scip-java
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.9.1
Choose a base ref
...
head repository: sourcegraph/scip-java
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.9.2
Choose a head ref
  • 2 commits
  • 9 files changed
  • 1 contributor

Commits on Aug 8, 2023

  1. Unconditionally add agent in Gradle plugin, add protobuf codegen test (

    …#630)
    
    Also: 
    * Add a protobuf generator testcase to gradle auto-indexing
    keynmol authored Aug 8, 2023
    Copy the full SHA
    30378f1 View commit details
  2. Copy the full SHA
    e75a04c View commit details
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -250,6 +250,7 @@ lazy val cli = project
libraryDependencies ++=
List(
"io.get-coursier" %% "coursier" % V.coursier,
"io.get-coursier" %% "coursier-jvm" % V.coursier,
"org.scalameta" % "mtags-interfaces" % V.metals,
"org.scala-lang.modules" %% "scala-xml" % V.scalaXml,
"com.lihaoyi" %% "requests" % V.requests,
Binary file removed scip-java/src/main/resources/scip-java/coursier
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -18,11 +18,6 @@ object Embedded {
copyFile(tmpDir, "gradle-plugin.jar")

def agentJar(tmpDir: Path): Path = copyFile(tmpDir, "semanticdb-agent.jar")
def coursier(tmpDir: Path): Path = {
val result = copyFile(tmpDir, "scip-java/coursier")
result.toFile().setExecutable(true)
result
}

private def javacErrorpath(tmp: Path) = tmp.resolve("errorpath.txt")

Original file line number Diff line number Diff line change
@@ -22,6 +22,9 @@ import java.util.jar.JarFile

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ListBuffer
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration
import scala.jdk.CollectionConverters._
import scala.language.postfixOps
import scala.util.Failure
@@ -40,6 +43,7 @@ import com.sourcegraph.scip_java.Embedded
import com.sourcegraph.scip_java.commands.IndexCommand
import com.sourcegraph.semanticdb_javac.Semanticdb.TextDocument
import com.sourcegraph.semanticdb_javac.Semanticdb.TextDocuments
import coursier.jvm.JvmIndex
import moped.json.DecodingContext
import moped.json.ErrorResult
import moped.json.JsonCodec
@@ -606,30 +610,62 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) {
}

private def javacPathViaCoursier(jvmVersion: String, tmp: Path): Path = {
val coursier = Embedded.coursier(tmp)
Paths.get(
os.proc(
coursier.toString,
"java-home",
"--jvm",
jvmVersion,
"--architecture",
jvmArchitecture
import _root_.coursier.jvm._

val jvmChannel = index
.app
.env
.environmentVariables
.get("COURSIER_JVM_INDEX")
.map { idx =>
JvmChannel
.parse(idx)
.fold(
msg =>
throw new RuntimeException(
s"Malformed COURSIER_JVM_INDEX environment variable variable: $msg"
),
identity
)
}

val home = JavaHome().withCache(
JvmCache()
.withIndexChannel(
repositories = Seq.empty,
indexChannel = jvmChannel
.getOrElse(JvmChannel.url(JvmIndex.defaultIndexUrl))
)
.call()
.out
.trim(),
"bin",
"javac"
.withArchitecture(jvmArchitecture(jvmVersion))
)

val javaExecutable = Await.result(
home.javaBin(jvmVersion).value(ExecutionContext.global),
Duration.Inf
)

javaExecutable
.getParent()
.resolve {
if (scala.util.Properties.isWin)
"javac.exe"
else
"javac"
}

}

private def jvmArchitecture: String =
if (scala.util.Properties.isMac && sys.props("os.arch") == "aarch64")
private def jvmArchitecture(jvm: String): String =
if (
// Hack only for local development on ARM64 Mac OS X
// won't affect any other system
scala.util.Properties.isMac && sys.props("os.arch") == "aarch64" &&
(jvm.startsWith("8") || jvm.startsWith("1.8"))
)
"amd64"
else
defaultCoursierJVMArchitecture
JvmIndex.defaultArchitecture()

def defaultCoursierJVMArchitecture: String =
sys.props("os.arch") match {
case "x86_64" =>
Original file line number Diff line number Diff line change
@@ -24,20 +24,20 @@ public class SemanticdbAgent {

public static void premain(String agentArgs, Instrumentation inst) {
// NOTE(olafur): Uncoment below if you want see all the loaded classes.
// PrintStream logger = newLogger();
// inst.addTransformer(
// new ClassFileTransformer() {
// @Override
// public byte[] transform(
// ClassLoader loader,
// String className,
// Class<?> classBeingRedefined,
// ProtectionDomain protectionDomain,
// byte[] classfileBuffer) {
// logger.println(className);
// return classfileBuffer;
// }
// });
// PrintStream logger = newLogger();
// inst.addTransformer(
// new ClassFileTransformer() {
// @Override
// public byte[] transform(
// ClassLoader loader,
// String className,
// Class<?> classBeingRedefined,
// ProtectionDomain protectionDomain,
// byte[] classfileBuffer) {
// logger.println(className);
// return classfileBuffer;
// }
// });
new AgentBuilder.Default()
.disableClassFormatChanges()
.type(
@@ -156,6 +156,7 @@ public static void build(
}

boolean isProcessorpathUpdated = false;
boolean semanticdbAlreadyAdded = false;
String previousOption = "";

ArrayList<String> newOptions = new ArrayList<>();
@@ -172,6 +173,10 @@ public static void build(
case "-Xlint":
break;
default:
if (option.startsWith("-Xplugin:semanticdb")) {
semanticdbAlreadyAdded = true;
break;
}
if (option.startsWith("-Xplugin:ErrorProne")) {
break;
}
@@ -180,33 +185,36 @@ public static void build(
}
previousOption = option;
}
if (!isProcessorpathUpdated) {
newOptions.add("-classpath");
newOptions.add(PLUGINPATH);
}
newOptions.add(
String.format(
"-Xplugin:semanticdb -sourceroot:%s -targetroot:%s", SOURCEROOT, TARGETROOT));

if (DEBUGPATH != null) {
ArrayList<String> debuglines = new ArrayList<>();
debuglines.add("============== Java Home: " + System.getProperty("java.home"));
debuglines.add("============== Old Options");
debuglines.addAll(arguments);
debuglines.add("============== New Options");
debuglines.addAll(newOptions);
if (!semanticdbAlreadyAdded) {

try {
Files.write(
Paths.get(DEBUGPATH),
debuglines,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
} catch (IOException e) {
if (!isProcessorpathUpdated) {
newOptions.add("-classpath");
newOptions.add(PLUGINPATH);
}
}
newOptions.add(
String.format(
"-Xplugin:semanticdb -sourceroot:%s -targetroot:%s", SOURCEROOT, TARGETROOT));

if (DEBUGPATH != null) {
ArrayList<String> debuglines = new ArrayList<>();
debuglines.add("============== Java Home: " + System.getProperty("java.home"));
debuglines.add("============== Old Options");
debuglines.addAll(arguments);
debuglines.add("============== New Options");
debuglines.addAll(newOptions);

arguments = newOptions;
try {
Files.write(
Paths.get(DEBUGPATH),
debuglines,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
} catch (IOException e) {
}
}

arguments = newOptions;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
task.getOptions().setFork(true)
task.getOptions().setIncremental(false)

if (compilerPluginAdded)
if (compilerPluginAdded) {
task
.getOptions()
.getCompilerArgs()
@@ -169,21 +169,32 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
s"-Xplugin:semanticdb -targetroot:$targetRoot -sourceroot:$sourceRoot"
).asJava
)
else
agentJar.foreach { agentpath =>
javacPluginJar.foreach { pluginpath =>
val jvmArgs = task.getOptions.getForkOptions.getJvmArgs

jvmArgs.addAll(
List(
s"-javaagent:$agentpath",
s"-Dsemanticdb.pluginpath=$pluginpath",
s"-Dsemanticdb.sourceroot=$sourceRoot",
s"-Dsemanticdb.targetroot=$targetRoot"
).asJava
)
}
}

/**
* In some yet to be understood cases we see that compiler plugin
* can be added successfully, but the correct flags are still not
* propagated.
*
* To work around it, we enable the agent unconditionally, and then
* if necessary deduplicate the arguments.
*
* TODO: figure out why this is necessary
*/
agentJar.foreach { agentpath =>
javacPluginJar.foreach { pluginpath =>
val jvmArgs = task.getOptions.getForkOptions.getJvmArgs

jvmArgs.addAll(
List(
s"-javaagent:$agentpath",
s"-Dsemanticdb.pluginpath=$pluginpath",
s"-Dsemanticdb.sourceroot=$sourceRoot",
s"-Dsemanticdb.targetroot=$targetRoot"
).asJava
)
}
}

}
}
38 changes: 38 additions & 0 deletions tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala
Original file line number Diff line number Diff line change
@@ -157,6 +157,44 @@ abstract class GradleBuildToolSuite(allGradle: List[String])
)
}

checkGradleBuild(
"protobuf-generator",
"""|/build.gradle
|plugins {
| id "java"
| id "com.google.protobuf" version "0.9.4"
|}
|dependencies {
| implementation 'com.google.protobuf:protobuf-javalite:3.8.0'
|}
|protobuf {
| protoc {
| artifact = 'com.google.protobuf:protoc:3.23.4'
| }
| generateProtoTasks {
| all().configureEach { task ->
| task.builtins {
| java {
| option "lite"
| }
| }
| }
| }
|}
|/src/main/proto/message.proto
|syntax = "proto3";
|message SearchRequest {
| string query = 1;
| int32 page_number = 2;
| int32 results_per_page = 3;
|}
|/src/main/java/Example.java
|public class Example {}
|""".stripMargin,
expectedSemanticdbFiles = 2,
gradleVersions = List(Gradle8, Gradle7, Gradle67)
)

checkGradleBuild(
"explicit",
"""|/build.gradle
Loading