Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add a consoleScalacOptions target #2948

Merged
merged 4 commits into from
Jan 24, 2024
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
16 changes: 15 additions & 1 deletion docs/modules/ROOT/pages/Scala_Module_Config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,21 @@ for details.

All ``ScalaModule``s have a `console` and a `repl` target, to start a Scala console or an Ammonite Repl.

To use the latter, you can (and sometimes need to) customize the Ammonite version to work with your selected Scala version.
When using the `console`, you can configure its `scalac` options using the `consoleScalacOptions` target.

For example, you may want to inherit all of your regular `scalacOptions` but disable `-Xfatal-warnings`:

.Example: Using `consoleScalacOptions` to disable fatal warnings
[source,scala,subs="attributes,verbatim"]
----
import mill._, scalalib._

object foo extends ScalaModule {
def consoleScalacOptions = scalacOptions().filterNot(o => o == "-Xfatal-warnings")
}
----

To use the `repl`, you can (and sometimes need to) customize the Ammonite version to work with your selected Scala version.
Mill provides a default Ammonite version,
but depending on the Scala version you are using, there may be no matching Ammonite release available.
In order to start the repl, you may have to specify a different available Ammonite version.
Expand Down
8 changes: 7 additions & 1 deletion scalalib/src/mill/scalalib/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>

}

/**
* Command-line options to pass to the Scala console
*/
def consoleScalacOptions: T[Seq[String]] = T(Seq.empty[String])

/**
* Opens up a Scala console with your module and all dependencies present,
* for you to test and operate your code interactively.
Expand All @@ -440,6 +445,7 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>
if (!Util.isInteractive()) {
Result.Failure("console needs to be run with the -i/--interactive flag")
} else {
val useJavaCp = "-usejavacp"
SystemStreams.withStreams(SystemStreams.original) {
Jvm.runSubprocess(
mainClass =
Expand All @@ -452,7 +458,7 @@ trait ScalaModule extends JavaModule with TestModule.ScalaModuleBase { outer =>
),
jvmArgs = forkArgs(),
envArgs = forkEnv(),
mainArgs = Seq("-usejavacp"),
mainArgs = Seq(useJavaCp) ++ consoleScalacOptions().filterNot(Set(useJavaCp)),
workingDir = forkWorkingDir()
)
}
Expand Down