Skip to content

Commit

Permalink
Add support for directives in main kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jul 3, 2023
1 parent 5ced09f commit 623bf9a
Show file tree
Hide file tree
Showing 27 changed files with 830 additions and 88 deletions.
32 changes: 28 additions & 4 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ class ScalaInterpreter(val crossScalaVersion: String) extends AlmondModule with
shared.interpreter(ScalaVersions.scala3Compat),
scala.`coursier-logger`(ScalaVersions.scala3Compat),
scala.`scala-kernel-api-helper`(),
scala.`shared-directives`(ScalaVersions.scala3Compat),
scala.`toree-hooks`(ScalaVersions.binary(crossScalaVersion))
)
else
Seq(
shared.interpreter(),
scala.`coursier-logger`(),
scala.`scala-kernel-api`(),
scala.`shared-directives`(),
scala.`toree-hooks`(ScalaVersions.binary(crossScalaVersion))
)
def ivyDeps = T {
Expand All @@ -219,11 +221,19 @@ class ScalaInterpreter(val crossScalaVersion: String) extends AlmondModule with
metabrowse ++ Agg(
Deps.coursier.withDottyCompat(crossScalaVersion),
Deps.coursierApi,
Deps.dependencyInterface,
Deps.directiveHandler,
Deps.jansi,
Deps.ammoniteCompiler(crossScalaVersion).exclude(("net.java.dev.jna", "jna")),
Deps.ammoniteRepl(crossScalaVersion).exclude(("net.java.dev.jna", "jna"))
)
}
def scalacOptions = super.scalacOptions() ++ {
val scala213Options =
if (scalaVersion().startsWith("2.13.")) Seq("-Ymacro-annotations")
else Nil
scala213Options
}
object test extends Tests with AlmondTestModule {
def moduleDeps = {
val rx =
Expand Down Expand Up @@ -369,19 +379,30 @@ class CoursierLogger(val crossScalaVersion: String) extends AlmondModule {
)
}

class SharedDirectives(val crossScalaVersion: String) extends AlmondModule {
def supports3 = true
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.directiveHandler,
Deps.jsoniterScalaCore.applyBinaryVersion213_3(scalaVersion())
)
def compileIvyDeps = Agg(
Deps.jsoniterScalaMacros
)
}

trait Launcher extends AlmondSimpleModule with BootstrapLauncher with PropertyFile
with Bloop.Module {
def supports3 = true
private def sv = ScalaVersions.scala3Latest
def scalaVersion = sv
def moduleDeps = Seq(
scala.`coursier-logger`(ScalaVersions.scala3Compat),
scala.`shared-directives`(ScalaVersions.scala3Compat),
shared.kernel(ScalaVersions.scala3Compat)
)
def ivyDeps = Agg(
Deps.caseApp,
Deps.coursierLauncher,
Deps.directiveHandler,
Deps.fansi,
Deps.scalaparse
)
Expand Down Expand Up @@ -476,9 +497,11 @@ object scala extends Module {
object `scala-kernel-helper`
extends Cross[ScalaKernelHelper](ScalaVersions.all.filter(_.startsWith("3.")): _*)
object `coursier-logger` extends Cross[CoursierLogger](ScalaVersions.binaries: _*)
object launcher extends Launcher
object `almond-scalapy` extends Cross[AlmondScalaPy](ScalaVersions.binaries: _*)
object `almond-rx` extends Cross[AlmondRx](ScalaVersions.scala212, ScalaVersions.scala213)
object `shared-directives`
extends Cross[SharedDirectives]("2.12.15" +: ScalaVersions.binaries: _*)
object launcher extends Launcher
object `almond-scalapy` extends Cross[AlmondScalaPy](ScalaVersions.binaries: _*)
object `almond-rx` extends Cross[AlmondRx](ScalaVersions.scala212, ScalaVersions.scala213)

object `toree-hooks` extends Cross[ToreeHooks](ScalaVersions.binaries: _*)

Expand Down Expand Up @@ -539,6 +562,7 @@ class KernelLocalRepo(val testScalaVersion: String) extends LocalRepo {
scala.`scala-interpreter`(testScalaVersion),
scala.`toree-hooks`(ScalaVersions.binary(testScalaVersion)),
scala.`coursier-logger`(ScalaVersions.binary(testScalaVersion)),
scala.`shared-directives`(ScalaVersions.binary(testScalaVersion)),
scala.launcher,
shared.kernel(ScalaVersions.binary(ScalaVersions.scala3Latest)),
shared.interpreter(ScalaVersions.binary(ScalaVersions.scala3Latest)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ abstract class KernelTestsDefinitions extends AlmondFunSuite {
}
}

test("add dependency") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
almond.integration.Tests.addDependency()
}
}

test("add repository") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
almond.integration.Tests.addRepository()
}
}

test("add scalac option") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
almond.integration.Tests.addScalacOption()
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,185 @@
package almond.integration

import almond.testkit.Dsl._

class KernelTestsTwoStepStartup213 extends KernelTestsDefinitions {

lazy val kernelLauncher =
new KernelLauncher(KernelLauncher.LauncherType.Jvm, KernelLauncher.testScala213Version)

test("mixed directives") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
runner.withSession() { implicit session =>

execute(
"""//> using scala "2.13.11"
|2
|""".stripMargin,
"res1: Int = 2"
)
execute(
"""//> using option "-deprecation" "-Xfatal-warnings"
|
|@deprecated
|def foo() = 2
|
|foo()
|""".stripMargin,
expectError = true,
stderr =
"""cell2.sc:4: method foo in class Helper is deprecated
|val res2_1 = foo()
| ^
|No warnings can be incurred under -Werror.
|Compilation Failed""".stripMargin,
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
)
)
}
}
}

