-
Notifications
You must be signed in to change notification settings - Fork 202
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
feature: Switch to using Bloop Rifle and backport all improvements #2355
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc8e3b2
feature: Switch to using Bloop Rifle and backport all improvements
tgodzik 199bab4
improvement: Use cli module from bloop-core as it has more utilities
tgodzik b20ee47
bugfix: Remove everything if Zombie found
tgodzik 6db1fec
bugfix: Switch to fixed version of libjvmdeamon
tgodzik 4c8fcde
bugfix: Fix all cli module issues
tgodzik 8e7645b
chore: Don't publish Bloop cli native image on PRs
tgodzik 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
186 changes: 186 additions & 0 deletions
186
bloop-rifle/src/main/scala/bloop/rifle/BloopRifle.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 |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package bloop.rifle | ||
|
||
import bloop.rifle.internal.{Operations, Util} | ||
|
||
import java.io.{ByteArrayOutputStream, OutputStream} | ||
import java.nio.file.Path | ||
import java.util.concurrent.ScheduledExecutorService | ||
|
||
import scala.concurrent.Future | ||
|
||
object BloopRifle { | ||
|
||
/** | ||
* Checks whether a bloop server is running at this host / port. | ||
* | ||
* @param host | ||
* @param port | ||
* @param logger | ||
* @return | ||
* Whether a server is running or not. | ||
*/ | ||
def check( | ||
config: BloopRifleConfig, | ||
logger: BloopRifleLogger | ||
): Boolean = { | ||
def check() = | ||
Operations.check( | ||
config.address, | ||
logger | ||
) | ||
check() | ||
} | ||
|
||
/** | ||
* Starts a new bloop server. | ||
* | ||
* @param config | ||
* @param scheduler | ||
* @param logger | ||
* @return | ||
* A future, that gets completed when the server is done starting (and can thus be used). | ||
*/ | ||
def startServer( | ||
config: BloopRifleConfig, | ||
scheduler: ScheduledExecutorService, | ||
logger: BloopRifleLogger, | ||
version: String, | ||
bloopJava: String | ||
): Future[Unit] = | ||
config.classPath(version) match { | ||
case Left(ex) => Future.failed(new Exception("Error getting Bloop class path", ex)) | ||
case Right(cp) => | ||
logger.info("Starting compilation server") | ||
logger.debug( | ||
s"Starting Bloop $version at ${config.address.render} using JVM $bloopJava" | ||
) | ||
object IntValue { | ||
def unapply(s: String): Option[Int] = | ||
// no String.toIntOption in Scala 2.12.x | ||
try Some(s.toInt) | ||
catch { | ||
case _: NumberFormatException => None | ||
} | ||
} | ||
val bloopServerSupportsFileTruncating = | ||
version.takeWhile(c => c.isDigit || c == '.').split('.') match { | ||
case Array(IntValue(maj), IntValue(min), IntValue(patch), _ @_*) => | ||
import scala.math.Ordering.Implicits._ | ||
Seq(maj, min, patch) >= Seq(1, 4, 20) | ||
case _ => | ||
false | ||
} | ||
|
||
Operations.startServer( | ||
config.address, | ||
bloopJava, | ||
config.javaOpts, | ||
cp.map(_.toPath), | ||
config.workingDir, | ||
scheduler, | ||
config.startCheckPeriod, | ||
config.startCheckTimeout, | ||
logger, | ||
bloopServerSupportsFileTruncating = bloopServerSupportsFileTruncating | ||
) | ||
} | ||
|
||
/** | ||
* Opens a BSP connection to a running bloop server. | ||
* | ||
* Starts a thread to read output from the nailgun connection, and another one to pass input to | ||
* it. | ||
* | ||
* @param logger | ||
* @return | ||
* A [[BspConnection]] object, that can be used to close the connection. | ||
*/ | ||
def bsp( | ||
config: BloopRifleConfig, | ||
workingDir: Path, | ||
logger: BloopRifleLogger | ||
): BspConnection = { | ||
|
||
val bspSocketOrPort = config.bspSocketOrPort.map(_()).getOrElse { | ||
BspConnectionAddress.Tcp("127.0.0.1", Util.randomPort()) | ||
} | ||
|
||
val inOpt = config.bspStdin | ||
|
||
val out = config.bspStdout.getOrElse(OutputStream.nullOutputStream()) | ||
val err = config.bspStderr.getOrElse(OutputStream.nullOutputStream()) | ||
|
||
Operations.bsp( | ||
config.address, | ||
bspSocketOrPort, | ||
workingDir, | ||
inOpt, | ||
out, | ||
err, | ||
logger | ||
) | ||
} | ||
|
||
def exit( | ||
config: BloopRifleConfig, | ||
workingDir: Path, | ||
logger: BloopRifleLogger | ||
): Int = { | ||
|
||
val out = config.bspStdout.getOrElse(OutputStream.nullOutputStream()) | ||
val err = config.bspStderr.getOrElse(OutputStream.nullOutputStream()) | ||
|
||
Operations.exit( | ||
config.address, | ||
workingDir, | ||
out, | ||
err, | ||
logger | ||
) | ||
} | ||
|
||
def getCurrentBloopVersion( | ||
config: BloopRifleConfig, | ||
logger: BloopRifleLogger, | ||
workdir: Path, | ||
scheduler: ScheduledExecutorService | ||
): Either[BloopAboutFailure, BloopServerRuntimeInfo] = { | ||
val isRunning = BloopRifle.check(config, logger) | ||
|
||
if (isRunning) { | ||
val bufferedOStream = new ByteArrayOutputStream | ||
Operations.about( | ||
config.address, | ||
workdir, | ||
bufferedOStream, | ||
OutputStream.nullOutputStream(), | ||
logger, | ||
scheduler | ||
) | ||
val bloopAboutOutput = new String(bufferedOStream.toByteArray) | ||
VersionUtil | ||
.parseBloopAbout(bloopAboutOutput) | ||
.toRight(ParsingFailed(bloopAboutOutput)) | ||
} else | ||
Left(BloopNotRunning) | ||
} | ||
|
||
sealed abstract class BloopAboutFailure extends Product with Serializable { | ||
def message: String | ||
} | ||
case object BloopNotRunning extends BloopAboutFailure { | ||
def message = "not running" | ||
} | ||
final case class ParsingFailed(bloopAboutOutput: String) extends BloopAboutFailure { | ||
def message = s"failed to parse output: '$bloopAboutOutput'" | ||
} | ||
|
||
final case class BloopServerRuntimeInfo( | ||
bloopVersion: BloopVersion, | ||
jvmVersion: Int, | ||
javaHome: String | ||
) { | ||
def message: String = | ||
s"version $bloopVersion, JVM $jvmVersion under $javaHome" | ||
} | ||
} |
Oops, something went wrong.
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.
Is it not compatible with Java 11 anymore?
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.
We need at least Java 16 for UnixDomainSocket to work, which was the biggest blocker here, but Metals already dropped JDK 8 and uses JDK 17 as default with correct release flags being set automatically. And Bloop also does add release flags to make sure the compilation is done with the correct version of Java. Plus we also download JDK automatically using coursier both in Metals and Bloop new (moved) cli project, so this should be smooth for most people especially since Scala CLI already does it.