From 69631fc8bc969f0729aa9017d3d85ee0aa71740b Mon Sep 17 00:00:00 2001 From: Piotr Chabelski Date: Wed, 13 Nov 2024 17:09:05 +0100 Subject: [PATCH] Allow shading of single-choice compiler options from the command line regardless of `-`/`--` prefix --- .../scala/cli/commands/ScalaCommand.scala | 4 +-- .../CompileScalacCompatTestDefinitions.scala | 32 +++++++++++++++++++ .../scala/build/options/ShadowingSeq.scala | 6 ++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala b/modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala index bedc39b873..14a7600fd7 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala @@ -177,9 +177,7 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T], sharedOptions(options).foreach { so => val scalacOpts = so.scalacOptions.toScalacOptShadowingSeq scalacOpts.keys - .find(k => - k == ScalacOpt(s"-$YScriptRunnerOption") || k == ScalacOpt(s"--$YScriptRunnerOption") - ) + .find(_.value.noDashPrefixes == YScriptRunnerOption) .map(_.value) .foreach(k => logger.message(LegacyScalaOptions.yScriptRunnerWarning(k, scalacOpts.getOption(k))) diff --git a/modules/integration/src/test/scala/scala/cli/integration/CompileScalacCompatTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/CompileScalacCompatTestDefinitions.scala index 872924ae58..a862818e58 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/CompileScalacCompatTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/CompileScalacCompatTestDefinitions.scala @@ -205,4 +205,36 @@ trait CompileScalacCompatTestDefinitions { _: CompileTestDefinitions => } } } + + { + val prefixes = Seq("-", "--") + for { + prefix1 <- prefixes + prefix2 <- prefixes + optionKey = "Werror" + option1 = prefix1 + optionKey + option2 = prefix2 + optionKey + if actualScalaVersion.startsWith("3") + } test( + s"allow to override $option1 compiler option passed via directive by passing $option2 from the command line" + ) { + val file = "example.scala" + TestInputs(os.rel / file -> + s"""//> using options -Wunused:all $option1 + |@main def main() = { + | val unused = "" + | println("Hello, world!") + |} + |""".stripMargin).fromRoot { root => + os.proc( + TestUtil.cli, + "compile", + file, + s"$option2:false", + extraOptions + ) + .call(cwd = root, stderr = os.Pipe) + } + } + } } diff --git a/modules/options/src/main/scala/scala/build/options/ShadowingSeq.scala b/modules/options/src/main/scala/scala/build/options/ShadowingSeq.scala index 1ff88b5daf..1e847f0317 100644 --- a/modules/options/src/main/scala/scala/build/options/ShadowingSeq.scala +++ b/modules/options/src/main/scala/scala/build/options/ShadowingSeq.scala @@ -2,6 +2,7 @@ package scala.build.options import dependency.AnyDependency +import scala.build.options.ScalacOpt.noDashPrefixes import scala.collection.mutable /** Seq ensuring some of its values are unique according to some key */ @@ -31,10 +32,11 @@ final case class ShadowingSeq[T] private (values: Seq[Seq[T]]) { for (group <- values.iterator ++ other.iterator) { assert(group.nonEmpty) val keyOpt = key.makeKey(group) - if (!keyOpt.exists(seen.contains)) { + if !keyOpt.exists(k => seen.contains(k.noDashPrefixes)) + then { l += group for (key <- keyOpt) - seen += key + seen += key.noDashPrefixes } }