-
-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored BSP module and use isolated classloading for implementations #2245
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4245302
Refactored BSP module and use isolated classloading for implemenations
lefou d93b018
Cleanup
lefou bab124f
Detect and log version mismatches between Mill and BSP worker
lefou e9d8781
Addressed review comments
lefou 8d031b6
Moved reflection logic into BspServerStarter companion
lefou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ | ||
package mill.bsp | ||
|
||
import mill.main.BspServerStarter | ||
|
||
object BspServerStarterImpl { | ||
def get: BspServerStarter = BSP | ||
} |
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,80 @@ | ||
package mill.bsp | ||
|
||
import mill.Agg | ||
import mill.api.{Ctx, PathRef, Result, internal} | ||
import mill.define.Task | ||
import mill.eval.Evaluator | ||
import mill.main.{BspServerHandle, BspServerResult} | ||
|
||
import java.io.{InputStream, PrintStream} | ||
import java.net.URL | ||
import scala.concurrent.Promise | ||
import scala.util.{Failure, Success, Try} | ||
|
||
@internal | ||
trait BspWorker { | ||
|
||
def createBspConnection( | ||
jobs: Int, | ||
serverName: String | ||
)(implicit ctx: Ctx): Unit | ||
|
||
def startBspServer( | ||
initialEvaluator: Option[Evaluator], | ||
outStream: PrintStream, | ||
errStream: PrintStream, | ||
inStream: InputStream, | ||
logDir: os.Path, | ||
canReload: Boolean, | ||
serverHandles: Seq[Promise[BspServerHandle]] | ||
): BspServerResult | ||
|
||
} | ||
|
||
@internal | ||
object BspWorker { | ||
|
||
private[this] var worker: Option[BspWorker] = None | ||
|
||
def apply(millCtx: Ctx.Workspace with Ctx.Home with Ctx.Log): Result[BspWorker] = { | ||
worker match { | ||
case Some(x) => Result.Success(x) | ||
case None => | ||
// load extra classpath entries from file | ||
val cpFile = millCtx.workspace / Constants.bspDir / s"${Constants.serverName}.resources" | ||
if (!os.exists(cpFile)) return Result.Failure( | ||
"You need to run `mill mill.bsp.BSP/install` before you can use the BSP server" | ||
) | ||
|
||
// TODO: if outdated, we could regenerate the resource file and re-load the worker | ||
|
||
val urls = os.read(cpFile).linesIterator.map(u => new URL(u)).toSeq | ||
|
||
// create classloader with bsp.worker and deps | ||
val cl = mill.api.ClassLoader.create(urls, getClass().getClassLoader())(millCtx) | ||
val workerVersion = Try { | ||
val workerBuildInfo = cl.loadClass(Constants.bspWorkerBuildInfoClass) | ||
workerBuildInfo.getMethod("millBspWorkerVersion").invoke(null) | ||
} match { | ||
case Success(mill.BuildInfo.millVersion) => // same as Mill, everything is good | ||
case Success(workerVersion) => | ||
millCtx.log.error( | ||
s"""BSP worker version ($workerVersion) does not match Mill version (${mill.BuildInfo.millVersion}). | ||
|You need to run `mill mill.bsp.BSP/install` again.""".stripMargin | ||
) | ||
case Failure(e) => | ||
millCtx.log.error( | ||
s"""Could not validate worker version number. | ||
|Error message: ${e.getMessage} | ||
|""".stripMargin) | ||
} | ||
|
||
val workerCls = cl.loadClass(Constants.bspWorkerImplClass) | ||
val ctr = workerCls.getConstructor() | ||
val workerImpl = ctr.newInstance().asInstanceOf[BspWorker] | ||
worker = Some(workerImpl) | ||
Result.Success(workerImpl) | ||
} | ||
} | ||
|
||
} |
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,13 @@ | ||
package mill.bsp | ||
|
||
import mill.api.internal | ||
|
||
@internal | ||
object Constants { | ||
lefou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val bspDir = os.sub / ".bsp" | ||
val bspProtocolVersion = BuildInfo.bsp4jVersion | ||
val bspWorkerImplClass = "mill.bsp.worker.BspWorkerImpl" | ||
val bspWorkerBuildInfoClass = "mill.bsp.worker.BuildInfo" | ||
val languages = Seq("scala", "java") | ||
val serverName = "mill-bsp" | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be nice, detect the Mill version somehow and warn the user if there is a mismatch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, I see we actually do this down below, so is this a leftover comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's still valid. We detect that we are outdated, but at this point we can't do anything about it, except logging or failing. Failing is too rude here, as we would force the user to understand and handle it. Logging is only helpful to understand the issue when we get access to the logs.
At this stage (firing up BSP server without having a completely initialized Evaluator) we can't trigger a re-build of the BSP connection file, as we can't use the evaluator and the task graph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to handle a detected outdated worker could be to: