diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e5d4cf..a99513f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,13 +7,30 @@ on: jobs: test: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + java: [8, 11, 17] steps: - - uses: actions/checkout@v2 - - uses: olafurpg/setup-scala@v13 - - run: sbt scripted +test + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: "temurin" + cache: "sbt" + java-version: ${{ matrix.java }} + - run: | + ls $JAVA_HOME + sbt scripted +test + shell: bash + check: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: olafurpg/setup-scala@v13 + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: "temurin" + cache: "sbt" + java-version: 17 - run: sbt checkAll diff --git a/.github/workflows/pr-auditor.yml b/.github/workflows/pr-auditor.yml index bdc06f3..3fe4223 100644 --- a/.github/workflows/pr-auditor.yml +++ b/.github/workflows/pr-auditor.yml @@ -8,14 +8,13 @@ jobs: check-pr: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - with: - repository: 'sourcegraph/pr-auditor' - - uses: actions/setup-go@v4 - with: { go-version: '1.20' } + - uses: actions/checkout@v2 + with: { repository: 'sourcegraph/sourcegraph' } + - uses: actions/setup-go@v2 + with: { go-version: '1.18' } - - run: './check-pr.sh' + - run: ./dev/pr-auditor/check-pr.sh env: GITHUB_EVENT_PATH: ${{ env.GITHUB_EVENT_PATH }} - GITHUB_TOKEN: ${{ github.token }} + GITHUB_TOKEN: ${{ secrets.CODENOTIFY_GITHUB_TOKEN }} GITHUB_RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} diff --git a/.github/workflows/sourcegraph.yml b/.github/workflows/sourcegraph.yml index 6c6c626..170bc58 100644 --- a/.github/workflows/sourcegraph.yml +++ b/.github/workflows/sourcegraph.yml @@ -10,11 +10,14 @@ jobs: name: "Upload LSIF" steps: - uses: actions/checkout@v2 - - uses: coursier/setup-action@v1.1.2 + - uses: actions/setup-java@v3 with: - jvm: adopt:8 + distribution: "temurin" + cache: "sbt" + java-version: 17 - run: | - cs launch com.sourcegraph:scip-java_2.13:latest.stable -M com.sourcegraph.scip_java.ScipJava -- index + curl -fL "https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz" | gzip -d > cs && chmod +x cs + ./cs launch com.sourcegraph:scip-java_2.13:latest.stable -M com.sourcegraph.scip_java.ScipJava -- index - run: yarn global add @sourcegraph/src - run: | src code-intel upload "-commit=${GITHUB_SHA}" "-github-token=${GITHUB_TOKEN}" diff --git a/src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala b/src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala index 19382d9..96843a7 100644 --- a/src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala +++ b/src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala @@ -31,12 +31,20 @@ object SourcegraphEnable { ) val semanticdbJavacVersion = Versions.semanticdbJavacVersion() + val settings = for { (p, semanticdbVersion, overriddenScalaVersion) <- collectProjects( extracted ) enableSemanticdbPlugin = List( + Option( + javacOptions.in(p) ++= { + if (Versions.isJavaAtLeast(17, home = javaHome.in(p).value)) + javacModuleOptions + else Nil + } + ), Option( allDependencies.in(p) += "com.sourcegraph" % "semanticdb-javac" % semanticdbJavacVersion @@ -51,6 +59,11 @@ object SourcegraphEnable { Option(SemanticdbPlugin.semanticdbEnabled.in(p) := true), semanticdbVersion.map(ver => SemanticdbPlugin.semanticdbVersion.in(p) := ver + ), + Option( + javaHome.in(p) := { + javaHome.in(p).value orElse calculateJavaHome + } ) ).flatten settings <- @@ -101,6 +114,14 @@ object SourcegraphEnable { ) ) }.toSeq + ), + Option( + javacOptions.in(p) ++= { + if (Versions.isJavaAtLeast(17)) javacModuleOptions else Nil + } + ), + Option( + javaHome.in(p) := javaHome.in(p).value orElse calculateJavaHome ) ).flatten settings <- @@ -143,4 +164,34 @@ object SourcegraphEnable { semanticdbVersion = Versions .semanticdbVersion(overriddenScalaVersion.getOrElse(projectScalaVersion)) } yield (p, semanticdbVersion, overriddenScalaVersion) + + def javacModuleOptions: List[String] = + List( + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", + "-J--add-exports", + "-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" + ) + + private def calculateJavaHome = { + // We can safely use java.home property + // on JDK 17+ as it won't be pointing to JRE which + // doesn't contain a compiler. + if (Versions.isJavaAtLeast(17)) { + // On JDK 17+ we need to explicitly fork the compiler + // so that we can set the necessary JVM options to access + // jdk.compiler module + Some(new File(System.getProperty("java.home"))) + } else { + // If JDK is below 17, we don't actually need to + // fork the compiler, so we can keep javaHome empty + None + } + } } diff --git a/src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala b/src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala index 08c107f..db4f1ca 100644 --- a/src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala +++ b/src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala @@ -5,6 +5,7 @@ import java.nio.file.Paths import java.util.Properties import scala.collection.JavaConverters._ import scala.sys.process._ +import java.io.File object Versions { def scalametaVersion = "4.4.26" @@ -65,6 +66,67 @@ object Versions { .updated(semanticdbJavacKey, semanticdbJavacVersions.last) } + private val jvmVersionCache = collection.mutable.Map.empty[Option[File], Int] + + def isJavaAtLeast(n: Int, home: Option[File] = None): Boolean = { + + val significant = jvmVersionCache.getOrElseUpdate( + home, { + val raw = + home match { + case None => + System.getProperty("java.version") + case Some(javaHome) => + val sb = new StringBuilder + val proc = { + val cmd = + if (scala.util.Properties.isWin) + Paths.get("bin", "java") + else Paths.get("bin", "java") + + scala.sys.process + .Process(Seq(cmd.toString(), "-version"), cwd = javaHome) + .!!(ProcessLogger(sb.append(_))) + + sb.result().trim + } + + val rgx = "version \"(.*?)\"".r + + rgx.findFirstMatchIn( + proc.linesIterator.take(1).mkString("") + ) match { + case None => + sys.error( + s"Cannot process [java -version] output (in $javaHome): [$proc]" + ) + case Some(value) => + value.group(1) + } + } + + val prop = raw.takeWhile(c => c.isDigit || c == '.') + + val segments = prop.split("\\.").toList + + segments match { + // Java 1.6 - 1.8 + case "1" :: lessThan8 :: _ :: Nil => lessThan8.toInt + // Java 17.0.1, .. + case modern :: _ :: _ :: Nil => modern.toInt + // Java 12 + case modern :: Nil => modern.toInt + case other => + sys.error( + s"Cannot process java.home property, unknown format: [$raw]" + ) + } + } + ) + + significant >= n + } + private def proc(cmd: String*): List[String] = { println(cmd.updated(0, "coursier").mkString("$ ", " ", "")) cmd.!!.linesIterator.toList diff --git a/src/sbt-test/sbt-sourcegraph/basic/build.sbt b/src/sbt-test/sbt-sourcegraph/basic/build.sbt index 96243bf..d847b2e 100644 --- a/src/sbt-test/sbt-sourcegraph/basic/build.sbt +++ b/src/sbt-test/sbt-sourcegraph/basic/build.sbt @@ -17,6 +17,20 @@ lazy val a = project lazy val b = project .dependsOn(a) + .settings( + // Test to ensure the plugin works with explicitly set java home + // On Java 8 the java.home property returns JRE path, not JDK path. + // so we try and work around it hoping that JAVA_HOME is set by executing + // environment + javaHome := { + println(sys.env.get("JAVA_HOME")) + Some( + new File( + sys.env.getOrElse("JAVA_HOME", System.getProperty("java.home")) + ) + ) + } + ) commands += Command.command("checkLsif") { s => val dumpPath = @@ -32,6 +46,7 @@ commands += Command.command("checkLsif") { s => .filterNot(_ == ".") .distinct .sorted + .toList if ( packageNames != List( "jdk", diff --git a/src/sbt-test/sbt-sourcegraph/basic/project/build.properties b/src/sbt-test/sbt-sourcegraph/basic/project/build.properties index 19479ba..52413ab 100644 --- a/src/sbt-test/sbt-sourcegraph/basic/project/build.properties +++ b/src/sbt-test/sbt-sourcegraph/basic/project/build.properties @@ -1 +1 @@ -sbt.version=1.5.2 +sbt.version=1.9.3 diff --git a/src/sbt-test/sbt-sourcegraph/scala-3/test b/src/sbt-test/sbt-sourcegraph/scala-3/test index d318c67..a60e80e 100644 --- a/src/sbt-test/sbt-sourcegraph/scala-3/test +++ b/src/sbt-test/sbt-sourcegraph/scala-3/test @@ -1,4 +1,2 @@ > sourcegraphEnable -> show semanticdbEnabled -> show Compile/semanticdbEnabled > sourcegraphCompile