Skip to content

Commit

Permalink
Warning for multiple files with using directives
Browse files Browse the repository at this point in the history
  • Loading branch information
wleczny committed Nov 22, 2022
1 parent 30680c2 commit 1db852a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Expand Up @@ -153,6 +153,29 @@ object CrossSources {

val preprocessedInputFromArgs = value(preprocessSources(inputs.flattened()))

val preprocessedWithUsingDirs =
preprocessedInputFromArgs.filter(p => p.options.exists(_ != BuildOptions.monoid.zero))

if (preprocessedWithUsingDirs.length > 1)
val projectFilePath = inputs.elements.projectSettingsFiles.headOption match
case Some(s) => s.path
case _ => inputs.workspace / "project.scala"

preprocessedWithUsingDirs
.filter(p => os.Path(p.scopePath.path, os.root) != projectFilePath)
.foreach { file =>
file.scopePath.root match
case Right(base: os.Path) =>
val path = os.Path(file.scopePath.path, base)
val linesNumbers = os.read.lines(path).zipWithIndex
.filter((ln, _) => ln.startsWith("//> using"))
.map((_, idx) => idx + 1)
logger.diagnostic(
s"WARNING: using directives detected in $path [Ln: ${linesNumbers.mkString(", ")}]. It is recommended to keep them centralized in the $projectFilePath file."
)
case _ => ()
}

val sourcesFromDirectives =
preprocessedInputFromArgs
.flatMap(_.options)
Expand Down
Expand Up @@ -683,6 +683,30 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("multiple using directives warning message") {
val inputs = TestInputs(
os.rel / "Foo.scala" ->
s"""//> using scala "3.2.0"
|
|object Foo extends App {
| println("Foo")
|}
|""".stripMargin,
os.rel / "Bar.scala" -> "",
os.rel / "Hello.java" -> "//> using packaging.output \"out\""
)
inputs.fromRoot { root =>
val warningMessage = "WARNING: using directives detected in"
val output1 = os.proc(TestUtil.cli, ".").call(cwd = root, stderr = os.Pipe).err.trim()
val output2 = os.proc(TestUtil.cli, "Foo.scala", "Bar.scala").call(
cwd = root,
stderr = os.Pipe
).err.trim()
expect(output1.contains(warningMessage))
expect(!output2.contains(warningMessage))
}
}

def sudoTest(): Unit = {
val fileName = "simple.sc"
val message = "Hello"
Expand Down

0 comments on commit 1db852a

Please sign in to comment.