Skip to content

Commit cba8f54

Browse files
authored
Allow shading of single-choice compiler options from the command line regardless of -/-- prefix (#3279)
1 parent 2a68636 commit cba8f54

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
177177
sharedOptions(options).foreach { so =>
178178
val scalacOpts = so.scalacOptions.toScalacOptShadowingSeq
179179
scalacOpts.keys
180-
.find(k =>
181-
k == ScalacOpt(s"-$YScriptRunnerOption") || k == ScalacOpt(s"--$YScriptRunnerOption")
182-
)
180+
.find(_.value.noDashPrefixes == YScriptRunnerOption)
183181
.map(_.value)
184182
.foreach(k =>
185183
logger.message(LegacyScalaOptions.yScriptRunnerWarning(k, scalacOpts.getOption(k)))

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,36 @@ trait CompileScalacCompatTestDefinitions { _: CompileTestDefinitions =>
205205
}
206206
}
207207
}
208+
209+
{
210+
val prefixes = Seq("-", "--")
211+
for {
212+
prefix1 <- prefixes
213+
prefix2 <- prefixes
214+
optionKey = "Werror"
215+
option1 = prefix1 + optionKey
216+
option2 = prefix2 + optionKey
217+
if actualScalaVersion.startsWith("3")
218+
} test(
219+
s"allow to override $option1 compiler option passed via directive by passing $option2 from the command line"
220+
) {
221+
val file = "example.scala"
222+
TestInputs(os.rel / file ->
223+
s"""//> using options -Wunused:all $option1
224+
|@main def main() = {
225+
| val unused = ""
226+
| println("Hello, world!")
227+
|}
228+
|""".stripMargin).fromRoot { root =>
229+
os.proc(
230+
TestUtil.cli,
231+
"compile",
232+
file,
233+
s"$option2:false",
234+
extraOptions
235+
)
236+
.call(cwd = root, stderr = os.Pipe)
237+
}
238+
}
239+
}
208240
}

modules/options/src/main/scala/scala/build/options/ShadowingSeq.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.build.options
22

33
import dependency.AnyDependency
44

5+
import scala.build.options.ScalacOpt.noDashPrefixes
56
import scala.collection.mutable
67

78
/** 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]]) {
3132
for (group <- values.iterator ++ other.iterator) {
3233
assert(group.nonEmpty)
3334
val keyOpt = key.makeKey(group)
34-
if (!keyOpt.exists(seen.contains)) {
35+
if !keyOpt.exists(k => seen.contains(k.noDashPrefixes))
36+
then {
3537
l += group
3638
for (key <- keyOpt)
37-
seen += key
39+
seen += key.noDashPrefixes
3840
}
3941
}
4042

0 commit comments

Comments
 (0)