Skip to content

Add warnings for the deprecated -Yscriptrunner legacy scala runner option instead of passing it to scalac #1804

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 26, 2023
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 modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import scala.build.EitherCps.{either, value}
import scala.build.compiler.SimpleScalaCompiler
import scala.build.errors.BuildException
import scala.build.internal.{Constants, Runner}
import scala.build.options.{BuildOptions, Scope}
import scala.build.options.{BuildOptions, ScalacOpt, Scope}
import scala.build.{Artifacts, Logger, Positioned, ReplArtifacts}
import scala.cli.commands.default.LegacyScalaOptions
import scala.cli.commands.shared.{HasLoggingOptions, ScalaCliHelp, ScalacOptions, SharedOptions}
import scala.cli.commands.util.CommandHelpers
import scala.cli.commands.util.ScalacOptionsUtil.*
Expand Down Expand Up @@ -133,6 +134,18 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]
for (shared <- sharedOptions(options))
shared.helpGroups.maybePrintGroupHelp(help, helpFormat)

private def maybePrintWarnings(options: T): Unit = {
import scala.cli.commands.shared.ScalacOptions.YScriptRunnerOption
val logger = options.logging.logger
sharedOptions(options).foreach { so =>
val scalacOpts = so.scalac.scalacOption.toScalacOptShadowingSeq
if scalacOpts.keys.contains(ScalacOpt(YScriptRunnerOption)) then
logger.message(
LegacyScalaOptions.yScriptRunnerWarning(scalacOpts.getOption(YScriptRunnerOption))
)
}
}

