From 0fc8576d9cdbbdfef6bea0159f1011cf91103507 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 11 Dec 2023 06:07:59 -0700 Subject: [PATCH] fix: Use ForkRun for generateJmhSourcesAndResources (#243) Forking has a couple of benefits: 1. It runs with the full modules for the JVM, so that it doesn't error if you use something from a module such as java.sql 2. If the generation throws an exception, it doesn't kill sbt outright. Taken from https://github.com/pawel-wiejacha/sbt-jmh/commit/a916d771b2d75dce3404cce5cba282884f2251c6 Fixes: #190 Co-authored-by: pawel-wiejacha --- .../src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala b/plugin/src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala index 1821e4b..0e9e9d1 100644 --- a/plugin/src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala +++ b/plugin/src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala @@ -56,7 +56,7 @@ object JmhPlugin extends AutoPlugin { resourceDirectory := (Compile / resourceDirectory).value, sourceGenerators := Seq(Def.task { generateJmhSourcesAndResources.value._1 }.taskValue), resourceGenerators := Seq(Def.task { generateJmhSourcesAndResources.value._2 }.taskValue), - generateJmhSourcesAndResources := generateBenchmarkSourcesAndResources(streams.value, crossTarget.value / "jmh-cache", (Jmh / classDirectory).value, sourceManaged.value, resourceManaged.value, generatorType.value, (Jmh / dependencyClasspath).value, new Run(scalaInstance.value, true, taskTemporaryDirectory.value)), + generateJmhSourcesAndResources := generateBenchmarkSourcesAndResources(streams.value, crossTarget.value / "jmh-cache", (Jmh / classDirectory).value, sourceManaged.value, resourceManaged.value, generatorType.value, (Jmh / dependencyClasspath).value, new ForkRun(ForkOptions())), generateJmhSourcesAndResources := (generateJmhSourcesAndResources dependsOn(Compile / compile)).value, // local copy of https://github.com/sbt/sbt/blob/e4231ac03903e174bc9975ee00d34064a1d1f373/main/src/main/scala/sbt/Keys.scala#L400 @@ -89,7 +89,7 @@ object JmhPlugin extends AutoPlugin { props.get("jmh.version").toString } - private def generateBenchmarkSourcesAndResources(s: TaskStreams, cacheDir: File, bytecodeDir: File, sourceDir: File, resourceDir: File, generatorType: String, classpath: Seq[Attributed[File]], run: Run): (Seq[File], Seq[File]) = { + private def generateBenchmarkSourcesAndResources(s: TaskStreams, cacheDir: File, bytecodeDir: File, sourceDir: File, resourceDir: File, generatorType: String, classpath: Seq[Attributed[File]], run: ScalaRun): (Seq[File], Seq[File]) = { val inputs: Set[File] = (bytecodeDir ** "*").filter(_.isFile).get.toSet val cachedGeneration = FileFunction.cached(cacheDir, FilesInfo.hash) { _ => // ignore change report and rebuild it completely @@ -100,7 +100,7 @@ object JmhPlugin extends AutoPlugin { private def internalGenerateBenchmarkSourcesAndResources(s: TaskStreams, bytecodeDir: File, sourceDir: File, resourceDir: File, generatorType: String, classpath: Seq[Attributed[File]], - run: Run, log: Logger): Set[File] = { + run: ScalaRun, log: Logger): Set[File] = { // rebuild everything IO.delete(sourceDir) IO.createDirectory(sourceDir)