Skip to content

Commit f6e343b

Browse files
authored
Fix java 17 compilation (#77)
* Fix compilation of Java 17+ sources
1 parent 3b5938f commit f6e343b

File tree

8 files changed

+163
-18
lines changed

8 files changed

+163
-18
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@ on:
77
jobs:
88
test:
99
runs-on: ubuntu-latest
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
os: [ubuntu-latest]
14+
java: [8, 11, 17]
1015
steps:
11-
- uses: actions/checkout@v2
12-
- uses: olafurpg/setup-scala@v13
13-
- run: sbt scripted +test
16+
- uses: actions/checkout@v3
17+
- uses: actions/setup-java@v3
18+
with:
19+
distribution: "temurin"
20+
cache: "sbt"
21+
java-version: ${{ matrix.java }}
22+
- run: |
23+
ls $JAVA_HOME
24+
sbt scripted +test
25+
shell: bash
26+
1427
check:
1528
runs-on: ubuntu-latest
1629
steps:
17-
- uses: actions/checkout@v2
18-
- uses: olafurpg/setup-scala@v13
30+
- uses: actions/checkout@v3
31+
- uses: actions/setup-java@v3
32+
with:
33+
distribution: "temurin"
34+
cache: "sbt"
35+
java-version: 17
1936
- run: sbt checkAll

.github/workflows/pr-auditor.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ jobs:
88
check-pr:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v3
12-
with:
13-
repository: 'sourcegraph/pr-auditor'
14-
- uses: actions/setup-go@v4
15-
with: { go-version: '1.20' }
11+
- uses: actions/checkout@v2
12+
with: { repository: 'sourcegraph/sourcegraph' }
13+
- uses: actions/setup-go@v2
14+
with: { go-version: '1.18' }
1615

17-
- run: './check-pr.sh'
16+
- run: ./dev/pr-auditor/check-pr.sh
1817
env:
1918
GITHUB_EVENT_PATH: ${{ env.GITHUB_EVENT_PATH }}
20-
GITHUB_TOKEN: ${{ github.token }}
19+
GITHUB_TOKEN: ${{ secrets.CODENOTIFY_GITHUB_TOKEN }}
2120
GITHUB_RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

.github/workflows/sourcegraph.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ jobs:
1010
name: "Upload LSIF"
1111
steps:
1212
- uses: actions/checkout@v2
13-
- uses: coursier/setup-[email protected]
13+
- uses: actions/setup-java@v3
1414
with:
15-
jvm: adopt:8
15+
distribution: "temurin"
16+
cache: "sbt"
17+
java-version: 17
1618
- run: |
17-
cs launch com.sourcegraph:scip-java_2.13:latest.stable -M com.sourcegraph.scip_java.ScipJava -- index
19+
curl -fL "https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz" | gzip -d > cs && chmod +x cs
20+
./cs launch com.sourcegraph:scip-java_2.13:latest.stable -M com.sourcegraph.scip_java.ScipJava -- index
1821
- run: yarn global add @sourcegraph/src
1922
- run: |
2023
src code-intel upload "-commit=${GITHUB_SHA}" "-github-token=${GITHUB_TOKEN}"

src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ object SourcegraphEnable {
3131
)
3232

3333
val semanticdbJavacVersion = Versions.semanticdbJavacVersion()
34+
3435
val settings = for {
3536
(p, semanticdbVersion, overriddenScalaVersion) <- collectProjects(
3637
extracted
3738
)
3839
enableSemanticdbPlugin =
3940
List(
41+
Option(
42+
javacOptions.in(p) ++= {
43+
if (Versions.isJavaAtLeast(17, home = javaHome.in(p).value))
44+
javacModuleOptions
45+
else Nil
46+
}
47+
),
4048
Option(
4149
allDependencies.in(p) +=
4250
"com.sourcegraph" % "semanticdb-javac" % semanticdbJavacVersion
@@ -51,6 +59,11 @@ object SourcegraphEnable {
5159
Option(SemanticdbPlugin.semanticdbEnabled.in(p) := true),
5260
semanticdbVersion.map(ver =>
5361
SemanticdbPlugin.semanticdbVersion.in(p) := ver
62+
),
63+
Option(
64+
javaHome.in(p) := {
65+
javaHome.in(p).value orElse calculateJavaHome
66+
}
5467
)
5568
).flatten
5669
settings <-
@@ -101,6 +114,14 @@ object SourcegraphEnable {
101114
)
102115
)
103116
}.toSeq
117+
),
118+
Option(
119+
javacOptions.in(p) ++= {
120+
if (Versions.isJavaAtLeast(17)) javacModuleOptions else Nil
121+
}
122+
),
123+
Option(
124+
javaHome.in(p) := javaHome.in(p).value orElse calculateJavaHome
104125
)
105126
).flatten
106127
settings <-
@@ -143,4 +164,34 @@ object SourcegraphEnable {
143164
semanticdbVersion = Versions
144165
.semanticdbVersion(overriddenScalaVersion.getOrElse(projectScalaVersion))
145166
} yield (p, semanticdbVersion, overriddenScalaVersion)
167+
168+
def javacModuleOptions: List[String] =
169+
List(
170+
"-J--add-exports",
171+
"-Jjdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
172+
"-J--add-exports",
173+
"-Jjdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
174+
"-J--add-exports",
175+
"-Jjdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
176+
"-J--add-exports",
177+
"-Jjdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
178+
"-J--add-exports",
179+
"-Jjdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
180+
)
181+
182+
private def calculateJavaHome = {
183+
// We can safely use java.home property
184+
// on JDK 17+ as it won't be pointing to JRE which
185+
// doesn't contain a compiler.
186+
if (Versions.isJavaAtLeast(17)) {
187+
// On JDK 17+ we need to explicitly fork the compiler
188+
// so that we can set the necessary JVM options to access
189+
// jdk.compiler module
190+
Some(new File(System.getProperty("java.home")))
191+
} else {
192+
// If JDK is below 17, we don't actually need to
193+
// fork the compiler, so we can keep javaHome empty
194+
None
195+
}
196+
}
146197
}