test("mixed directives in first cell") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
runner.withSession() { implicit session =>

execute(
"""//> using scala "2.13.11"
|//> using option "-deprecation" "-Xfatal-warnings"
|2
|""".stripMargin,
"res1: Int = 2"
)
execute(
"""@deprecated
|def foo() = 2
|
|foo()
|""".stripMargin,
expectError = true,
stderr =
"""cell2.sc:4: method foo in class Helper is deprecated
|val res2_1 = foo()
| ^
|No warnings can be incurred under -Werror.
|Compilation Failed""".stripMargin,
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
)
)
}
}
}

test("mixed directives single cell") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
runner.withSession() { implicit session =>

execute(
"""//> using scala "2.13.11"
|//> using option "-deprecation" "-Xfatal-warnings"
|
|@deprecated
|def foo() = 2
|
|foo()
|""".stripMargin,
expectError = true,
stderr =
"""cell1.sc:4: method foo in class Helper is deprecated
|val res1_1 = foo()
| ^
|No warnings can be incurred under -Werror.
|Compilation Failed""".stripMargin,
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
)
)
}
}
}

test("mixed directives several kernel options") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
runner.withSession() { implicit session =>

execute(
"""//> using scala "2.12.18"
|//> using option "-Xfatal-warnings"
|""".stripMargin,
""
)

execute(
"""//> using option "-deprecation"
|
|@deprecated
|def foo() = 2
|
|foo()
|""".stripMargin,
expectError = true,
stderr =
"""cell2.sc:4: method foo in class Helper is deprecated
|val res2_1 = foo()
| ^
|No warnings can be incurred under -Xfatal-warnings.
|Compilation Failed""".stripMargin,
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
)
)
}
}
}

test("late launcher directives") {
kernelLauncher.withKernel { implicit runner =>
implicit val sessionId: SessionId = SessionId()
runner.withSession() { implicit session =>

execute(
"""//> using scala "2.12.18"
|//> using option "-Xfatal-warnings"
|""".stripMargin,
""
)

execute(
"""//> using option "-deprecation"
|
|@deprecated
|def foo() = 2
|
|foo()
|""".stripMargin,
expectError = true,
stderr =
"""cell2.sc:4: method foo in class Helper is deprecated
|val res2_1 = foo()
| ^
|No warnings can be incurred under -Xfatal-warnings.
|Compilation Failed""".stripMargin,
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
)
)

execute(
"""//> using scala "2.13.11"""",
"",
stderr =
"""Warning: ignoring 1 directive(s) that can only be used prior to any code:
| //> using scala
|""".stripMargin
)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package almond.launcher
import almond.channels.{Channel, Connection, Message => RawMessage}
import almond.channels.zeromq.ZeromqThreads
import almond.cslogger.NotebookCacheLogger
import almond.directives.KernelOptions
import almond.interpreter.ExecuteResult
import almond.interpreter.api.{DisplayData, OutputHandler}
import almond.kernel.install.Install
import almond.kernel.{Kernel, KernelThreads, MessageFile}
import almond.launcher.directives.LauncherParameters
import almond.logger.{Level, LoggerContext}
import almond.protocol.{Execute, RawJson}
import almond.util.ThreadUtil.singleThreadedExecutionContext
import caseapp.core.RemainingArgs
import caseapp.core.app.CaseApp
import cats.effect.IO
import cats.effect.unsafe.IORuntime
import com.github.plokhotnyuk.jsoniter_scala.core._
import coursier.launcher.{BootstrapGenerator, ClassLoaderContent, ClassPathEntry, Parameters}
import dependency.ScalaParameters

Expand All @@ -33,6 +36,7 @@ object Launcher extends CaseApp[LauncherOptions] {
options: LauncherOptions,
noExecuteInputFor: Seq[String],
params0: LauncherParameters,
kernelOptions: KernelOptions,
outputHandler: OutputHandler,
logCtx: LoggerContext
): (os.proc, String, Option[String]) = {
Expand Down Expand Up @@ -149,6 +153,15 @@ object Launcher extends CaseApp[LauncherOptions] {
Seq("--no-execute-input-for", id)
}

val optionsArgs =
if (kernelOptions.isEmpty) Nil
else {
val asJson = KernelOptions.AsJson(kernelOptions)
val bytes = writeToArray(asJson)(KernelOptions.AsJson.codec)
val optionsFile = os.temp(bytes, prefix = "almond-options-", suffix = ".json")
Seq[os.Shellable]("--kernel-options", optionsFile)
}

val jvmIdOpt = params0.jvm.filter(_.trim.nonEmpty)
val javaCommand = jvmIdOpt match {
case Some(jvmId) =>
Expand Down Expand Up @@ -183,6 +196,7 @@ object Launcher extends CaseApp[LauncherOptions] {
currentCellCount,
msgFileArgs,
noExecuteInputArgs,
optionsArgs,
options.kernelOptions
)

Expand Down Expand Up @@ -359,6 +373,7 @@ object Launcher extends CaseApp[LauncherOptions] {
options,
firstMessageIdOpt.toSeq,
interpreter.params,
interpreter.kernelOptions,
outputHandlerOpt.getOrElse(OutputHandler.NopOutputHandler),
logCtx
)
Expand Down
Loading

0 comments on commit 623bf9a

Please sign in to comment.