Skip to content
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

Fix #21242: Add REPL flag to quit after evaluating init script #22636

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/repl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >& /dev/null && pwd)/.."
. $ROOT/bin/commonQ

java -Dscala.usejavacp=true -cp $cp dotty.tools.repl.Main -usejavacp "$@"
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ trait CommonScalaSettings:
val usejavacp: Setting[Boolean] = BooleanSetting(RootSetting, "usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path"))
val scalajs: Setting[Boolean] = BooleanSetting(RootSetting, "scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs"))
val replInitScript: Setting[String] = StringSetting(RootSetting, "repl-init-script", "code", "The code will be run on REPL startup.", "", aliases = List("--repl-init-script"))
val replEvalOnly: Setting[Boolean] = BooleanSetting(RootSetting, "repl-eval", "Quit REPL after evaluating the init script.", aliases = List("--repl-eval"))
end CommonScalaSettings

/** -P "plugin" settings. Various tools might support plugins. */
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class ReplDriver(settings: Array[String],
*
* Possible reason for unsuccessful run are raised flags in CLI like --help or --version
*/
final def tryRunning = if shouldStart then runUntilQuit()
final def tryRunning = if shouldStart then
if rootCtx.settings.replEvalOnly.value(using rootCtx) then initialState
else runUntilQuit()

/** Run REPL with `state` until `:quit` command found
*
Expand Down
5 changes: 4 additions & 1 deletion compiler/test/dotty/tools/scripting/BashExitCodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ class BashExitCodeTests:
s"expected $expectedExitCode but got $exitCode${pp("out", stdout)}${pp("err", stderr)}"
}, expectedExitCode, exitCode)

// Helpers for running scala, scalac, and scalac without the output directory ("raw")
// Helpers for running scala, scalac, scalac, and repl without the output directory ("raw")
def scala(args: String*) = verifyExit(scalaPath, ("--power" +: args :+ "--offline" :+ "--server=false")*)
def scalacRaw(args: String*) = verifyExit(scalacPath, args*)
def scalac(args: String*) = scalacRaw(("-d" +: tmpDir +: args)*)
def repl(args: String*) = verifyExit(replPath, args*)

/** The path to the test file for this class. */
def f(body: String, suffix: String = ".scala"): String =
Expand Down Expand Up @@ -72,6 +73,8 @@ class BashExitCodeTests:
@Test def xPluginList = scala("-Xplugin-list")(0)
@Test def vPhases = scala("-Vphases")(0)

@Test def replEval = repl("--repl-eval", "--repl-init-script", "println(\"Hello from init script!\"); val i = 2 * 2")(0)

/** A utility for running two commands in a row, like you do in bash. */
extension (inline u1: Unit) inline def & (inline u2: Unit): Unit = { u1; u2 }
end BashExitCodeTests
5 changes: 3 additions & 2 deletions compiler/test/dotty/tools/scripting/ScriptTestEnv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,11 @@ object ScriptTestEnv {

lazy val cwd: Path = Paths.get(".").toAbsolutePath.normalize

lazy val (scalacPath: String, scalaPath: String) = {
lazy val (scalacPath: String, scalaPath: String, replPath: String) = {
val scalac = s"$workingDirectory/$packBinDir/scalac".toPath.normalize
val scala = s"$workingDirectory/$packBinDir/scala".toPath.normalize
(scalac.norm, scala.norm)
val repl = s"$workingDirectory/$packBinDir/repl".toPath.normalize
(scalac.norm, scala.norm, repl.norm)
}


Expand Down
Loading