Skip to content

Commit

Permalink
Merge pull request #2195 from tgodzik/add-runtime
Browse files Browse the repository at this point in the history
bugfix: Export runtime jars correctly from sbt
  • Loading branch information
tgodzik authored Nov 21, 2023
2 parents 6cb81c3 + 8132093 commit cfc8328
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import sbt.Optional
import sbt.ProjectRef
import sbt.Provided
import sbt.ResolvedProject
import sbt.Runtime
import sbt.TaskKey
import sbt.Test
import sbt.ThisBuild
Expand Down Expand Up @@ -600,25 +601,6 @@ object BloopDefaults {
}
}

// Unused for now, but we leave it here because it may be useful in the future.
def nameFromDependency(dependency: ClasspathDependency, thisProject: ResolvedProject): String = {
import sbt.{LocalProject, LocalRootProject, RootProject}

val projectName = dependency.project match {
case ThisProject => thisProject.id
case LocalProject(project) => project
// Not sure about these three:
case LocalRootProject => "root"
case ProjectRef(_, project) => project
case RootProject(_) => "root"
}

dependency.configuration match {
case Some("compile") | None => projectName
case Some(configurationName) => s"$projectName-$configurationName"
}
}

/**
* Replace any old path that is used as a scalac option by the new path.
*
Expand Down Expand Up @@ -806,8 +788,19 @@ object BloopDefaults {
val isForkedExecution = (Keys.fork in configuration in forkScopedTask).value
val workingDir = if (isForkedExecution) Keys.baseDirectory.value else rootBaseDirectory
val extraJavaOptions = List(s"-Duser.dir=${workingDir.getAbsolutePath}")
lazy val runtimeClasspath = (Runtime / Keys.fullClasspath).value.map(_.data.toPath()).toList
lazy val javaRuntimeHome = (Runtime / Keys.javaHome).value.map(_.toPath())
lazy val javaRuntimeOptions = (Runtime / Keys.javaOptions).value
val config = Config.JvmConfig(Some(javaHome.toPath), (extraJavaOptions ++ javaOptions).toList)
Config.Platform.Jvm(config, mainClass, None, None, None)
lazy val configRuntime =
Config.JvmConfig(javaRuntimeHome.orElse(Some(javaHome.toPath)), (extraJavaOptions ++ javaRuntimeOptions).toList)
/* Runtime config is only used for run task, which is in Compile scope normally.
* Test classpath already contains runtime config, so it's not needed to set separate classpath for test scope.
*/
if (configuration == Compile)
Config.Platform.Jvm(config, mainClass, Some(configRuntime), Some(runtimeClasspath), None)
else
Config.Platform.Jvm(config, mainClass, None, None, None)
}
}
// FORMAT: ON
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import bloop.integrations.sbt.BloopDefaults

name := "runtime-dependency"

libraryDependencies +=
"ch.qos.logback" % "logback-classic" % "1.2.7" % Runtime

val bloopConfigFile = settingKey[File]("Config file to test")
ThisBuild / bloopConfigFile := {
val bloopDir = Keys.baseDirectory.value./(".bloop")
val config = bloopDir./("runtime-dependency.json")
config
}

val bloopTestConfigFile = settingKey[File]("Test config file to test")
ThisBuild / bloopTestConfigFile := {
val bloopDir = Keys.baseDirectory.value./(".bloop")
val config = bloopDir./("runtime-dependency-test.json")
config
}

val checkBloopFiles = taskKey[Unit]("Check bloop file contents")
ThisBuild / checkBloopFiles := {
val configContents = BloopDefaults.unsafeParseConfig(bloopConfigFile.value.toPath)
assert(configContents.project.platform.isDefined)
val platformJvm =
configContents.project.platform.get.asInstanceOf[bloop.config.Config.Platform.Jvm]
val obtainedRuntimeClasspath = platformJvm.classpath.map(_.map(_.getFileName.toString))
val expectedRuntimeClasspath = Some(
List(
"classes",
"logback-core-1.2.7.jar",
"scala-library.jar",
"slf4j-api-1.7.32.jar",
"logback-classic-1.2.7.jar"
)
)
assert(obtainedRuntimeClasspath == expectedRuntimeClasspath)

assert(configContents.project.classpath.map(_.getFileName.toString) == List("scala-library.jar"))

val configTestContents = BloopDefaults.unsafeParseConfig(bloopTestConfigFile.value.toPath)
assert(configTestContents.project.platform.isDefined)
val testPlatformJvm =
configTestContents.project.platform.get.asInstanceOf[bloop.config.Config.Platform.Jvm]
assert(testPlatformJvm.classpath.isEmpty)

val obtainedTestClasspath = configTestContents.project.classpath.map(_.getFileName.toString)
println(obtainedTestClasspath)
val expectedTestClasspath =
List(
"classes",
"logback-core-1.2.7.jar",
"scala-library.jar",
"slf4j-api-1.7.32.jar",
"logback-classic-1.2.7.jar"
)

assert(obtainedTestClasspath == expectedTestClasspath)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % sys.props.apply("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> bloopGenerate
> test:bloopGenerate
> checkBloopFiles

0 comments on commit cfc8328

Please sign in to comment.