Skip to content

Commit 79e75dc

Browse files
authored
Chore/group warnings about directives in multiple files (#2550)
* Add new mechanism for displaying readable diagnostics in CLI * Move warning message out to WarningMessages.scala * Remove assertion that cannot be checked now
1 parent 9b7a75d commit 79e75dc

File tree

7 files changed

+120
-17
lines changed

7 files changed

+120
-17
lines changed

modules/build/src/main/scala/scala/build/CrossSources.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,18 @@ object CrossSources {
346346
.values
347347
.flatten
348348
.filter((path, _) => ScopePath.fromPath(path) != ScopePath.fromPath(projectFilePath))
349-
.foreach { (_, directivesPositions) =>
350-
logger.diagnostic(
351-
s"Using directives detected in multiple files. It is recommended to keep them centralized in the $projectFilePath file.",
352-
positions = Seq(directivesPositions)
349+
.pipe { pathsToReport =>
350+
val diagnosticMessage = WarningMessages
351+
.directivesInMultipleFilesWarning(projectFilePath.toString)
352+
val cliFriendlyMessage = WarningMessages.directivesInMultipleFilesWarning(
353+
projectFilePath.toString,
354+
pathsToReport.map(_._2.render())
355+
)
356+
357+
logger.cliFriendlyDiagnostic(
358+
message = diagnosticMessage,
359+
cliFriendlyMessage = cliFriendlyMessage,
360+
positions = pathsToReport.map(_._2).toSeq
353361
)
354362
}
355363
}

modules/build/src/main/scala/scala/build/internal/util/WarningMessages.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,20 @@ object WarningMessages {
104104

105105
val offlineModeBloopJvmNotFound =
106106
"Offline mode is ON and a JVM for Bloop could not be fetched from the local cache, using scalac as fallback"
107+
108+
def directivesInMultipleFilesWarning(
109+
projectFilePath: String,
110+
pathsToReport: Iterable[String] = Nil
111+
) = {
112+
val detectedMsg = "Using directives detected in multiple files"
113+
val recommendedMsg =
114+
s"It is recommended to keep them centralized in the $projectFilePath file."
115+
if pathsToReport.isEmpty then
116+
s"$detectedMsg. $recommendedMsg"
117+
else
118+
s"""$detectedMsg:
119+
|${pathsToReport.mkString("- ", s"${System.lineSeparator}- ", "")}
120+
|$recommendedMsg
121+
|""".stripMargin
122+
}
107123
}

modules/cli/src/main/scala/scala/cli/internal/CliLogger.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ class CliLogger(
233233
} yield featureType -> (names ++ reportedNames)
234234
experimentalWarnings = Map.empty
235235
}
236+
237+
override def cliFriendlyDiagnostic(
238+
message: String,
239+
cliFriendlyMessage: String,
240+
severity: Severity,
241+
positions: Seq[Position]
242+
): Unit =
243+
diagnostic(cliFriendlyMessage, severity, Nil)
236244
}
237245

238246
object CliLogger {

modules/core/src/main/scala/scala/build/Logger.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ trait Logger {
4444
*/
4545
def experimentalWarning(featureName: String, featureType: FeatureType): Unit
4646
def flushExperimentalWarnings: Unit
47+
48+
def cliFriendlyDiagnostic(
49+
message: String,
50+
cliFriendlyMessage: String,
51+
severity: Severity = Severity.Warning,
52+
positions: Seq[Position] = Nil
53+
): Unit = diagnostic(message, severity, positions)
4754
}
4855

4956
object Logger {

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,77 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String])
597597
}
598598
}
599599

