Skip to content

Commit

Permalink
Merge pull request #1357 from scala-steward/update/ammonite-compiler-…
Browse files Browse the repository at this point in the history
…3.0.0-M2-1-3763a1d4

Update Ammonite-compiler, ammonite-repl, ... to 3.0.0-M2-1-3763a1d4
  • Loading branch information
alexarchambault authored May 29, 2024
2 parents 4c91fe7 + 1023fda commit 1d03567
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 84 deletions.
9 changes: 8 additions & 1 deletion modules/echo/src/main/scala/almond/echo/EchoKernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ object EchoKernel extends CaseApp[Options] {

log.debug("Running kernel")
Kernel.create(new EchoInterpreter, interpreterEc, kernelThreads, logCtx)
.flatMap(_.runOnConnectionFile(connectionFile, "echo", zeromqThreads, Nil, autoClose = true))
.flatMap(_.runOnConnectionFile(
connectionFile,
"echo",
zeromqThreads,
Nil,
autoClose = true,
lingerDuration = options.lingerDuration
))
.unsafeRunSync()(IORuntime.global)
}
}
23 changes: 20 additions & 3 deletions modules/echo/src/main/scala/almond/echo/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,32 @@ import almond.kernel.install.{Options => InstallOptions}
import caseapp.{HelpMessage, Recurse}
import caseapp.core.help.Help
import caseapp.core.parser.Parser
import caseapp.Hidden
import scala.concurrent.duration.{Duration, DurationInt}

// format: off
final case class Options(
connectionFile: Option[String] = None,
@HelpMessage("Log level (one of none, error, warn, info, or debug)")
log: String = "warn",
log: String = "warn",
install: Boolean = false,
@Recurse
installOptions: InstallOptions = InstallOptions()
)
installOptions: InstallOptions = InstallOptions(),

@HelpMessage(
"""Time given to the client to accept ZeroMQ messages before exiting. Parsed with scala.concurrent.duration.Duration, this accepts things like "Inf" or "5 seconds""""
)
@Hidden
linger: Option[String] = None
) {
// format: on

lazy val lingerDuration = linger
.map(_.trim)
.filter(_.nonEmpty)
.map(Duration(_))
.getOrElse(5.seconds)
}

