Skip to content

Commit bf8f93a

Browse files
authored
Moved BSP-specific module API out of public trait MainModule (#5496)
Fix #5492 Same motivation as for #5214: We use a dedicated `trait` (`BspMainModuleApi`) to hold internal BSP tasks. This is, to free the binary compatibly Module API from these internal tasks. Pull request: #5496
1 parent 36e44a3 commit bf8f93a

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package mill.api.daemon.internal
22

3+
import mill.api.daemon.internal.bsp.BspMainModuleApi
4+
35
trait MainModuleApi extends ModuleApi {
4-
private[mill] def bspClean(
5-
evaluator: EvaluatorApi,
6-
tasks: String*
7-
): TaskApi[Seq[java.nio.file.Path]]
6+
7+
/**
8+
* Internal access to some BSP helper tasks
9+
*/
10+
private[mill] def bspMainModule: () => BspMainModuleApi
11+
812
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package mill.api.daemon.internal.bsp
2+
3+
import mill.api.daemon.internal.{EvaluatorApi, ModuleApi, TaskApi}
4+
5+
trait BspMainModuleApi extends ModuleApi {
6+
7+
private[mill] def bspClean(
8+
evaluator: EvaluatorApi,
9+
tasks: String*
10+
): TaskApi[Seq[java.nio.file.Path]]
11+
12+
}

libs/util/src/mill/util/MainModule.scala

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package mill.util
22

33
import mill.{util, *}
44
import mill.api.*
5-
import mill.api.daemon.internal.{EvaluatorApi, MainModuleApi, TaskApi}
5+
import mill.api.daemon.internal.MainModuleApi
66
import mill.api.*
7-
import mill.api.internal.{RootModule0, RootModule}
7+
import mill.api.internal.{RootModule, RootModule0}
88
import mill.api.SelectMode.Separated
99
import mill.api.daemon.Watchable
1010
import mill.moduledefs.Scaladoc
1111
import mill.api.BuildCtx
12+
import mill.api.daemon.internal.bsp.BspMainModuleApi
1213

1314
import java.util.concurrent.LinkedBlockingQueue
1415
import scala.collection.mutable
@@ -24,7 +25,14 @@ abstract class MainRootModule()(implicit
2425
* [[mill.api.Module]] containing all the default tasks that Mill provides: [[resolve]],
2526
* [[show]], [[inspect]], [[plan]], etc.
2627
*/
27-
trait MainModule extends RootModule0 with MainModuleApi {
28+
trait MainModule extends RootModule0, MainModuleApi {
29+
30+
private lazy val bspExt = {
31+
import bsp.BspMainModule.given
32+
ModuleRef(this.internalBspMainModule)
33+
}
34+
35+
private[mill] def bspMainModule: () => BspMainModuleApi = () => bspExt()
2836

2937
/**
3038
* Show the mill version.
@@ -139,13 +147,6 @@ trait MainModule extends RootModule0 with MainModuleApi {
139147
}
140148
}
141149

142-
private[mill] def bspClean(
143-
evaluator: EvaluatorApi,
144-
tasks: String*
145-
): TaskApi[Seq[java.nio.file.Path]] = Task.Anon {
146-
cleanTask(evaluator.asInstanceOf[Evaluator], tasks*)().map(_.path.toNIO)
147-
}
148-
149150
/**
150151
* Deletes the given targets from the out directory. Providing no targets
151152
* will clean everything.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mill.util.bsp
2+
3+
import mill.api.JsonFormatters.given
4+
import mill.api.daemon.internal.bsp.BspMainModuleApi
5+
import mill.api.daemon.internal.{EvaluatorApi, TaskApi, internal}
6+
import mill.api.{Discover, Evaluator, ExternalModule, ModuleCtx}
7+
import mill.util.MainModule
8+
import mill.Task
9+
10+
import java.nio.file.Path
11+
12+
@internal
13+
private[mill] object BspMainModule extends ExternalModule {
14+
15+
// Requirement of ExternalModule's
16+
override protected def millDiscover: Discover = Discover[this.type]
17+
18+
// Hack-ish way to have some BSP state in the module context
19+
@internal
20+
implicit class EmbeddableBspMainModule(mainModule: MainModule)
21+
extends mill.api.Module {
22+
// We act in the context of the module
23+
override def moduleCtx: ModuleCtx = mainModule.moduleCtx
24+
25+
// We keep all BSP-related tasks/state in this sub-module
26+
@internal
27+
object internalBspMainModule extends mill.api.Module with BspMainModuleApi {
28+
29+
private[mill] def bspClean(
30+
evaluator: EvaluatorApi,
31+
tasks: String*
32+
): TaskApi[Seq[java.nio.file.Path]] = Task.Anon {
33+
mainModule.cleanTask(evaluator.asInstanceOf[Evaluator], tasks*)().map(_.path.toNIO)
34+
}
35+
36+
}
37+
}
38+
39+
}

runner/bsp/worker/src/mill/bsp/worker/MillBuildServer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ private class MillBuildServer(
706706
val mainModule = ev.rootModule.asInstanceOf[mill.api.daemon.internal.MainModuleApi]
707707
val compileTaskName = (module.moduleSegments ++ Label("compile")).render
708708
logger.debug(s"about to clean: ${compileTaskName}")
709-
val cleanTask = mainModule.bspClean(ev, Seq(compileTaskName)*)
709+
val cleanTask = mainModule.bspMainModule().bspClean(ev, Seq(compileTaskName)*)
710710
val cleanResult = evaluate(
711711
ev,
712712
s"Cleaning cache of ${module.bspDisplayName}",

0 commit comments

Comments
 (0)