600+
test("directives in multiple files diagnostics") {
601+
val inputs = TestInputs(
602+
os.rel / "Foo.scala" ->
603+
s"""//> using scala "3.3.0"
604+
|
605+
|object Foo extends App {
606+
| println("Foo")
607+
|}
608+
|""".stripMargin,
609+
os.rel / "Bar.scala" -> "",
610+
os.rel / "Hello.java" -> "//> using jvm \"11\""
611+
)
612+
613+
withBsp(inputs, Seq(".")) { (root, localClient, remoteServer) =>
614+
async {
615+
await(remoteServer.workspaceBuildTargets().asScala)
616+
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
617+
val target = {
618+
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
619+
expect(targets.length == 2)
620+
extractMainTargets(targets)
621+
}
622+
623+
val targetUri = TestUtil.normalizeUri(target.getUri)
624+
checkTargetUri(root, targetUri)
625+
626+
val targets = List(target).asJava
627+
628+
val compileResp = await {
629+
remoteServer
630+
.buildTargetCompile(new b.CompileParams(targets))
631+
.asScala
632+
}
633+
expect(compileResp.getStatusCode == b.StatusCode.OK)
634+
635+
def checkDirectivesInMultipleFilesWarnings(
636+
fileName: String,
637+
expectedStartLine: Int,
638+
expectedStartCharacter: Int,
639+
expectedEndLine: Int,
640+
expectedEndCharacter: Int
641+
): Unit = {
642+
val diagnosticsParams = localClient.diagnostics().collectFirst {
643+
case diag
644+
if !diag.getDiagnostics.isEmpty &&
645+
TestUtil.normalizeUri(diag.getTextDocument.getUri) ==
646+
TestUtil.normalizeUri((root / fileName).toNIO.toUri.toASCIIString) => diag
647+
}
648+
expect(diagnosticsParams.isDefined)
649+
val diagnostics = diagnosticsParams.get.getDiagnostics.asScala.toSeq
650+
651+
val expectedMessage =
652+
"Using directives detected in multiple files. It is recommended to keep them centralized in the"
653+
checkDiagnostic(
654+
diagnostic = diagnostics.head,
655+
expectedMessage = expectedMessage,
656+
expectedSeverity = b.DiagnosticSeverity.WARNING,
657+
expectedStartLine = expectedStartLine,
658+
expectedStartCharacter = expectedStartCharacter,
659+
expectedEndLine = expectedEndLine,
660+
expectedEndCharacter = expectedEndCharacter,
661+
strictlyCheckMessage = false
662+
)
663+
}
664+
665+
checkDirectivesInMultipleFilesWarnings("Foo.scala", 0, 0, 0, 23)
666+
checkDirectivesInMultipleFilesWarnings("Hello.java", 0, 0, 0, 18)
667+
}
668+
}
669+
}
670+
600671
test("workspace update") {
601672
val inputs = TestInputs(
602673
os.rel / "simple.sc" ->

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,6 @@ class FixTests extends ScalaCliSuite {
383383

384384
assertNoDiff(withUsedTargetContents, withUsedTargetContentsRead)
385385
assertNoDiff(withUnusedTargetContents, withUnusedTargetContentsRead)
386-
387-
val runProc = os.proc(TestUtil.cli, "--power", "compile", ".")
388-
.call(cwd = root, stderr = os.Pipe)
389-
390-
val runErrOut = TestUtil.removeAnsiColors(runProc.err.trim)
391-
expect(runErrOut.contains("Using directives detected in multiple files"))
392-
expect(runErrOut.linesIterator.count(_.startsWith("[warn] //> using")) == 2)
393-
expect(runErrOut.contains("[warn] //> using options -Werror"))
394-
// TODO: Warning about using directives in multiple files for the test scope should not be displayed
395-
expect(runErrOut.contains("[warn] //> using scala \"3.2.2\""))
396386
}
397387

398388
assertNoDiff(

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,14 +768,17 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
768768
os.rel / "Hello.java" -> "//> using jvm \"11\""
769769
)
770770
inputs.fromRoot { root =>
771-
val warningMessage = "Using directives detected in"
772-
val output1 = os.proc(TestUtil.cli, ".").call(cwd = root, stderr = os.Pipe).err.trim()
771+
val warningMessage =
772+
"""Using directives detected in multiple files:
773+
|- Foo.scala:1:1-24
774+
|- Hello.java:1:1-19""".stripMargin
775+
val output1 = os.proc(TestUtil.cli, ".").call(cwd = root, stderr = os.Pipe).err.trim()
773776
val output2 = os.proc(TestUtil.cli, "Foo.scala", "Bar.scala").call(
774777
cwd = root,
775778
stderr = os.Pipe
776779
).err.trim()
777780
expect(output1.contains(warningMessage))
778-
expect(!output2.contains(warningMessage))
781+
expect(!output2.contains("Using directives detected in multiple files"))
779782
}
780783
}
781784

0 commit comments

Comments
 (0)