object Options {
implicit lazy val parser: Parser[Options] = Parser.derive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ abstract class AlmondFunSuite extends munit.FunSuite {
def mightRetry: Boolean = false
override def munitTimeout = 5.minutes

override def test(options: TestOptions)(body: => Any)(implicit loc: Location): Unit =
override def test(options: TestOptions)(body: => Any)(implicit loc: Location): Unit = {
val className = getClass.getName
val (classNameInit, classNameLast) = {
val a = className.split('.')
(a.init, a.last)
}
super.test(options) {

def runBody(attempt: Int): Any = {
Expand All @@ -33,7 +38,9 @@ abstract class AlmondFunSuite extends munit.FunSuite {
if (attempt == 1)
AlmondFunSuite.retriedTestsCount.incrementAndGet()
System.err.println(
s"Attempt $attempt of ${Console.RED}${options.name}${Console.RESET} failed, trying again"
s"Attempt $attempt of ${Console.RED}${classNameInit.mkString(".")}" + "." +
s"${Console.BOLD}$classNameLast${Console.RESET}${Console.RED}" + "." +
s"${Console.BOLD}${options.name}${Console.RESET} failed, trying again"
)
e.printStackTrace(System.err)
runBody(attempt + 1)
Expand All @@ -44,7 +51,11 @@ abstract class AlmondFunSuite extends munit.FunSuite {
}

System.err.println()
System.err.println(s"Running ${Console.BLUE}${options.name}${Console.RESET}")
System.err.println(
s"${Console.BLUE}Running ${classNameInit.mkString(".")}" + "." +
s"${Console.BOLD}$classNameLast${Console.RESET}${Console.BLUE}" + "." +
s"${Console.BOLD}${options.name}${Console.RESET}"
)
var success = false
var exOpt = Option.empty[Throwable]
try {
Expand All @@ -58,15 +69,23 @@ abstract class AlmondFunSuite extends munit.FunSuite {
}
finally {
if (success)
System.err.println(s"Done: ${Console.CYAN}${options.name}${Console.RESET}")
System.err.println(
s"${Console.CYAN}Done: ${classNameInit.mkString(".")}" + "." +
s"${Console.BOLD}$classNameLast${Console.RESET}${Console.CYAN}" + "." +
s"${Console.BOLD}${options.name}${Console.RESET}"
)
else {
System.err.println(s"Failed: ${Console.RED}${options.name}${Console.RESET}")
System.err.println(
s"${Console.RED}Failed: ${classNameInit.mkString(".")}" + "." +
s"${Console.BOLD}$classNameLast${Console.RESET}${Console.RED}" + "." +
s"${Console.BOLD}${options.name}${Console.RESET}"
)
exOpt.foreach(_.printStackTrace(System.err))
}
System.err.println()
}
}(loc)

}
}

object AlmondFunSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ class KernelLauncher(
}

def close(): Unit = {
conn.close(partial = false).unsafeRunTimed(2.minutes)(IORuntime.global).getOrElse {
sys.error("Timeout when closing ZeroMQ connections")
}
conn.close(partial = false, lingerDuration = 30.seconds)
.unsafeRunTimed(2.minutes)(IORuntime.global)
.getOrElse {
sys.error("Timeout when closing ZeroMQ connections")
}

if (perTestZeroMqContext) {
val t = stackTracePrinterThread(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ abstract class KernelTestsDefinitions extends AlmondFunSuite {

override def mightRetry = true

lazy val javaMajorVersion = {
val versionString =
sys.props.getOrElse("java.version", sys.error("java.version property not set"))
.stripPrefix("1.") // for Java 8 and below
versionString.takeWhile(_ != '.').toInt
}

test("jvm-repr") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
Expand Down Expand Up @@ -102,7 +109,7 @@ abstract class KernelTestsDefinitions extends AlmondFunSuite {
}
}

if (kernelLauncher.defaultScalaVersion.startsWith("2."))
if (kernelLauncher.defaultScalaVersion.startsWith("2.") && javaMajorVersion >= 11)
test("inspections") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import java.nio.channels.ClosedSelectorException
import scala.concurrent.duration.DurationInt
import scala.jdk.CollectionConverters._
import scala.util.control.NonFatal
import scala.concurrent.duration.Duration

object Launcher extends CaseApp[LauncherOptions] {

Expand Down Expand Up @@ -325,7 +326,8 @@ object Launcher extends CaseApp[LauncherOptions] {
"scala",
zeromqThreads,
Nil,
autoClose = false
autoClose = false,
lingerDuration = Duration.Inf // unused here
))
.unsafeRunSync()(IORuntime.global)
val leftoverMessages: Seq[(Channel, RawMessage)] = run.unsafeRunSync()(IORuntime.global)
Expand Down Expand Up @@ -410,7 +412,9 @@ object Launcher extends CaseApp[LauncherOptions] {
for (outputHandler <- outputHandlerOpt)
outputHandler.done()

try conn.close(partial = false).unsafeRunSync()(IORuntime.global)
try
conn.close(partial = false, lingerDuration = options.lingerDuration)
.unsafeRunSync()(IORuntime.global)
catch {
case NonFatal(e) =>
throw new Exception(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import caseapp._

import scala.cli.directivehandler.EitherSequence._
import scala.collection.mutable
import scala.concurrent.duration.{Duration, DurationInt}

// format: off
final case class LauncherOptions(
Expand Down Expand Up @@ -34,7 +35,10 @@ final case class LauncherOptions(
quiet: Option[Boolean] = None,
silentImports: Option[Boolean] = None,
useNotebookCoursierLogger: Option[Boolean] = None,
customDirectiveGroup: List[String] = Nil
customDirectiveGroup: List[String] = Nil,
@HelpMessage("Time given to the client to accept ZeroMQ messages before handing over the connections to the kernel. Parsed with scala.concurrent.duration.Duration, this accepts things like \"Inf\" or \"5 seconds\"")
@Hidden
linger: Option[String] = None
) {
// format: on

Expand Down Expand Up @@ -91,6 +95,12 @@ final case class LauncherOptions(
groups
}
}

lazy val lingerDuration = linger
.map(_.trim)
.filter(_.nonEmpty)
.map(Duration(_))
.getOrElse(5.seconds)
}

object LauncherOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ final class ScalaInterpreterInspections(
try
docs.documentation(
sym,
() => symbol.allOverriddenSymbols.map(_.toSemantic).toList.asJava
() => symbol.allOverriddenSymbols.map(_.toSemantic).toList.asJava,
scala.meta.pc.ContentType.MARKDOWN
)
catch {
case e: IndexingExceptions.InvalidSymbolException =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ final class ScalaInterpreter(
if (params.extraClassPath.nonEmpty)
frames0().head.addClasspath(params.extraClassPath.map(_.toNIO.toUri.toURL))

private val inspections = new ScalaInterpreterInspections(
logCtx,
params.metabrowse,
params.metabrowseHost,
params.metabrowsePort,
ammonite.compiler.CompilerBuilder.scalaVersion,
ammInterp
.compilerManager
.asInstanceOf[ammonite.compiler.CompilerLifecycleManager],
frames0()
)
private var inspectionsInitialized = false
private lazy val inspections = {
val value = new ScalaInterpreterInspections(
logCtx,
params.metabrowse,
params.metabrowseHost,
params.metabrowsePort,
ammonite.compiler.CompilerBuilder.scalaVersion,
ammInterp
.compilerManager
.asInstanceOf[ammonite.compiler.CompilerLifecycleManager],
frames0()
)
inspectionsInitialized = true
value
}

private val colors0: Ref[Colors] = Ref(params.initialColors)

Expand Down Expand Up @@ -306,7 +311,8 @@ final class ScalaInterpreter(
case NonFatal(e) =>
log.warn("Caught exception while trying to run exit hooks", e)
}
inspections.shutdown()
if (inspectionsInitialized)
inspections.shutdown()
}

}
46 changes: 28 additions & 18 deletions modules/scala/scala-kernel/src/main/scala/almond/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import coursierapi.{Dependency, Module}
import coursier.parse.{DependencyParser, ModuleParser}

import scala.collection.compat._
import scala.concurrent.duration.{Duration, DurationInt}
import scala.jdk.CollectionConverters._

// format: off
@ProgName("almond")
final case class Options(
install: Boolean = false,
@Recurse
installOptions: InstallOptions = InstallOptions(),
installOptions: InstallOptions = InstallOptions(),
extraRepository: List[String] = Nil,
banner: Option[String] = None,
link: List[String] = Nil,
Expand All @@ -35,50 +36,50 @@ final case class Options(
defaultAutoDependencies: Boolean = true,
defaultAutoVersions: Boolean = true,
@HelpMessage("Default almond-spark version to load when Spark is loaded as a library")
defaultAlmondSparkVersion: Option[String] = None,
defaultAlmondSparkVersion: Option[String] = None,
@HelpMessage("Default almond-scalapy version to load when ScalaPy is loaded as a library")
defaultAlmondScalapyVersion: Option[String] = None,
defaultAlmondScalapyVersion: Option[String] = None,
@HelpMessage("Force Maven properties during dependency resolution")
forceProperty: List[String] = Nil,
forceProperty: List[String] = Nil,
@HelpMessage("Enable Maven profile (start with ! to disable)")
profile: List[String] = Nil,
profile: List[String] = Nil,
@HelpMessage("Log level (one of none, error, warn, info, debug)")
log: Option[String] = None,
log: Option[String] = None,
@HelpMessage("Send log to a file rather than stderr")
@ValueDescription("/path/to/log-file")
logTo: Option[String] = None,
logTo: Option[String] = None,
connectionFile: Option[String] = None,
// For class loader isolation, the user code is loaded from the classloader of the api module.
// If the right -i / -I options are passed to coursier bootstrap when generating a launcher, that loader
// only sees the api module and its dependencies, rather than the full classpath of almond.
@HelpMessage("Use class loader that loaded the api module rather than the context class loader")
specificLoader: Boolean = true,
specificLoader: Boolean = true,
@HelpMessage(
"Start a metabrowse server for go to source navigation (linked from Jupyter inspections, server is started upon first inspection)"
)
metabrowse: Boolean = false,
metabrowse: Boolean = false,
@HelpMessage("Trap what user code sends to stdout and stderr")
trapOutput: Boolean = false,
trapOutput: Boolean = false,
@HelpMessage("If false, duplicates stdout/stderr to console, similar to IPKernelApp.quiet")
quiet: Boolean = true,
quiet: Boolean = true,
@HelpMessage("Disable ammonite compilation cache")
disableCache: Boolean = false,
disableCache: Boolean = false,
@HelpMessage("Whether to automatically update lazy val-s upon computation")
autoUpdateLazyVals: Boolean = true,
autoUpdateLazyVals: Boolean = true,
@HelpMessage("Whether to automatically update var-s upon change")
autoUpdateVars: Boolean = true,
autoUpdateVars: Boolean = true,
@HelpMessage("Whether to silence imports (not printing them back in output)")
silentImports: Boolean = false,
@HelpMessage("Whether to use a notebook-specific coursier logger")
useNotebookCoursierLogger: Boolean = false,
@HelpMessage("Whether to enable variable inspector")
variableInspector: Option[Boolean] = None,
variableInspector: Option[Boolean] = None,
@HelpMessage("Whether to process format requests with scalafmt")
scalafmt: Boolean = true,
scalafmt: Boolean = true,
@HelpMessage(
"Whether to use 'Thread.interrupt' method or deprecated 'Thread.stop' method (default) when interrupting kernel."
)
useThreadInterrupt: Boolean = false,
useThreadInterrupt: Boolean = false,

@ExtraName("outputDir")
outputDirectory: Option[String] = None,
Expand Down Expand Up @@ -129,7 +130,11 @@ final case class Options(

@HelpMessage("Pass launcher directive groups with this option. These directives will be either ignored (see --ignore-launcher-directives-in), or trigger an unused directive warning")
@Hidden
launcherDirectiveGroup: List[String] = Nil
launcherDirectiveGroup: List[String] = Nil,

@HelpMessage("""Time given to the client to accept ZeroMQ messages before exiting. Parsed with scala.concurrent.duration.Duration, this accepts things like "Inf" or "5 seconds"""")
@Hidden
linger: Option[String] = None
) {
// format: on

Expand Down Expand Up @@ -300,6 +305,11 @@ final case class Options(
readFromArray(bytes)(KernelOptions.AsJson.codec)
}

lazy val lingerDuration = linger
.map(_.trim)
.filter(_.nonEmpty)
.map(Duration(_))
.getOrElse(5.seconds)
}

object Options {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ object ScalaKernel extends CaseApp[Options] {
"scala",
zeromqThreads,
options.leftoverMessages0(),
autoClose = true
autoClose = true,
lingerDuration = options.lingerDuration
))
.unsafeRunSync()(IORuntime.global)
finally
Expand Down
Loading

0 comments on commit 1d03567

Please sign in to comment.