Skip to content

Enable direct usage of --repl-init-script with Scala REPL >= 3.6.4-RC1 #3447

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 1 commit into from
Jan 21, 2025
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
28 changes: 22 additions & 6 deletions modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ai.kien.python.Python
import caseapp.*
import caseapp.core.help.HelpFormat
import coursier.cache.FileCache
import coursier.core.Version
import coursier.error.{FetchError, ResolutionError}
import dependency.*

Expand All @@ -20,6 +21,7 @@ import scala.build.errors.{
}
import scala.build.input.Inputs
import scala.build.internal.{Constants, Runner}
import scala.build.options.ScalacOpt.noDashPrefixes
import scala.build.options.{BuildOptions, JavaOpt, MaybeScalaVersion, Scope}
import scala.cli.commands.publish.ConfigUtil.*
import scala.cli.commands.run.Run.{
Expand All @@ -28,14 +30,14 @@ import scala.cli.commands.run.Run.{
pythonPathEnv
}
import scala.cli.commands.run.RunMode
import scala.cli.commands.shared.{HelpCommandGroup, HelpGroup, SharedOptions}
import scala.cli.commands.shared.{HelpCommandGroup, HelpGroup, ScalacOptions, SharedOptions}
import scala.cli.commands.util.BuildCommandHelpers
import scala.cli.commands.{ScalaCommand, WatchUtil}
import scala.cli.config.{ConfigDb, Keys}
import scala.cli.packaging.Library
import scala.cli.util.ArgHelpers.*
import scala.cli.util.ConfigDbUtils
import scala.cli.{CurrentParams, ScalaCli}
import scala.cli.{CurrentParams, ScalaCli, coursierVersion}
import scala.jdk.CollectionConverters.*
import scala.util.Properties

Expand Down Expand Up @@ -390,11 +392,24 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
replArgs: Seq[String],
extraEnv: Map[String, String] = Map.empty,
extraProps: Map[String, String] = Map.empty
): Unit =
if (dryRun)
logger.message("Dry run, not running REPL.")
): Unit = {
val isAmmonite = replArtifacts.replMainClass.startsWith("ammonite")
if replArgs.exists(_.noDashPrefixes == ScalacOptions.replInitScript) then
scalaParams.scalaVersion match
case _ if isAmmonite =>
logger.message(
"The '--repl-init-script' option is not supported with Ammonite. Did you mean to use '--ammonite-arg'?"
)
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 isAmmonite = replArtifacts.replMainClass.startsWith("ammonite")
val depClassPathArgs: Seq[String] =
if replArtifacts.depsClassPath.nonEmpty && !isAmmonite then
Seq(
Expand Down Expand Up @@ -422,6 +437,7 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
if (retCode != 0)
value(Left(new ReplError(retCode)))
}
}

def defaultArtifacts(): Either[BuildException, ReplArtifacts] = either {
value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ 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)
private val scalacAliasedOptions = // these options don't require being passed after -O and accept an arg
Set(
"coverage-exclude-classlikes",
Expand All @@ -61,7 +63,7 @@ object ScalacOptions {
"target",
"source",
YScriptRunnerOption
)
) ++ replAliasedOptions
private val scalacNoArgAliasedOptions = // these options don't require being passed after -O and don't accept an arg
Set(
"experimental",
Expand Down
7 changes: 7 additions & 0 deletions modules/cli/src/main/scala/scala/cli/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scala

import coursier.core.Version

package object cli {
extension (s: String) def coursierVersion: Version = Version(s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,26 @@ abstract class ReplTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
expect(res.exitCode == 0)
}
}

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))
}
}
}
57 changes: 57 additions & 0 deletions website/docs/commands/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,63 @@ scala> :quit

</ChainedSnippets>

## Passing REPL options
It is also possible to manually pass REPL-specific options.
It can be done in a couple ways:
- after the `--` separator, as the REPL itself is the launched app, so its options are app arguments

<ChainedSnippets>

```bash ignore
scala repl -S 3.6.4-RC1 -- --repl-init-script 'println("Hello")'
```

```
Hello
Welcome to Scala 3.6.4-RC1 (23.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala>
```
</ChainedSnippets>


- with the `-O`, effectively passing them as compiler options:

<ChainedSnippets>

```bash ignore
scala repl -S 3.6.4-RC1 -O --repl-init-script -O 'println("Hello")'
```

```
Hello
Welcome to Scala 3.6.4-RC1 (23.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala>
```

</ChainedSnippets>

- directly, as a Scala CLI option (do note that newly added options from an RC version or a snapshot may not be supported this way just yet):

<ChainedSnippets>

```bash ignore
scala repl -S 3.6.4-RC1 --repl-init-script 'println("Hello")'
```

```
Hello
Welcome to Scala 3.6.4-RC1 (23.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala>
```

</ChainedSnippets>

## Using Toolkit in REPL
It is also possible to start the scala-cli REPL with [toolkit](https://scala-cli.virtuslab.org/docs/guides/introduction/toolkit/) enabled

Expand Down
Loading