Skip to content

Commit

Permalink
Allow shading of single-choice compiler options from the command line…
Browse files Browse the repository at this point in the history
… regardless of `-`/`--` prefix (#3279)
  • Loading branch information
Gedochao authored Nov 25, 2024
1 parent 2a68636 commit cba8f54
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit cba8f54

Please sign in to comment.