Skip to content

Add support for the --repl-quit-after-init REPL option #3664

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

Merged
Merged
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
19 changes: 7 additions & 12 deletions modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,15 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
extraProps: Map[String, String] = Map.empty
): Unit = {
val isAmmonite = replArtifacts.replMainClass.startsWith("ammonite")
if replArgs.exists(_.noDashPrefixes == ScalacOptions.replInitScript) then
scalaParams.scalaVersion match
case _ if isAmmonite =>
if isAmmonite then
replArgs
.map(_.noDashPrefixes)
.filter(ScalacOptions.replExecuteScriptOptions.contains)
.foreach(arg =>
logger.message(
"The '--repl-init-script' option is not supported with Ammonite. Did you mean to use '--ammonite-arg'?"
s"The '--$arg' option is not supported with Ammonite. Did you mean to use '--ammonite-arg -c' to execute a script?"
)
case s
if s.coursierVersion < "3.6.4-RC1".coursierVersion &&
s.coursierVersion < "3.6.4".coursierVersion &&
s.coursierVersion < "3.6.4-RC1-bin-20250109-a50a1e4-NIGHTLY".coursierVersion =>
logger.message(
"The '--repl-init-script option' is only supported starting with Scala 3.6.4 and onwards."
)
case _ => ()
)
if dryRun then logger.message("Dry run, not running REPL.")
else {
val depClassPathArgs: Seq[String] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ object ScalacOptions {
val YScriptRunnerOption = "Yscriptrunner"
private val scalacOptionsPurePrefixes = Set("V", "W", "X", "Y")
private val scalacOptionsPrefixes = Set("P") ++ scalacOptionsPurePrefixes
val replInitScript = "repl-init-script"
private val replAliasedOptions = Set(replInitScript)
val replExecuteScriptOptions @ Seq(replInitScript, replQuitAfterInit) =
Seq("repl-init-script", "repl-quit-after-init")
private val replAliasedOptions = Set(replInitScript)
private val replNoArgAliasedOptions = Set(replQuitAfterInit)
private val scalacAliasedOptions = // these options don't require being passed after -O and accept an arg
Set(
"bootclasspath",
Expand Down Expand Up @@ -104,7 +106,7 @@ object ScalacOptions {
"preview",
"uniqid",
"unique-id"
)
) ++ replNoArgAliasedOptions

/** This includes all the scalac options which disregard inputs and print a help and/or context
* message instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ abstract class ReplTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
}
}

test("--repl-init-script dry run") {
TestInputs.empty.fromRoot { root =>
val r = os.proc(
TestUtil.cli,
"repl",
extraOptions,
"--repl-init-script",
"println(\"Hello\")",
"--repl-dry-run"
)
.call(cwd = root, stderr = os.Pipe, check = false)
val warningText =
"The '--repl-init-script option' is only supported starting with Scala 3.6.4 and onwards."
val coursierScalaVersion = actualScalaVersion.coursierVersion
val shouldPrintWarning = coursierScalaVersion < "3.6.4".coursierVersion &&
coursierScalaVersion < "3.6.4-RC1".coursierVersion &&
coursierScalaVersion < "3.6.4-RC1-bin-20250109-a50a1e4-NIGHTLY".coursierVersion
if (shouldPrintWarning) expect(r.err.trim().contains(warningText))
else expect(!r.err.trim().contains(warningText))
if (
(actualScalaVersion.startsWith("3.3") &&
actualScalaVersion.coursierVersion >= "3.3.7".coursierVersion) ||
actualScalaVersion.startsWith("3.7") ||
actualScalaVersion.coursierVersion >= "3.7.0-RC1".coursierVersion
)
test("run hello world from the REPL") {
TestInputs.empty.fromRoot { root =>
val expectedMessage = "1337"
val code = s"""println($expectedMessage)"""
val r = os.proc(
TestUtil.cli,
"repl",
"--repl-quit-after-init",
"--repl-init-script",
code,
extraOptions
)
.call(cwd = root)
expect(r.out.trim() == expectedMessage)
}
}
}
}