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

Start Bloop with Java 17 #508

Merged
merged 7 commits into from
Dec 22, 2021
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
3 changes: 3 additions & 0 deletions .github/scripts/docker/ScalaCliSlimDockerFile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM debian:stable-slim AS build-env

FROM gcr.io/distroless/base-debian10
ADD scala-cli /usr/local/bin/scala-cli
COPY --from=build-env /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1
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 sure, but we probably should copy distroless license as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file originates from the debian:stable-slim image, not a distroless one. I guess this should be fine…

ENTRYPOINT ["/usr/local/bin/scala-cli"]
7 changes: 7 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ trait CliIntegrationBase extends SbtModule with ScalaCliPublishModule with HasTe

def constantsFile = T.persistent {
val dest = T.dest / "Constants.scala"
val mostlyStaticDockerfile =
os.rel / ".github" / "scripts" / "docker" / "ScalaCliSlimDockerFile"
assert(
os.exists(os.pwd / mostlyStaticDockerfile),
s"Error: ${os.pwd / mostlyStaticDockerfile} not found"
)
val code =
s"""package scala.cli.integration
|
Expand All @@ -416,6 +422,7 @@ trait CliIntegrationBase extends SbtModule with ScalaCliPublishModule with HasTe
| def munitVersion = "${TestDeps.munit.dep.version}"
| def dockerTestImage = "${Docker.testImage}"
| def dockerAlpineTestImage = "${Docker.alpineTestImage}"
| def mostlyStaticDockerfile = "${mostlyStaticDockerfile.toString.replace("\\", "\\\\")}"
| def cs = "${settings.cs().replace("\\", "\\\\")}"
|}
|""".stripMargin
Expand Down
8 changes: 5 additions & 3 deletions modules/build/src/main/scala/scala/build/Artifacts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ object Artifacts {
result
}

private[build] def fetch(
private def fetch(
dependencies: Positioned[Seq[AnyDependency]],
extraRepositories: Seq[String],
params: ScalaParameters,
Expand Down Expand Up @@ -232,9 +232,11 @@ object Artifacts {
.addClassifiers(classifiers.toSeq.filter(_ != "_").map(coursier.Classifier(_)): _*)
}

value {
val res = cache.logger.use {
fetcher.eitherResult()
.left.map(ex => new FetchingDependenciesError(ex, dependencies.positions))
}
value {
res.left.map(ex => new FetchingDependenciesError(ex, dependencies.positions))
}
}

Expand Down
15 changes: 12 additions & 3 deletions modules/build/src/main/scala/scala/build/internal/OsLibc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import coursier.jvm.{JavaHome, JvmIndex}
import java.io.IOException
import java.nio.charset.Charset

import scala.util.Try

object OsLibc {

lazy val isMusl: Option[Boolean] = {
Expand Down Expand Up @@ -59,11 +61,18 @@ object OsLibc {

private def defaultJvmVersion = "8"

def baseDefaultJvm(os: String, jvmVersion: String): String = {
def java17OrHigher = Try(jvmVersion.takeWhile(_.isDigit).toInt)
.toOption
.forall(_ >= 17)
if (os == "linux-musl") s"liberica:$jvmVersion" // zulu could work too
else if (java17OrHigher) s"temurin:$jvmVersion"
else s"adopt:$jvmVersion"
}

def defaultJvm(os: String): String = {
val hasEmptyJavaHome = Option(System.getenv("JAVA_HOME")).exists(_.trim.isEmpty)
val defaultJvm0 =
if (os == "linux-musl") s"liberica:$defaultJvmVersion" // zulu could work too
else s"adopt:$defaultJvmVersion"
val defaultJvm0 = baseDefaultJvm(os, defaultJvmVersion)
if (hasEmptyJavaHome)
// Not using the system JVM if JAVA_HOME is set to an empty string
// (workaround for https://github.com/coursier/coursier/issues/2292)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ final case class BuildOptions(
if (internalDependencies.addTestRunnerDependency) Some(scalaJsOptions.finalVersion)
else None

private lazy val finalCache = internal.cache.getOrElse(FileCache())
lazy val finalCache = internal.cache.getOrElse(FileCache())
// This might download a JVM if --jvm … is passed or no system JVM is installed

case class JavaHomeInfo(javaCommand: String, version: Int)
Expand Down Expand Up @@ -234,7 +234,7 @@ final case class BuildOptions(

def javaHome(): Positioned[JavaHomeInfo] = javaCommand0

private lazy val javaHomeManager = {
lazy val javaHomeManager = {
val indexUrl = javaOptions.jvmIndexOpt.getOrElse(JvmIndex.coursierIndexUrl)
val indexTask = JvmIndex.load(finalCache, indexUrl)
val jvmCache = JvmCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package scala.build.preprocessing
import scala.build.Ops._
import scala.build.errors.{BuildException, CompositeBuildException}
import scala.build.options.{BuildOptions, ConfigMonoid, ScalaOptions}
import scala.build.options.ConfigMonoid
import scala.build.preprocessing.directives.{DirectiveHandler, ProcessedDirective, StrictDirective}
import scala.collection.JavaConverters._

object DirectivesProcessor {

Expand All @@ -13,39 +12,6 @@ object DirectivesProcessor {
unused: Seq[StrictDirective]
)

private def processScala(value: Any): BuildOptions = {

val versions = Some(value)
.toList
.collect {
case list: java.util.List[_] =>
list.asScala.collect { case v: String => v }.toList
case v: String =>
List(v)
}
.flatten
.map(_.filter(!_.isSpaceChar))
.filter(_.nonEmpty)
.distinct

versions match {
case Nil => BuildOptions()
case v :: Nil =>
BuildOptions(
scalaOptions = ScalaOptions(
scalaVersion = Some(v)
)
)
case _ =>
val highest = versions.maxBy(coursier.core.Version(_))
BuildOptions(
scalaOptions = ScalaOptions(
scalaVersion = Some(highest)
)
)
}
}

def process[T: ConfigMonoid](
directives: Seq[StrictDirective],
handlers: Seq[DirectiveHandler[T]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ case object UsingPlatformDirectiveHandler extends UsingDirectiveHandler {
else (input.take(idx), Some(input.drop(idx + 1)))
}

private def maybePlatforms(inputs: Seq[String]): Boolean =
inputs.nonEmpty &&
Platform.parse(Platform.normalize(split(inputs.head)._1)).nonEmpty

private def handle(
rawPfStrsWithPos: Seq[Positioned[String]]
): Either[BuildException, BuildOptions] = either {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ final case class SharedCompilationServerOptions(
@HelpMessage("Bloop global options file")
@Hidden
bloopGlobalOptionsFile: String = (os.home / ".bloop" / "bloop.json").toString,

@Group("Compilation server")
@HelpMessage("JVM to use to start Bloop (e.g. 'system|11', 'temurin:17', …)")
@Hidden
bloopJvm: Option[String] = None
) {
// format: on

Expand Down
19 changes: 13 additions & 6 deletions modules/cli/src/main/scala/scala/cli/commands/SharedOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import upickle.default.{ReadWriter, macroRW}
import java.io.{ByteArrayOutputStream, File, InputStream}

import scala.build.blooprifle.BloopRifleConfig
import scala.build.internal.Constants
import scala.build.internal.{Constants, OsLibc}
import scala.build.options._
import scala.build.{Inputs, LocalRepo, Logger, Os, Position, Positioned}
import scala.concurrent.duration._
Expand Down Expand Up @@ -181,15 +181,22 @@ final case class SharedOptions(

def bloopRifleConfig(): BloopRifleConfig = {

val bo = buildOptions(false, None)
val javaV = bo.javaHome().value.version
val options = buildOptions(false, None)
implicit val ec = options.finalCache.ec
val jvmId = compilationServer.bloopJvm.getOrElse {
OsLibc.baseDefaultJvm(OsLibc.jvmIndexOs, "17")
}
val logger = options.javaHomeManager.cache
.flatMap(_.archiveCache.cache.loggerOpt)
.getOrElse(_root_.coursier.cache.CacheLogger.nop)
val command = os.Path(logger.use(options.javaHomeManager.get(jvmId).unsafeRun()))
val ext = if (Properties.isWin) ".exe" else ""
compilationServer.bloopRifleConfig(
logging.logger,
logging.verbosity,
// This might download a JVM if --jvm … is passed or no system JVM is installed
bo.javaHome().value.javaCommand,
(command / "bin" / s"java$ext").toString,
directories.directories,
Some(javaV)
Some(17)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class RunDockerTests extends munit.FunSuite {
)
inputs.fromRoot { root =>
val termOpt = if (System.console() == null) Nil else Seq("-t")
val ciOpt = Option(System.getenv("CI")).map(v => Seq("-e", s"CI=$v")).getOrElse(Nil)
val rawOutput = new ByteArrayOutputStream
val cmd = Seq[os.Shellable](
// format: off
"docker", "run", "--rm", termOpt, "-v", s"$root:/data", "-w", "/data",
"docker", "run", "--rm", termOpt, "-v", s"$root:/data", "-w", "/data", ciOpt,
imageName, fileName
// format: on
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class NativePackagerTests extends munit.FunSuite {
)
)

private val ciOpt = Option(System.getenv("CI")).map(v => Seq("-e", s"CI=$v")).getOrElse(Nil)

if (Properties.isMac) {
test("building pkg package") {

Expand Down Expand Up @@ -232,7 +234,8 @@ class NativePackagerTests extends munit.FunSuite {
s"$imageRepository:$imageTag"

try {
val output = os.proc("docker", "run", expectedImage).call(cwd = os.root).out.text().trim
val output =
os.proc("docker", "run", ciOpt, expectedImage).call(cwd = os.root).out.text().trim
expect(output == message)
}
// clear
Expand Down Expand Up @@ -268,7 +271,8 @@ class NativePackagerTests extends munit.FunSuite {
s"$imageRepository:$imageTag"

try {
val output = os.proc("docker", "run", expectedImage).call(cwd = os.root).out.text().trim
val output =
os.proc("docker", "run", ciOpt, expectedImage).call(cwd = os.root).out.text().trim
expect(output == message)

}
Expand Down Expand Up @@ -306,7 +310,8 @@ class NativePackagerTests extends munit.FunSuite {
s"$imageRepository:$imageTag"

try {
val output = os.proc("docker", "run", expectedImage).call(cwd = os.root).out.text().trim
val output =
os.proc("docker", "run", ciOpt, expectedImage).call(cwd = os.root).out.text().trim
expect(output == message)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])

private lazy val extraOptions = scalaVersionArgs ++ TestUtil.extraOptions

private val ciOpt = Option(System.getenv("CI")).map(v => Seq("-e", s"CI=$v")).getOrElse(Nil)

def simpleScriptTest(ignoreErrors: Boolean = false): Unit = {
val fileName = "simple.sc"
val message = "Hello"
Expand Down Expand Up @@ -911,6 +913,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
os.copy(os.Path(TestUtil.cli.head, os.pwd), root / "scala")
val script =
s"""#!/usr/bin/env sh
|set -e
|./scala ${extraOptions.mkString(" ") /* meh escaping */} $fileName | tee -a output
|""".stripMargin
os.write(root / "script.sh", script)
Expand All @@ -921,11 +924,13 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
"docker", "run", "--rm", termOpt,
"-v", s"${root}:/data",
"-w", "/data",
ciOpt,
baseImage,
"/data/script.sh"
)
// format: on
os.proc(cmd).call(cwd = root)
val res = os.proc(cmd).call(cwd = root)
System.err.println(res.out.text())
val output = os.read(root / "output").trim
expect(output == message)
}
Expand Down Expand Up @@ -974,14 +979,11 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
|println(msg)
|""".stripMargin,
os.rel / "Dockerfile" ->
"""FROM gcr.io/distroless/base-debian10
|ADD scala /usr/local/bin/scala
|ENTRYPOINT ["/usr/local/bin/scala"]
|""".stripMargin
os.read(os.Path(Constants.mostlyStaticDockerfile, os.pwd))
)
)
inputs.fromRoot { root =>
os.copy(os.Path(TestUtil.cli.head), root / "scala")
os.copy(os.Path(TestUtil.cli.head), root / "scala-cli")
os.proc("docker", "build", "-t", "scala-cli-distroless-it", ".").call(
cwd = root,
stdout = os.Inherit
Expand All @@ -995,6 +997,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
"docker", "run", "--rm", termOpt,
"-v", s"$root:/data",
"-w", "/data",
ciOpt,
"scala-cli-distroless-it",
extraOptions,
fileName
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ Include default JVM options for Bloop

Bloop global options file

#### `--bloop-jvm`

JVM to use to start Bloop (e.g. 'system|11', 'temurin:17', …)

## Compile options

Available in commands:
Expand Down