Skip to content

Commit 6cfd71a

Browse files
authored
Pass javaHome to Bloop (#2985)
* try passing `javaHome` to Bloop * don't add `--release` flags for bloop * test `javaHome` gets properly passed to Bloop * don't import `os.*` instead use directly
1 parent 7a23f2b commit 6cfd71a

File tree

5 files changed

+66
-118
lines changed

5 files changed

+66
-118
lines changed

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,9 @@ object Build {
857857
val semanticDbSourceRoot =
858858
options.scalaOptions.semanticDbOptions.semanticDbSourceRoot.getOrElse(inputs.workspace)
859859

860-
val releaseFlagVersion = releaseFlag(options, compilerJvmVersionOpt, logger).map(_.toString)
860+
val releaseFlagVersion =
861+
if (options.useBuildServer.getOrElse(true)) None
862+
else releaseFlag(options, compilerJvmVersionOpt, logger).map(_.toString)
861863

862864
val scalaCompilerParamsOpt = artifacts.scalaOpt match {
863865
case Some(scalaArtifacts) =>

modules/build/src/main/scala/scala/build/Project.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ final case class Project(
3737
val platform = (scalaJsOptions, scalaNativeOptions) match {
3838
case (None, None) =>
3939
val baseJvmConf = bloopJvmPlatform
40-
baseJvmConf.copy(
41-
// We don't pass jvm home here, because it applies only to java files compilation
42-
config = baseJvmConf.config.copy(home = None)
43-
)
40+
val home = javaHomeOpt.map(_.toNIO).orElse(baseJvmConf.config.home)
41+
baseJvmConf.copy(config = baseJvmConf.config.copy(home = home))
4442
case (Some(jsConfig), _) => BloopConfig.Platform.Js(config = jsConfig, mainClass = None)
4543
case (_, Some(nativeConfig)) =>
4644
BloopConfig.Platform.Native(config = nativeConfig, mainClass = None)

modules/build/src/test/scala/scala/build/tests/BuildProjectTests.scala

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -65,108 +65,6 @@ class BuildProjectTests extends TestUtil.ScalaCliBuildSuite {
6565
override def flushExperimentalWarnings: Unit = ???
6666
}
6767

68-
val bloopJavaPath = Position.Bloop("/home/empty/jvm/8/")
69-
70-
def testJvmReleaseIsSetCorrectly(
71-
javaHome: String,
72-
bloopJvmVersion: Option[Int],
73-
scalacOptions: Seq[String] = Nil
74-
) = {
75-
val options = BuildOptions(
76-
internal = InternalOptions(localRepository =
77-
LocalRepo.localRepo(scala.build.Directories.default().localRepoDir)
78-
),
79-
javaOptions = JavaOptions(
80-
javaHomeOpt = Some(Positioned.none(os.Path(javaHome)))
81-
),
82-
scalaOptions = ScalaOptions(
83-
scalacOptions = ShadowingSeq.from(
84-
scalacOptions.map(ScalacOpt(_)).map(Positioned.commandLine(_))
85-
)
86-
)
87-
)
88-
89-
val inputs = Inputs.empty("project")
90-
val sources = Sources(Nil, Nil, None, Nil, options)
91-
val logger = new LoggerMock()
92-
val artifacts = options.artifacts(logger, Scope.Test).orThrow
93-
val res = Build.buildProject(
94-
inputs,
95-
sources,
96-
Nil,
97-
options,
98-
bloopJvmVersion.map(bv => Positioned(bloopJavaPath, bv)),
99-
Scope.Test,
100-
logger,
101-
artifacts
102-
)
103-
104-
val scalaCompilerOptions = res.fold(throw _, identity)
105-
.scalaCompiler
106-
.toSeq
107-
.flatMap(_.scalacOptions)
108-
(scalaCompilerOptions, res.fold(throw _, identity).javacOptions, logger.diagnostics)
109-
}
110-
111-
def jvm(v: Int) = os.proc(TestUtil.cs, "java-home", "--jvm", s"zulu:$v").call().out.trim()
112-
113-
test("Compiler options contain target JVM release") {
114-
val javaHome = jvm(8)
115-
val bloopJvmVersion = 11
116-
val (scalacOptions, javacOptions, diagnostics) =
117-
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
118-
expect(scalacOptions.containsSlice(Seq("-release", "8")))
119-
expect(javacOptions.containsSlice(Seq("--release", "8")))
120-
expect(diagnostics.isEmpty)
121-
122-
}
123-
124-
test("Empty BuildOptions is actually empty 2 ") {
125-
val javaHome = jvm(8)
126-
val bloopJvmVersion = 8
127-
val (scalacOptions, javacOptions, diagnostics) =
128-
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
129-
expect(!scalacOptions.containsSlice(Seq("-release")))
130-
expect(!javacOptions.containsSlice(Seq("--release")))
131-
expect(diagnostics.isEmpty)
132-
}
133-
134-
test("Empty BuildOptions is actually empty 2 ") {
135-
val javaHome = jvm(11)
136-
val bloopJvmVersion = 17
137-
val (scalacOptions, javacOptions, diagnostics) =
138-
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
139-
expect(scalacOptions.containsSlice(Seq("-release", "11")))
140-
expect(javacOptions.containsSlice(Seq("--release", "11")))
141-
expect(diagnostics.isEmpty)
142-
}
143-
144-
lazy val expectedDiagnostic = Diagnostic(
145-
Diagnostic.Messages.bloopTooOld,
146-
Severity.Warning,
147-
List(bloopJavaPath)
148-
)
149-
150-
test("Compiler options contain target JVM release") {
151-
val javaHome = jvm(17)
152-
val bloopJvmVersion = 11
153-
val (scalacOptions, javacOptions, diagnostics) =
154-
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
155-
expect(!scalacOptions.containsSlice(Seq("-release")))
156-
expect(!javacOptions.containsSlice(Seq("--release")))
157-
expect(diagnostics == List(expectedDiagnostic))
158-
}
159-
160-
test("Empty BuildOptions is actually empty 2 ") {
161-
val javaHome = jvm(11)
162-
val bloopJvmVersion = 8
163-
val (scalacOptions, javacOptions, diagnostics) =
164-
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion), List("-release", "17"))
165-
expect(scalacOptions.containsSlice(Seq("-release", "17")))
166-
expect(!javacOptions.containsSlice(Seq("--release")))
167-
expect(diagnostics == List(expectedDiagnostic))
168-
}
169-
17068
test("workspace for bsp") {
17169
val options = BuildOptions(
17270
internal = InternalOptions(localRepository =
@@ -183,11 +81,4 @@ class BuildProjectTests extends TestUtil.ScalaCliBuildSuite {
18381

18482
expect(project.workspace == inputs.workspace)
18583
}
186-
test("skip passing release flag for java 8 for ScalaSimpleCompiler") {
187-
val javaHome = jvm(8)
188-
val bloopJvmVersion = 17
189-
val (_, javacOptions, _) =
190-
testJvmReleaseIsSetCorrectly(javaHome, bloopJvmVersion = None)
191-
expect(!javacOptions.containsSlice(Seq("--release")))
192-
}
19384
}

modules/integration/src/test/scala/scala/cli/integration/BloopTests.scala

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class BloopTests extends ScalaCliSuite {
174174
val inputs = TestInputs(
175175
os.rel / "Simple.java" ->
176176
s"""|//> using jvm 21
177-
|//> using javacOpt --enable-preview -Xlint:preview
177+
|//> using javacOpt --enable-preview -Xlint:preview --release 21
178178
|//> using javaOpt --enable-preview
179179
|//> using mainClass Simple
180180
|
@@ -191,4 +191,54 @@ class BloopTests extends ScalaCliSuite {
191191
runScalaCli("bloop", "exit", "--power").call(cwd = root)
192192
}
193193
}
194+
195+
for {
196+
lang <- List("scala", "java")
197+
useDirective <- List(true, false)
198+
option <- List("java-home", "jvm")
199+
}
200+
test(s"compiles $lang file with correct jdk version for $option ${
201+
if (useDirective) "use directive" else "option"
202+
}") {
203+
def isScala = lang == "scala"
204+
val optionValue =
205+
if (option == "java-home")
206+
os.Path(os.proc(TestUtil.cs, "java-home", "--jvm", "zulu:8").call().out.trim()).toString()
207+
else "8"
208+
val directive =
209+
if (useDirective) s"//> using ${option.replace("-h", "H")} $optionValue\n" else ""
210+
val options = if (useDirective) Nil else List(s"--$option", optionValue)
211+
val content =
212+
if (isScala)
213+
"""|package a
214+
|import java.net.http.HttpClient
215+
|
216+
|trait Simple {
217+
| def httpClient: HttpClient
218+
|}
219+
|""".stripMargin
220+
else """|package a;
221+
|
222+
|import java.net.http.HttpClient;
223+
|
224+
|interface Simple {
225+
| HttpClient httpClient();
226+
|}
227+
|""".stripMargin
228+
val inputs = TestInputs(os.rel / s"Simple.$lang" -> s"$directive$content")
229+
230+
inputs.fromRoot { root =>
231+
val res =
232+
runScalaCli(("compile" :: "." :: options)*).call(root, check = false, stderr = os.Pipe)
233+
assert(res.exitCode == 1)
234+
235+
val compilationError = res.err.text()
236+
val message =
237+
if (isScala) "value http is not a member of java.net"
238+
else "error: package java.net.http does not exist"
239+
240+
assert(compilationError.contains("Compilation failed"))
241+
assert(compilationError.contains(message))
242+
}
243+
}
194244
}

modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,14 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
12841284
await(remoteServer.buildTargetCompile(new b.CompileParams(targets.asJava)).asScala)
12851285
expect(errorResponse.getStatusCode == b.StatusCode.ERROR)
12861286

1287-
val javacOptions = Seq("--javac-opt", "--enable-preview")
1287+
val javacOptions = Seq(
1288+
"--javac-opt",
1289+
"--enable-preview",
1290+
"--javac-opt",
1291+
"--release",
1292+
"--javac-opt",
1293+
"19"
1294+
)
12881295

12891296
os.proc(TestUtil.cli, "setup-ide", ".", "--jvm", "19", javacOptions, extraOptions)
12901297
.call(
@@ -1348,7 +1355,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
13481355

13491356
val updatedSourceFile =
13501357
s"""//> using jvm 19
1351-
|//> using javacOpt --enable-preview
1358+
|//> using javacOpt --enable-preview --release 19
13521359
|
13531360
|public class ReloadTest {
13541361
| public static void main(String[] args) {
@@ -1390,7 +1397,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
13901397
val inputs = TestInputs(
13911398
sourceFilePath ->
13921399
s"""//> using jvm 19
1393-
|//> using javacOpt --enable-preview
1400+
|//> using javacOpt --enable-preview --release 19
13941401
|
13951402
|public class ReloadTest {
13961403
| public static void main(String[] args) {

0 commit comments

Comments
 (0)