/** Print `scalac` output if passed options imply no inputs are necessary and raw `scalac` output
* is required instead. (i.e. `--scalac-option -help`)
* @param options
Expand Down Expand Up @@ -265,6 +278,7 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]
*/
final override def run(options: T, remainingArgs: RemainingArgs): Unit = {
CurrentParams.verbosity = options.logging.verbosity
maybePrintWarnings(options)
maybePrintGroupHelp(options)
buildOptions(options).foreach { bo =>
maybePrintSimpleScalacOutput(options, bo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import caseapp.core.Indexed

import scala.build.Logger
import scala.cli.ScalaCli
import scala.cli.ScalaCli.fullRunnerName
import scala.cli.ScalaCli.{fullRunnerName, progName}
import scala.cli.commands.bloop.BloopExit
import scala.cli.commands.default.LegacyScalaOptions.*
import scala.cli.commands.package0.Package
import scala.cli.commands.shared.ScalacOptions.YScriptRunnerOption
import scala.cli.commands.tags

/** Options covering backwards compatibility with the old scala runner.
Expand Down Expand Up @@ -164,4 +166,26 @@ object LegacyScalaOptions {

private[default] lazy val PowerString =
if ScalaCli.allowRestrictedFeatures then "" else "--power "

def yScriptRunnerWarning(yScriptRunnerValue: Option[String]): String = {
val valueSpecificMsg = yScriptRunnerValue match {
case Some(v @ "default") =>
s"scala.tools.nsc.DefaultScriptRunner (the $v script runner) is no longer available."
case Some(v @ "resident") =>
s"scala.tools.nsc.fsc.ResidentScriptRunner (the $v script runner) is no longer available."
case Some(v @ "shutdown") =>
val bloopExitCommandName =
BloopExit.names.headOption.map(_.mkString(" ")).getOrElse(BloopExit.name)
s"""scala.tools.nsc.fsc.DaemonKiller (the $v script runner) is no longer available.
|Did you want to stop the $fullRunnerName build server (Bloop) instead?
|If so, consider using the following command:
| ${Console.BOLD}$progName $PowerString$bloopExitCommandName${Console.RESET}""".stripMargin
case Some(className) =>
s"Using $className as the script runner is no longer supported and will not be attempted."
case _ => ""
}
s"""Deprecated option '$YScriptRunnerOption' is ignored.
|The script runner can no longer be picked as before.
|$valueSpecificMsg""".stripMargin
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ object ScalacOptions {
origin = Some("ScalacOptions")
)
// .withIsFlag(true) // The scalac options we handle accept no value after the -… argument
val YScriptRunnerOption = "-Yscriptrunner"
private val scalacOptionsPurePrefixes =
Set("-V", "-W", "-X", "-Y")
private val scalacOptionsPrefixes =
Set("-g", "-language", "-opt", "-P", "-target", "-source") ++ scalacOptionsPurePrefixes
private val scalacAliasedOptions = // these options don't require being passed after -O and accept an arg
Set("-encoding", "-release", "-color")
Set("-encoding", "-release", "-color", YScriptRunnerOption)
private val scalacNoArgAliasedOptions = // these options don't require being passed after -O and don't accept an arg
Set(
"-nowarn",
Expand All @@ -64,6 +65,9 @@ object ScalacOptions {
"-classpath", // redirected to --extra-jars
"-d" // redirected to --compilation-output
)
val ScalacDeprecatedOptions: Set[String] = Set(
YScriptRunnerOption // old 'scala' runner specific, no longer supported
)

private val scalacOptionsArgument: Argument[List[String]] =
new Argument[List[String]] {
Expand All @@ -81,7 +85,9 @@ object ScalacOptions {
formatter: Formatter[Name]
): Either[(Error, List[String]), Option[(Option[List[String]], List[String])]] =
args match {
case h :: t if scalacOptionsPrefixes.exists(h.startsWith) =>
case h :: t
if scalacOptionsPrefixes.exists(h.startsWith) &&
!ScalacDeprecatedOptions.contains(h) =>
Right(Some((Some(h :: acc.getOrElse(Nil)), t)))
case h :: t if scalacNoArgAliasedOptions.contains(h) =>
Right(Some((Some(h :: acc.getOrElse(Nil)), t)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ final case class SharedOptions(
.withScalacExtraOptions(scalacExtra)
.toScalacOptShadowingSeq
.filterNonRedirected
.filterNonDeprecated
.map(Positioned.commandLine),
compilerPlugins =
SharedOptions.parseDependencies(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package scala.cli.commands.util

import scala.build.Logger
import scala.build.options.{ScalacOpt, ShadowingSeq}
import scala.cli.commands.bloop.BloopExit
import scala.cli.commands.default.LegacyScalaOptions
import scala.cli.commands.shared.ScalacOptions.YScriptRunnerOption
import scala.cli.commands.shared.{ScalacExtraOptions, ScalacOptions}

object ScalacOptionsUtil {
Expand Down Expand Up @@ -31,6 +35,8 @@ object ScalacOptionsUtil {
opts.filterKeys(_.key.exists(f))
def filterNonRedirected: ShadowingSeq[ScalacOpt] =
opts.filterScalacOptionKeys(!ScalacOptions.ScalaCliRedirectedOptions.contains(_))
def filterNonDeprecated: ShadowingSeq[ScalacOpt] =
opts.filterScalacOptionKeys(!ScalacOptions.ScalacDeprecatedOptions.contains(_))
def getOption(key: String): Option[String] =
opts.get(ScalacOpt(key)).headOption.map(_.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ trait LegacyScalaRunnerTestDefinitions { _: DefaultTests =>
}
}

test("ensure -Yscriptrunner works with the default command") {
legacyOptionBackwardsCompatTest("-Yscriptrunner") {
(legacyOption, inputFile, root) =>
Seq("default", "resident", "shutdown", "scala.tools.nsc.fsc.ResidentScriptRunner").foreach {
legacyOptionValue =>
val res =
os.proc(
TestUtil.cli,
legacyOption,
legacyOptionValue,
inputFile,
TestUtil.extraOptions
)
.call(cwd = root, stderr = os.Pipe)
expect(res.err.trim().contains(deprecatedOptionWarning(legacyOption)))
expect(res.err.trim().contains(legacyOptionValue))
}
}
}

private def simpleLegacyOptionBackwardsCompatTest(optionAliases: String*): Unit =
abstractLegacyOptionBackwardsCompatTest(optionAliases) {
(legacyOption, expectedMsg, _, root) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final case class ShadowingSeq[T] private (values: Seq[Seq[T]]) {
case Seq(head, _*) => f(head)
case _ => true
}
def keys: Seq[T] = values.map(_.head)
def ++(other: Seq[T])(implicit key: ShadowingSeq.KeyOf[T]): ShadowingSeq[T] =
addGroups(ShadowingSeq.groups(other, key.groups(other)))
private def addGroups(other: Seq[Seq[T]])(implicit key: ShadowingSeq.KeyOf[T]): ShadowingSeq[T] =
Expand Down