Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions .github/workflows/pr-auditor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
9 changes: 6 additions & 3 deletions .github/workflows/sourcegraph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
),
Comment on lines +41 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On JDK 17+ pass the required parameters to JVM that launches javac.

Option(
allDependencies.in(p) +=
"com.sourcegraph" % "semanticdb-javac" % semanticdbJavacVersion
Expand All @@ -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 <-
Expand Down Expand Up @@ -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 <-
Expand Down Expand Up @@ -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")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, but forking javac may cause some builds to fail. Not a blocking comment, just good to keep an eye for it. If we can't turn on forking then we could try to automatically add the exports to SBT_OPTS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on what might be causing issues?

Perhaps I could add an extra test for this just to be sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember any concrete examples. Fine to try this out and just keep an eye out for regressions.

} else {
// If JDK is below 17, we don't actually need to
// fork the compiler, so we can keep javaHome empty
None
}
}
}
62 changes: 62 additions & 0 deletions src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always going to be quite brittle. Ideally, we use something like SystemJavaVersion from scip-java. Not a blocking comment, just an FYI if you don't want too maintain this parser forever https://sourcegraph.com/github.com/sourcegraph/scip-java/-/blob/scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/SystemJavaVersion.scala?L16

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and the entire PrintJavaVersion class is only 504 bytes - so I think in some other PR I'll just base64 that and include as resources:

yv66vgAAAD0AIQoAAgADBwAEDAAFAAYBABBqYXZhL2xhbmcvT2JqZWN0AQAGPGluaXQ+AQADKClWCQAIAAkHAAoMAAsADAEAEGphdmEvbGFuZy9TeXN0ZW0BAANvdXQBABVMamF2YS9pby9QcmludFN0cmVhbTsIAA4BAAxqYXZhLnZlcnNpb24KAAgAEAwAEQASAQALZ2V0UHJvcGVydHkBACYoTGphdmEvbGFuZy9TdHJpbmc7KUxqYXZhL2xhbmcvU3RyaW5nOwoAFAAVBwAWDAAXABgBABNqYXZhL2lvL1ByaW50U3RyZWFtAQAFcHJpbnQBABUoTGphdmEvbGFuZy9TdHJpbmc7KVYHABoBABBQcmludEphdmFWZXJzaW9uAQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEABG1haW4BABYoW0xqYXZhL2xhbmcvU3RyaW5nOylWAQAKU291cmNlRmlsZQEAFVByaW50SmF2YVZlcnNpb24uamF2YQAhABkAAgAAAAAAAgABAAUABgABABsAAAAdAAEAAQAAAAUqtwABsQAAAAEAHAAAAAYAAQAAAAEACQAdAB4AAQAbAAAAKAACAAEAAAAMsgAHEg24AA+2ABOxAAAAAQAcAAAACgACAAAAAwALAAQAAQAfAAAAAgAg

So that I can shell out to java command and just run this class. Thanks for the hint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good.

.!!(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
Expand Down
15 changes: 15 additions & 0 deletions src/sbt-test/sbt-sourcegraph/basic/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -32,6 +46,7 @@ commands += Command.command("checkLsif") { s =>
.filterNot(_ == ".")
.distinct
.sorted
.toList
if (
packageNames != List(
"jdk",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.2
sbt.version=1.9.3
2 changes: 0 additions & 2 deletions src/sbt-test/sbt-sourcegraph/scala-3/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
> sourcegraphEnable
> show semanticdbEnabled
> show Compile/semanticdbEnabled
> sourcegraphCompile