-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanups for MillBuildServer (#2518)
1. Break `mill.bsp.worker.State` into a separate file 2. Fold `targetTasks` into `completableTasks`, and adjust all callsites to use that 3. Make `completableTasks` take a `tasks: BspModule => Task[W]` parameter, so we can perform all evaluation once up-front and then fall back to normal code after (rather than having it all execute in the context of the evaluator inside tasks, which obfuscates stack traces and evaluation order) 4. Move `agg` param in `completableTasks` to a third parameter list, to allow type inference to work better 5. Make everything in `mill.bsp.worker` private 6. Move all the remaining BSP stuff from `main/` and `runner/` to `bsp/`, remove weird reflection hacks to just use direct instantiation where possible since now they're in the same module 7. remove the `BspServerStarter`/`BspServerStarterImpl` layers of indirection 8. Move `BspConfigJson.scala`, `bspConnectionJson` and `createBspConnection` out of `bsp.worker` into `bsp` since it doesn't depend on anything heavyweight that necessitates it being in the worker module 9. Moved `def startBspServer` out of `mill.bsp.BSP` into `mill.bsp.BspContext`, leaving `mill.bsp.BSP` to just serve one purpose as `mill.define.Module` rather than additionally being a grab-bag of helper logic 10. Simplify the concurrency story around instantiating the `BspServerHandle`: - Previously, the whole of `startBspServer` was put in a `Future`, with the main result of the `Future` waiting on the server to terminate only to do some logging and discard the result, and the creation of the `BspServerHandle` was propagated via `Promise`s and that the main thread would `Await` upon - With this PR, the main thread runs the main logic of `startBspServer`, directly returning the `BspServerHandle` when it completes (or `Left[String]` on error). Server termination is waited for in a side thread, that does nothing but call `listening.get()` and do logging when it terminates. We still need a global variable to pass it to `def startSession`, but this can just be a `@volatile var` rather than a global `Promise` - This maintains the existing semantics, but using 2 parallel threads instead of 3, with the primary logic shifted to the main thread, and without need for any `Await`s Overall this PR tries to cut a lot of the unnecessary complexity and messiness around the BSP logic. This results in a substantial reduction in lines of code, while still keeping the core logic unchanged. Hopefully this will make things easier to maintain and debug going forward. Tested manually using VSCode on `examples/misc/3-import-file-ivy` Pull request: #2518
- Loading branch information
Showing
27 changed files
with
622 additions
and
774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mill.bsp | ||
|
||
import upickle.default._ | ||
|
||
private case class BspConfigJson( | ||
name: String, | ||
argv: Seq[String], | ||
millVersion: String, | ||
bspVersion: String, | ||
languages: Seq[String] | ||
) | ||
|
||
private object BspConfigJson { | ||
implicit val rw: ReadWriter[BspConfigJson] = macroRW | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package mill.bsp | ||
|
||
import mill.api.{DummyInputStream, Logger, SystemStreams, internal} | ||
import mill.eval.Evaluator | ||
|
||
import java.io.PrintStream | ||
import scala.util.control.NonFatal | ||
|
||
object BspContext { | ||
@volatile var bspServerHandle: BspServerHandle = null | ||
} | ||
|
||
@internal | ||
class BspContext(streams: SystemStreams, bspLogStream: Option[PrintStream], home: os.Path) { | ||
// BSP mode, run with a simple evaluator command to inject the evaluator | ||
// The command returns when the server exists or the workspace should be reloaded | ||
// if the `lastResult` is `ReloadWorkspace` we re-run the script in a loop | ||
|
||
streams.err.println("Running in BSP mode with hardcoded startSession command") | ||
|
||
streams.err.println("Trying to load BSP server...") | ||
BspContext.bspServerHandle = | ||
try { | ||
startBspServer( | ||
initialEvaluator = None, | ||
streams = streams, | ||
logStream = bspLogStream, | ||
canReload = true | ||
) match { | ||
case Left(err) => sys.error(err) | ||
case Right(res) => res | ||
} | ||
} catch { | ||
case NonFatal(e) => | ||
streams.err.println(s"Could not start BSP server. ${e.getMessage}") | ||
throw e | ||
} | ||
|
||
streams.err.println("BSP server started") | ||
|
||
def startBspServer( | ||
initialEvaluator: Option[Evaluator], | ||
streams: SystemStreams, | ||
logStream: Option[PrintStream], | ||
canReload: Boolean | ||
): Either[String, BspServerHandle] = { | ||
val log: Logger = new Logger { | ||
override def colored: Boolean = false | ||
override def systemStreams: SystemStreams = new SystemStreams( | ||
out = streams.out, | ||
err = streams.err, | ||
in = DummyInputStream | ||
) | ||
|
||
override def info(s: String): Unit = streams.err.println(s) | ||
override def error(s: String): Unit = streams.err.println(s) | ||
override def ticker(s: String): Unit = streams.err.println(s) | ||
override def debug(s: String): Unit = streams.err.println(s) | ||
override def debugEnabled: Boolean = true | ||
} | ||
|
||
BspWorker(os.pwd, home, log).flatMap { worker => | ||
os.makeDir.all(home / Constants.bspDir) | ||
worker.startBspServer( | ||
initialEvaluator, | ||
streams, | ||
logStream.getOrElse(streams.err), | ||
home / Constants.bspDir, | ||
canReload | ||
) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
main/src/mill/main/BspServerHandle.scala → bsp/src/mill/bsp/BspServerHandle.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mill.main | ||
package mill.bsp | ||
|
||
import mill.eval.Evaluator | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
main/src/mill/main/BspServerResult.scala → bsp/src/mill/bsp/BspServerResult.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mill.main | ||
package mill.bsp | ||
|
||
import mill.api.internal | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.