Skip to content

Commit

Permalink
Ondemand compilation doesn't take scalacOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mlachkar committed Nov 23, 2020
1 parent 673244b commit e52b0f2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
18 changes: 11 additions & 7 deletions scalafix-cli/src/main/scala/scalafix/internal/v1/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

import scala.annotation.StaticAnnotation
import scala.tools.nsc.CompilerCommand
import scala.tools.nsc.Settings
import scala.tools.nsc.interactive.Global
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.reporters.StoreReporter
import scala.util.Failure
import scala.util.Success
import scala.util.Try
Expand All @@ -42,7 +43,6 @@ import scalafix.internal.jgit.JGitDiff
import scalafix.internal.reflect.ClasspathOps
import scalafix.v1.Configuration
import scalafix.v1.RuleDecoder

class Section(val name: String) extends StaticAnnotation

case class Args(
Expand Down Expand Up @@ -376,11 +376,15 @@ case class Args(

def configuredGlobal: Configured[LazyValue[Option[Global]]] =
Configured.ok {
val settings = new Settings()
settings.YpresentationAnyThread.value = true
settings.classpath.value = validatedClasspath.syntax
val reporter = new ConsoleReporter(settings)
LazyValue.fromUnsafe(() => new Global(settings, reporter))
LazyValue.fromUnsafe(() => {
val ss = new Settings()
val command = new CompilerCommand(scalacOptions, ss)
val settings = command.settings
settings.YpresentationAnyThread.value = true
settings.classpath.value = validatedClasspath.syntax
val reporter = new StoreReporter()
new Global(settings, reporter)
})
}

def validate: Configured[ValidatedArgs] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import scala.util.control.NonFatal
import scala.meta.Tree
import scala.meta.inputs.Input
import scala.meta.interactive.InteractiveSemanticdb
import scala.meta.internal.semanticdb.Print
import scala.meta.internal.semanticdb.TextDocument
import scala.meta.io.AbsolutePath
import scala.meta.parsers.ParseException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.meta.testkit.StringFS

import scalafix.cli._
import scalafix.tests.core.Classpaths
import scalafix.tests.util.ScalaVersions

class CliSemanticSuite extends BaseCliSuite {

Expand Down Expand Up @@ -257,4 +258,54 @@ class CliSemanticSuite extends BaseCliSuite {
)
}

test("on-demand with scalacOptions") {
// Todo: Ondemand compilation doesn't take scalacOptions or the reporter doesn't gather all warns in 2.13
// see https://github.com/scalacenter/scalafix/pull/1215
if (ScalaVersions.isScala213) true
else {
val warnUnused =
if (ScalaVersions.isScala213) "-Wunused:imports"
else "-Ywarn-unused-import"

val root = StringFS.fromString(
"""|/NoSemanticdb2.scala
|package b
|import scala.concurrent.Future
|object a {
| val x = _root_.scala.List()
| x + "string"
|}
|/.scalafix.conf
|rules = RemoveUnused
|""".stripMargin
)
val (out, exit) = runMain(
Array(
"--files",
"NoSemanticdb2.scala",
"--classpath",
defaultClasspath,
"--scalac-options",
warnUnused
),
root.toNIO
)
assert(exit.isOk)
val obtained =
FileIO.slurp(
root.resolve("NoSemanticdb2.scala"),
StandardCharsets.UTF_8
)
assertNoDiff(
obtained,
"""
|package b
|object a {
| val x = _root_.scala.List()
| x + "string"
|}
|""".stripMargin
)
}
}
}

0 comments on commit e52b0f2

Please sign in to comment.