Skip to content

Commit

Permalink
Add a consoleScalacOptions target to ScalaModule (#2948)
Browse files Browse the repository at this point in the history
This will allow users to specify the exact set of `scalac` options that
are used in the `console` target.

Pull request: #2948
  • Loading branch information
mrdziuban committed Jan 24, 2024
1 parent eba9366 commit 3169663
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit 3169663

Please sign in to comment.