Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass javaHome to Bloop #2985

Merged
merged 4 commits into from
Jul 1, 2024
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
4 changes: 3 additions & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,9 @@ object Build {
val semanticDbSourceRoot =
options.scalaOptions.semanticDbOptions.semanticDbSourceRoot.getOrElse(inputs.workspace)

val releaseFlagVersion = releaseFlag(options, compilerJvmVersionOpt, logger).map(_.toString)
val releaseFlagVersion =
if (options.useBuildServer.getOrElse(true)) None
else releaseFlag(options, compilerJvmVersionOpt, logger).map(_.toString)

val scalaCompilerParamsOpt = artifacts.scalaOpt match {
case Some(scalaArtifacts) =>
Expand Down
6 changes: 2 additions & 4 deletions modules/build/src/main/scala/scala/build/Project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ final case class Project(
val platform = (scalaJsOptions, scalaNativeOptions) match {
case (None, None) =>
val baseJvmConf = bloopJvmPlatform
baseJvmConf.copy(
// We don't pass jvm home here, because it applies only to java files compilation
config = baseJvmConf.config.copy(home = None)
)
val home = javaHomeOpt.map(_.toNIO).orElse(baseJvmConf.config.home)
baseJvmConf.copy(config = baseJvmConf.config.copy(home = home))
case (Some(jsConfig), _) => BloopConfig.Platform.Js(config = jsConfig, mainClass = None)
case (_, Some(nativeConfig)) =>
BloopConfig.Platform.Native(config = nativeConfig, mainClass = None)
Expand Down
109 changes: 0 additions & 109 deletions modules/build/src/test/scala/scala/build/tests/BuildProjectTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,108 +65,6 @@ class BuildProjectTests extends TestUtil.ScalaCliBuildSuite {
override def flushExperimentalWarnings: Unit = ???
}

val bloopJavaPath = Position.Bloop("/home/empty/jvm/8/")

def testJvmReleaseIsSetCorrectly(
javaHome: String,
bloopJvmVersion: Option[Int],
scalacOptions: Seq[String] = Nil
) = {
val options = BuildOptions(
internal = InternalOptions(localRepository =
LocalRepo.localRepo(scala.build.Directories.default().localRepoDir)
),
javaOptions = JavaOptions(
javaHomeOpt = Some(Positioned.none(os.Path(javaHome)))
),
scalaOptions = ScalaOptions(
scalacOptions = ShadowingSeq.from(
scalacOptions.map(ScalacOpt(_)).map(Positioned.commandLine(_))
)
)
)

val inputs = Inputs.empty("project")
val sources = Sources(Nil, Nil, None, Nil, options)
val logger = new LoggerMock()
val artifacts = options.artifacts(logger, Scope.Test).orThrow
val res = Build.buildProject(
inputs,
sources,
Nil,
options,
bloopJvmVersion.map(bv => Positioned(bloopJavaPath, bv)),
Scope.Test,
logger,
artifacts
)

val scalaCompilerOptions = res.fold(throw _, identity)
.scalaCompiler
.toSeq
.flatMap(_.scalacOptions)
(scalaCompilerOptions, res.fold(throw _, identity).javacOptions, logger.diagnostics)
}

def jvm(v: Int) = os.proc(TestUtil.cs, "java-home", "--jvm", s"zulu:$v").call().out.trim()

test("Compiler options contain target JVM release") {
val javaHome = jvm(8)
val bloopJvmVersion = 11
val (scalacOptions, javacOptions, diagnostics) =
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
expect(scalacOptions.containsSlice(Seq("-release", "8")))
expect(javacOptions.containsSlice(Seq("--release", "8")))
expect(diagnostics.isEmpty)

}

test("Empty BuildOptions is actually empty 2 ") {
val javaHome = jvm(8)
val bloopJvmVersion = 8
val (scalacOptions, javacOptions, diagnostics) =
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
expect(!scalacOptions.containsSlice(Seq("-release")))
expect(!javacOptions.containsSlice(Seq("--release")))
expect(diagnostics.isEmpty)
}

test("Empty BuildOptions is actually empty 2 ") {
val javaHome = jvm(11)
val bloopJvmVersion = 17
val (scalacOptions, javacOptions, diagnostics) =
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
expect(scalacOptions.containsSlice(Seq("-release", "11")))
expect(javacOptions.containsSlice(Seq("--release", "11")))
expect(diagnostics.isEmpty)
}

lazy val expectedDiagnostic = Diagnostic(
Diagnostic.Messages.bloopTooOld,
Severity.Warning,
List(bloopJavaPath)
)

test("Compiler options contain target JVM release") {
val javaHome = jvm(17)
val bloopJvmVersion = 11
val (scalacOptions, javacOptions, diagnostics) =
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion))
expect(!scalacOptions.containsSlice(Seq("-release")))
expect(!javacOptions.containsSlice(Seq("--release")))
expect(diagnostics == List(expectedDiagnostic))
}

test("Empty BuildOptions is actually empty 2 ") {
val javaHome = jvm(11)
val bloopJvmVersion = 8
val (scalacOptions, javacOptions, diagnostics) =
testJvmReleaseIsSetCorrectly(javaHome, Some(bloopJvmVersion), List("-release", "17"))
expect(scalacOptions.containsSlice(Seq("-release", "17")))
expect(!javacOptions.containsSlice(Seq("--release")))
expect(diagnostics == List(expectedDiagnostic))
}

test("workspace for bsp") {
val options = BuildOptions(
internal = InternalOptions(localRepository =
Expand All @@ -183,11 +81,4 @@ class BuildProjectTests extends TestUtil.ScalaCliBuildSuite {

expect(project.workspace == inputs.workspace)
}
test("skip passing release flag for java 8 for ScalaSimpleCompiler") {
val javaHome = jvm(8)
val bloopJvmVersion = 17
val (_, javacOptions, _) =
testJvmReleaseIsSetCorrectly(javaHome, bloopJvmVersion = None)
expect(!javacOptions.containsSlice(Seq("--release")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class BloopTests extends ScalaCliSuite {
val inputs = TestInputs(
os.rel / "Simple.java" ->
s"""|//> using jvm 21
|//> using javacOpt --enable-preview -Xlint:preview
|//> using javacOpt --enable-preview -Xlint:preview --release 21
|//> using javaOpt --enable-preview
|//> using mainClass Simple
|
Expand All @@ -191,4 +191,54 @@ class BloopTests extends ScalaCliSuite {
runScalaCli("bloop", "exit", "--power").call(cwd = root)
}
}

for {
lang <- List("scala", "java")
useDirective <- List(true, false)
option <- List("java-home", "jvm")
}
test(s"compiles $lang file with correct jdk version for $option ${
if (useDirective) "use directive" else "option"
}") {
def isScala = lang == "scala"
val optionValue =
if (option == "java-home")
os.Path(os.proc(TestUtil.cs, "java-home", "--jvm", "zulu:8").call().out.trim()).toString()
else "8"
val directive =
if (useDirective) s"//> using ${option.replace("-h", "H")} $optionValue\n" else ""
val options = if (useDirective) Nil else List(s"--$option", optionValue)
val content =
if (isScala)
"""|package a
|import java.net.http.HttpClient
|
|trait Simple {
| def httpClient: HttpClient
|}
|""".stripMargin
else """|package a;
|
|import java.net.http.HttpClient;
|
|interface Simple {
| HttpClient httpClient();
|}
|""".stripMargin
val inputs = TestInputs(os.rel / s"Simple.$lang" -> s"$directive$content")

inputs.fromRoot { root =>
val res =
runScalaCli(("compile" :: "." :: options)*).call(root, check = false, stderr = os.Pipe)
assert(res.exitCode == 1)

val compilationError = res.err.text()
val message =
if (isScala) "value http is not a member of java.net"
else "error: package java.net.http does not exist"

assert(compilationError.contains("Compilation failed"))
assert(compilationError.contains(message))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,14 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
await(remoteServer.buildTargetCompile(new b.CompileParams(targets.asJava)).asScala)
expect(errorResponse.getStatusCode == b.StatusCode.ERROR)

val javacOptions = Seq("--javac-opt", "--enable-preview")
val javacOptions = Seq(
"--javac-opt",
"--enable-preview",
"--javac-opt",
"--release",
"--javac-opt",
"19"
)

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

val updatedSourceFile =
s"""//> using jvm 19
|//> using javacOpt --enable-preview
|//> using javacOpt --enable-preview --release 19
|
|public class ReloadTest {
| public static void main(String[] args) {
Expand Down Expand Up @@ -1390,7 +1397,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
val inputs = TestInputs(
sourceFilePath ->
s"""//> using jvm 19
|//> using javacOpt --enable-preview
|//> using javacOpt --enable-preview --release 19
|
|public class ReloadTest {
| public static void main(String[] args) {
Expand Down
Loading