src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.nio.file.Paths
55
import java.util.Properties
66
import scala.collection.JavaConverters._
77
import scala.sys.process._
8+
import java.io.File
89

910
object Versions {
1011
def scalametaVersion = "4.4.26"
@@ -65,6 +66,67 @@ object Versions {
6566
.updated(semanticdbJavacKey, semanticdbJavacVersions.last)
6667
}
6768

69+
private val jvmVersionCache = collection.mutable.Map.empty[Option[File], Int]
70+
71+
def isJavaAtLeast(n: Int, home: Option[File] = None): Boolean = {
72+
73+
val significant = jvmVersionCache.getOrElseUpdate(
74+
home, {
75+
val raw =
76+
home match {
77+
case None =>
78+
System.getProperty("java.version")
79+
case Some(javaHome) =>
80+
val sb = new StringBuilder
81+
val proc = {
82+
val cmd =
83+
if (scala.util.Properties.isWin)
84+
Paths.get("bin", "java")
85+
else Paths.get("bin", "java")
86+
87+
scala.sys.process
88+
.Process(Seq(cmd.toString(), "-version"), cwd = javaHome)
89+
.!!(ProcessLogger(sb.append(_)))
90+
91+
sb.result().trim
92+
}
93+
94+
val rgx = "version \"(.*?)\"".r
95+
96+
rgx.findFirstMatchIn(
97+
proc.linesIterator.take(1).mkString("")
98+
) match {
99+
case None =>
100+
sys.error(
101+
s"Cannot process [java -version] output (in $javaHome): [$proc]"
102+
)
103+
case Some(value) =>
104+
value.group(1)
105+
}
106+
}
107+
108+
val prop = raw.takeWhile(c => c.isDigit || c == '.')
109+
110+
val segments = prop.split("\\.").toList
111+
112+
segments match {
113+
// Java 1.6 - 1.8
114+
case "1" :: lessThan8 :: _ :: Nil => lessThan8.toInt
115+
// Java 17.0.1, ..
116+
case modern :: _ :: _ :: Nil => modern.toInt
117+
// Java 12
118+
case modern :: Nil => modern.toInt
119+
case other =>
120+
sys.error(
121+
s"Cannot process java.home property, unknown format: [$raw]"
122+
)
123+
}
124+
}
125+
)
126+
127+
significant >= n
128+
}
129+
68130
private def proc(cmd: String*): List[String] = {
69131
println(cmd.updated(0, "coursier").mkString("$ ", " ", ""))
70132
cmd.!!.linesIterator.toList

src/sbt-test/sbt-sourcegraph/basic/build.sbt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ lazy val a = project
1717

1818
lazy val b = project
1919
.dependsOn(a)
20+
.settings(
21+
// Test to ensure the plugin works with explicitly set java home
22+
// On Java 8 the java.home property returns JRE path, not JDK path.
23+
// so we try and work around it hoping that JAVA_HOME is set by executing
24+
// environment
25+
javaHome := {
26+
println(sys.env.get("JAVA_HOME"))
27+
Some(
28+
new File(
29+
sys.env.getOrElse("JAVA_HOME", System.getProperty("java.home"))
30+
)
31+
)
32+
}
33+
)
2034

2135
commands += Command.command("checkLsif") { s =>
2236
val dumpPath =
@@ -32,6 +46,7 @@ commands += Command.command("checkLsif") { s =>
3246
.filterNot(_ == ".")
3347
.distinct
3448
.sorted
49+
.toList
3550
if (
3651
packageNames != List(
3752
"jdk",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.5.2
1+
sbt.version=1.9.3
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
> sourcegraphEnable
2-
> show semanticdbEnabled
3-
> show Compile/semanticdbEnabled
42
> sourcegraphCompile

0 commit comments

Comments
 (0)