From d7e471e3ae02c209a7b2e61a7efb6dcfa2e2d78b Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Tue, 18 Jun 2024 09:25:58 +0200 Subject: [PATCH] Support `build.sc` in Bloop (#3208) After Mill `0.11` `mill-contrib-bloop` stopped supporting the build module. Now `BloopImpl` gets a sequence of `Evaluator`s and gets all modules for all the evaluators in `Evaluator.allBootstrapEvaluators`, so it supports the build module again. Pull Request: https://github.com/com-lihaoyi/mill/pull/3208 --- build.sc | 1 + .../bloop/src/mill/contrib/bloop/Bloop.scala | 2 +- .../src/mill/contrib/bloop/BloopImpl.scala | 28 +++++++++-------- .../src/mill/contrib/bloop/BloopTests.scala | 2 +- integration/feature/bloop/repo/build.sc | 8 +++++ .../feature/bloop/test/src/BloopTests.scala | 30 +++++++++++++++++++ 6 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 integration/feature/bloop/repo/build.sc create mode 100644 integration/feature/bloop/test/src/BloopTests.scala diff --git a/build.sc b/build.sc index 4655de59a65..b9d8d6c6228 100644 --- a/build.sc +++ b/build.sc @@ -1447,6 +1447,7 @@ object dev extends MillPublishScalaModule { def testTransitiveDeps = super.testTransitiveDeps() ++ Seq( runner.linenumbers.testDep(), scalalib.backgroundwrapper.testDep(), + contrib.bloop.testDep(), contrib.buildinfo.testDep(), contrib.scoverage.testDep(), contrib.scoverage.worker2.testDep(), diff --git a/contrib/bloop/src/mill/contrib/bloop/Bloop.scala b/contrib/bloop/src/mill/contrib/bloop/Bloop.scala index 5e53c54464c..99de7d536ec 100644 --- a/contrib/bloop/src/mill/contrib/bloop/Bloop.scala +++ b/contrib/bloop/src/mill/contrib/bloop/Bloop.scala @@ -5,4 +5,4 @@ import mill.eval.Evaluator /** * Usage : `mill mill.contrib.bloop.Bloop/install` */ -object Bloop extends BloopImpl(() => Evaluator.currentEvaluator.value, os.pwd) +object Bloop extends BloopImpl(() => Evaluator.allBootstrapEvaluators.value.value, os.pwd) diff --git a/contrib/bloop/src/mill/contrib/bloop/BloopImpl.scala b/contrib/bloop/src/mill/contrib/bloop/BloopImpl.scala index 49f85ed7a89..3d27d323cfd 100644 --- a/contrib/bloop/src/mill/contrib/bloop/BloopImpl.scala +++ b/contrib/bloop/src/mill/contrib/bloop/BloopImpl.scala @@ -5,7 +5,7 @@ import mill._ import mill.api.Result import mill.define.{Discover, ExternalModule, Module => MillModule} import mill.eval.Evaluator -import mill.scalalib.internal.{JavaModuleUtils, ModuleUtils} +import mill.scalalib.internal.JavaModuleUtils import mill.scalajslib.ScalaJSModule import mill.scalajslib.api.{JsEnvConfig, ModuleKind} import mill.scalalib._ @@ -17,7 +17,8 @@ import mill.scalanativelib.api.ReleaseMode * `mill.contrib.bloop.Bloop` object, and usable in tests by passing * a custom evaluator. */ -class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer => +class BloopImpl(evs: () => Seq[Evaluator], wd: os.Path) extends ExternalModule { + outer => import BloopFormats._ private val bloopDir = wd / ".bloop" @@ -116,12 +117,14 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer } protected def computeModules: Seq[JavaModule] = { - val eval = ev() - if (eval != null) - JavaModuleUtils.transitiveModules(eval.rootModule, accept) - .collect { case jm: JavaModule => jm } - else - Seq.empty + val evals = evs() + evals.flatMap { eval => + if (eval != null) + JavaModuleUtils.transitiveModules(eval.rootModule, accept) + .collect { case jm: JavaModule => jm } + else + Seq.empty + } } // class-based pattern matching against path-dependant types doesn't seem to work. @@ -144,10 +147,7 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer Result.Success(sources.toMap) } - protected def name(m: JavaModule): String = ModuleUtils.moduleDisplayName(m) match { - case "" => "root-module" - case n => n - } + protected def name(m: JavaModule): String = m.bspDisplayName.replace('/', '-') protected def bloopConfigPath(module: JavaModule): os.Path = bloopDir / s"${name(module)}.json" @@ -365,6 +365,7 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer .groupBy { case (org, mod, version, _, _) => (org, mod, version) } + .view .mapValues { _.map { case (_, mod, _, classifier, file) => @@ -381,8 +382,9 @@ class BloopImpl(ev: () => Evaluator, wd: os.Path) extends ExternalModule { outer artifacts = artifacts ) } + .toList - gatherTask.unsafeRun().toList + gatherTask.unsafeRun() } val bloopResolution: Task[BloopConfig.Resolution] = T.task { diff --git a/contrib/bloop/test/src/mill/contrib/bloop/BloopTests.scala b/contrib/bloop/test/src/mill/contrib/bloop/BloopTests.scala index da03026c81d..731cce4928a 100644 --- a/contrib/bloop/test/src/mill/contrib/bloop/BloopTests.scala +++ b/contrib/bloop/test/src/mill/contrib/bloop/BloopTests.scala @@ -17,7 +17,7 @@ object BloopTests extends TestSuite { val workdir = os.pwd / "target" / "workspace" / "bloop" val testEvaluator = TestEvaluator.static(build) - val testBloop = new BloopImpl(() => testEvaluator.evaluator, workdir) + val testBloop = new BloopImpl(() => Seq(testEvaluator.evaluator), workdir) object build extends TestUtil.BaseModule { diff --git a/integration/feature/bloop/repo/build.sc b/integration/feature/bloop/repo/build.sc new file mode 100644 index 00000000000..5ee3272e336 --- /dev/null +++ b/integration/feature/bloop/repo/build.sc @@ -0,0 +1,8 @@ +// mill plugins +import $ivy.`com.lihaoyi::mill-contrib-bloop:` + +// imports +import mill._ +import mill.scalalib._ + +object root extends RootModule with JavaModule diff --git a/integration/feature/bloop/test/src/BloopTests.scala b/integration/feature/bloop/test/src/BloopTests.scala new file mode 100644 index 00000000000..90743177ac3 --- /dev/null +++ b/integration/feature/bloop/test/src/BloopTests.scala @@ -0,0 +1,30 @@ +package mill.integration + +import utest._ + +object BloopTests extends IntegrationTestSuite { + initWorkspace() + + val installResult: Boolean = eval("mill.contrib.bloop.Bloop/install") + + val tests: Tests = Tests { + test("test") - { + assert(installResult) + + "root module bloop config should be created" - { + assert(os.exists(wd / ".bloop" / "root-module.json")) + } + val millBuildJsonFile = wd / ".bloop" / "mill-build-.json" + "mill-build module bloop config should be created" - { + assert(os.exists(millBuildJsonFile)) + } + + val config = ujson.read(os.read.stream(millBuildJsonFile)) + "mill-build config should contain build.sc source" - { + assert(config("project")("sources").arr.exists(path => + os.Path(path.str).last == "build.sc" + )) + } + } + } +}