Skip to content

Commit abb02a2

Browse files
committed
Add warnings for -save & -nosave legacy scala runner options instead of failing
1 parent 5264acd commit abb02a2

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

modules/cli/src/main/scala/scala/cli/commands/default/Default.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Default(
5252
sys.exit(0)
5353
}
5454

55-
override def runCommand(options: DefaultOptions, args: RemainingArgs, logger: Logger): Unit = {
55+
override def runCommand(options: DefaultOptions, args: RemainingArgs, logger: Logger): Unit =
5656
if options.version then println(Version.versionInfo)
5757
else
5858
{
@@ -62,9 +62,8 @@ class Default(
6262
options.shared.snippet.executeMarkdown.nonEmpty ||
6363
(options.shared.extraJarsAndClassPath.nonEmpty && options.sharedRun.mainClass.mainClass.nonEmpty)
6464
if shouldDefaultToRun then RunOptions.parser else ReplOptions.parser
65-
}.parse(rawArgs) match
65+
}.parse(options.legacyScala.filterNonDeprecatedArgs(rawArgs, progName, logger).toSeq) match
6666
case Left(e) => error(e)
6767
case Right((replOptions: ReplOptions, _)) => Repl.runCommand(replOptions, args, logger)
6868
case Right((runOptions: RunOptions, _)) => Run.runCommand(runOptions, args, logger)
69-
}
7069
}

modules/cli/src/main/scala/scala/cli/commands/default/DefaultOptions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ case class DefaultOptions(
1414
sharedRun: SharedRunOptions = SharedRunOptions(),
1515
@Recurse
1616
sharedRepl: SharedReplOptions = SharedReplOptions(),
17+
@Recurse
18+
legacyScala: LegacyScalaOptions = LegacyScalaOptions(),
1719
@Name("-version")
1820
version: Boolean = false
1921
) extends HasSharedOptions
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package scala.cli.commands.default
2+
3+
import caseapp.*
4+
import caseapp.core.Indexed
5+
6+
import scala.build.Logger
7+
import scala.cli.commands.package0.Package
8+
import scala.cli.commands.tags
9+
10+
/** Options covering backwards compatibility with the old scala runner.
11+
*/
12+
// format: off
13+
case class LegacyScalaOptions(
14+
@Group("Scala")
15+
@HelpMessage("Ignored legacy option. Deprecated equivalent of running a subsequent `package` command.")
16+
@Tag(tags.must)
17+
@Name("-save")
18+
save: Option[Indexed[Boolean]] = None,
19+
@Group("Scala")
20+
@HelpMessage("Ignored legacy option. Deprecated override canceling the `-nosave` option.")
21+
@Tag(tags.must)
22+
@Name("-nosave")
23+
nosave: Option[Indexed[Boolean]] = None,
24+
) {
25+
// format: on
26+
27+
extension [T](indexedOption: Option[Indexed[T]]) {
28+
private def findArg(args: Array[String]): Option[String] =
29+
indexedOption.flatMap(io => args.lift(io.index))
30+
}
31+
32+
def filterNonDeprecatedArgs(
33+
args: Array[String],
34+
progName: String,
35+
logger: Logger
36+
): Array[String] = {
37+
val saveOptionString = save.findArg(args)
38+
val noSaveOptionString = nosave.findArg(args)
39+
val deprecatedArgs = Seq(saveOptionString, noSaveOptionString).flatten
40+
val filteredArgs = args.filterNot(deprecatedArgs.contains)
41+
val filteredArgsString = filteredArgs.mkString(" ")
42+
saveOptionString.foreach { s =>
43+
logger.message(
44+
s"""Deprecated option '$s' ignored.
45+
|The compiled project files will be saved in the '.scala-build' directory in the project root folder.
46+
|If an actual jar file is necessary to be saved, run the 'package' command after this one:
47+
| ${Console.BOLD}$progName ${Package.name} $filteredArgsString${Console.RESET}""".stripMargin
48+
)
49+
}
50+
noSaveOptionString.foreach { ns =>
51+
logger.message(
52+
s"""Deprecated option '$ns' ignored.
53+
|A jar file is not saved unless the '${Package.name}' subcommand is called.""".stripMargin
54+
)
55+
}
56+
filteredArgs
57+
}
58+
}
59+
object LegacyScalaOptions {
60+
implicit lazy val parser: Parser[LegacyScalaOptions] = Parser.derive
61+
implicit lazy val help: Help[LegacyScalaOptions] = Help.derive
62+
}

modules/integration/src/test/scala/scala/cli/integration/DefaultTests.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,42 @@ class DefaultTests extends WithWarmUpScalaCliSuite {
183183
}
184184
}
185185

186+
test("ensure -save/--save works with the default command") {
187+
val msg = "Hello world"
188+
TestInputs(os.rel / "s.sc" -> s"""println("$msg")""").fromRoot { root =>
189+
val legacySaveOption = "-save"
190+
val res1 =
191+
os.proc(TestUtil.cli, ".", legacySaveOption, TestUtil.extraOptions)
192+
.call(cwd = root, stderr = os.Pipe)
193+
expect(res1.out.trim() == msg)
194+
expect(res1.err.trim().contains(s"Deprecated option '$legacySaveOption' ignored"))
195+
val doubleDashSaveOption = "--save"
196+
val res2 =
197+
os.proc(TestUtil.cli, ".", doubleDashSaveOption, TestUtil.extraOptions)
198+
.call(cwd = root, stderr = os.Pipe)
199+
expect(res2.out.trim() == msg)
200+
expect(res2.err.trim().contains(s"Deprecated option '$doubleDashSaveOption' ignored"))
201+
}
202+
}
203+
204+
test("ensure -nosave/--nosave works with the default command") {
205+
val msg = "Hello world"
206+
TestInputs(os.rel / "s.sc" -> s"""println("$msg")""").fromRoot { root =>
207+
val legacyNoSaveOption = "-nosave"
208+
val res1 =
209+
os.proc(TestUtil.cli, ".", legacyNoSaveOption, TestUtil.extraOptions)
210+
.call(cwd = root, stderr = os.Pipe)
211+
expect(res1.out.trim() == msg)
212+
expect(res1.err.trim().contains(s"Deprecated option '$legacyNoSaveOption' ignored"))
213+
val doubleDashNoSaveOption = "--nosave"
214+
val res2 =
215+
os.proc(TestUtil.cli, ".", doubleDashNoSaveOption, TestUtil.extraOptions)
216+
.call(cwd = root, stderr = os.Pipe)
217+
expect(res2.out.trim() == msg)
218+
expect(res2.err.trim().contains(s"Deprecated option '$doubleDashNoSaveOption' ignored"))
219+
}
220+
}
221+
186222
private def unrecognizedArgMessage(argName: String) =
187223
s"""
188224
|Unrecognized argument: $argName

0 commit comments

Comments
 (0)