From 5934bb3429a14f9c52687d201f938875182bd2db Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 1 Aug 2024 12:31:03 +0100 Subject: [PATCH 1/5] Add test reproducing the issue --- .../test/scala/tests/ScipBuildToolSuite.scala | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala b/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala index ae9db2b5..d05edd4c 100644 --- a/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala +++ b/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala @@ -110,6 +110,23 @@ class ScipBuildToolSuite extends BaseBuildToolSuite { ) ) + checkBuild( + "jvm-args", + """|/lsif-java.json + |{"dependencies": ["junit:junit:4.13.1"], "javacOptions": ["-J-add-exports java.base/sun.util=ALL-UNNAMED"]} + |/foo/Example.java + |package foo; + |import org.junit.Assert; + |import sun.util.BuddhistCalendar; + |public class Example { + | public static void hello() { + | BuddhistCalendar calendar = new BuddhistCalendar(); + | } + |} + |""".stripMargin, + expectedSemanticdbFiles = 2 + ) + checkBuild( "basic", """|/lsif-java.json From a6214a55a6e8782335d60beb30f1589048cde8f7 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 1 Aug 2024 13:49:38 +0100 Subject: [PATCH 2/5] add jvmOptions to scip-java.json config and pass it to javac javac cannot extract -J flags from the argfile because these flags need to be passed to the launcher, which is already running by the time the argfile comes into play. See https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#command-line-argument-files for reference. --- .../sourcegraph/scip_java/buildtools/ScipBuildTool.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala index 0719f73d..7d4601d1 100644 --- a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala +++ b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala @@ -558,8 +558,11 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) { BuildInfo.javacModuleOptions else Nil + + val jvmOptions = config.jvmOptions.map("-J" + _) + val result = os - .proc(javac.toString, s"@$argsfile", javacModuleOptions) + .proc(javac.toString, s"@$argsfile", javacModuleOptions, jvmOptions) .call( stdout = pipe, stderr = pipe, @@ -815,6 +818,7 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) { processorpath: List[String] = Nil, processors: List[String] = Nil, javacOptions: List[String] = Nil, + jvmOptions: List[String] = Nil, jvm: String = "17", kind: String = "" ) From fc853cc7c60bfe38ffc20ca7ef8a0b2403f2a6d1 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 1 Aug 2024 14:25:46 +0100 Subject: [PATCH 3/5] Make test more representative --- .../scip_java/buildtools/ScipBuildTool.scala | 2 +- .../scip_java/commands/IndexCommand.scala | 6 +++ .../test/scala/tests/ScipBuildToolSuite.scala | 54 ++++++++++++++----- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala index 7d4601d1..473f2a42 100644 --- a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala +++ b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala @@ -202,7 +202,7 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) { } val isSemanticdbGenerated = Files .isDirectory(targetroot.resolve("META-INF")) - if (errors.nonEmpty && !isSemanticdbGenerated) { + if (errors.nonEmpty && (index.strictCompilation || !isSemanticdbGenerated)) { errors.foreach { error => index.app.reporter.log(Diagnostic.exception(error)) } diff --git a/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala b/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala index 770754a6..d29a2d02 100644 --- a/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala +++ b/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala @@ -83,6 +83,12 @@ case class IndexCommand( "Defaults to a build-specific command. For example, the default command for Maven command is 'clean verify -DskipTests'." + "To override the default, pass in the build command after a double dash: 'scip-java index -- compile test:compile'" ) + + @Hidden + @Description( + "Fail command invocation if compiler produces any errors" + ) strictCompilation: Boolean = false, + @TrailingArguments() buildCommand: List[String] = Nil, @Hidden indexSemanticdb: IndexSemanticdbCommand = IndexSemanticdbCommand(), diff --git a/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala b/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala index d05edd4c..01b37f83 100644 --- a/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala +++ b/tests/buildTools/src/test/scala/tests/ScipBuildToolSuite.scala @@ -111,20 +111,46 @@ class ScipBuildToolSuite extends BaseBuildToolSuite { ) checkBuild( - "jvm-args", - """|/lsif-java.json - |{"dependencies": ["junit:junit:4.13.1"], "javacOptions": ["-J-add-exports java.base/sun.util=ALL-UNNAMED"]} - |/foo/Example.java - |package foo; - |import org.junit.Assert; - |import sun.util.BuddhistCalendar; - |public class Example { - | public static void hello() { - | BuddhistCalendar calendar = new BuddhistCalendar(); - | } - |} - |""".stripMargin, - expectedSemanticdbFiles = 2 + "jvm-args", { + // In this test we verify that JVM args and Javac options are passed + // correctly. + // Lombok modules need to be passed with -J prefix, and javacOptions should + // be passed unchanged + // For this test to work the lombok version HAS to be relatively old, + // so that it requires all these opens. + // The list is taken from here: https://github.com/projectlombok/lombok/issues/2681#issuecomment-748616687 + val lombokModules = """ + --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED + --add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED + """.trim.split("\n").map(_.trim).mkString("\"", "\", \"", "\"") + + s"""|/lsif-java.json + |{"jvmOptions": [$lombokModules], "javacOptions": ["--add-exports=java.base/sun.util=ALL-UNNAMED"], "dependencies": ["org.projectlombok:lombok:1.18.16"]} + |/foo/Example.java + |package foo; + |import sun.util.BuddhistCalendar; + |public class Example extends BuddhistCalendar { + | public static void hello() { + | BuddhistCalendar calendar = new BuddhistCalendar(); + | } + |} + |""".stripMargin + }, + expectedSemanticdbFiles = 1, + // somehow it seems the actual compilation error from javac + // does not stop semanticdb-javac from producing the file. + // we explicitly disable this lenient mode so that if there + // are any compilation errors, it will be reflected in failed + // CLI command. + extraArguments = List("--strict-compilation") ) checkBuild( From d9b8e7a02b91f5f65c520143ce80104a57a3c8ee Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 1 Aug 2024 14:28:51 +0100 Subject: [PATCH 4/5] chore: formatting --- .../com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala | 4 +++- .../com/sourcegraph/scip_java/commands/IndexCommand.scala | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala index 473f2a42..d0477aa8 100644 --- a/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala +++ b/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/ScipBuildTool.scala @@ -202,7 +202,9 @@ class ScipBuildTool(index: IndexCommand) extends BuildTool("SCIP", index) { } val isSemanticdbGenerated = Files .isDirectory(targetroot.resolve("META-INF")) - if (errors.nonEmpty && (index.strictCompilation || !isSemanticdbGenerated)) { + if ( + errors.nonEmpty && (index.strictCompilation || !isSemanticdbGenerated) + ) { errors.foreach { error => index.app.reporter.log(Diagnostic.exception(error)) } diff --git a/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala b/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala index d29a2d02..1ae41c71 100644 --- a/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala +++ b/scip-java/src/main/scala/com/sourcegraph/scip_java/commands/IndexCommand.scala @@ -88,7 +88,6 @@ case class IndexCommand( @Description( "Fail command invocation if compiler produces any errors" ) strictCompilation: Boolean = false, - @TrailingArguments() buildCommand: List[String] = Nil, @Hidden indexSemanticdb: IndexSemanticdbCommand = IndexSemanticdbCommand(), From 9c920a20fe3df325f61b1cdb4f9ed83015675c70 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 1 Aug 2024 16:08:25 +0100 Subject: [PATCH 5/5] Update bazel aspect --- scip-java/src/main/resources/scip-java/scip_java.bzl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scip-java/src/main/resources/scip-java/scip_java.bzl b/scip-java/src/main/resources/scip-java/scip_java.bzl index 1bc2bf1e..948ee683 100644 --- a/scip-java/src/main/resources/scip-java/scip_java.bzl +++ b/scip-java/src/main/resources/scip-java/scip_java.bzl @@ -71,11 +71,20 @@ def _scip_java(target, ctx): processorpath += [j.path for j in annotations.processor_classpath.to_list()] processors = annotations.processor_classnames + launcher_javac_flags = [] + compiler_javac_flags = [] + for value in compilation.javac_options: + if value.startswith("-J"): + launcher_javac_flags.append(value) + else: + compiler_javac_flags.append(value) + build_config = struct(**{ "javaHome": ctx.var["java_home"], "classpath": classpath, "sourceFiles": source_files, - "javacOptions": compilation.javac_options, + "javacOptions": compiler_javac_flags, + "jvmOptions": launcher_javac_flags, "processors": processors, "processorpath": processorpath, "bootclasspath": bootclasspath,