Skip to content

Commit eb215fe

Browse files
committed
Improve error message when directive is not supported
1 parent 1b56405 commit eb215fe

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package scala.build.errors
22

3-
final class DirectiveErrors(errors: ::[String]) extends BuildException(
4-
"Directives errors: " + errors.mkString(", ")
3+
import scala.build.Position
4+
5+
final class DirectiveErrors(errors: ::[String], positions: Seq[Position]) extends BuildException(
6+
"Directives errors: " + errors.mkString(", "),
7+
positions = positions
58
)

modules/build/src/main/scala/scala/build/preprocessing/ExtractedDirectives.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs}
1010
import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor}
1111

1212
import scala.build.errors._
13-
import scala.build.preprocessing.directives.StrictDirective
13+
import scala.build.preprocessing.directives.{DirectiveUtil, StrictDirective}
1414
import scala.build.{Logger, Position}
1515
import scala.collection.mutable
1616
import scala.jdk.CollectionConverters._
@@ -29,7 +29,8 @@ object ExtractedDirectives {
2929
contentChars: Array[Char],
3030
path: Either[String, os.Path],
3131
logger: Logger,
32-
supportedDirectives: Array[UsingDirectiveKind]
32+
supportedDirectives: Array[UsingDirectiveKind],
33+
cwd: ScopePath
3334
): Either[BuildException, ExtractedDirectives] = {
3435
val errors = new mutable.ListBuffer[Diagnostic]
3536
val reporter = CustomDirectivesReporter.create(path) { diag =>
@@ -111,9 +112,17 @@ object ExtractedDirectives {
111112
if (usedDirectives.getKind() != UsingDirectiveKind.Code) 0
112113
else usedDirectives.getCodeOffset()
113114
if (supportedDirectives.contains(usedDirectives.getKind()))
114-
Right(ExtractedDirectives(offset, strictDirectives))
115-
else
116-
Left(new DirectiveErrors(::(s"Unsupported using directive kind ${usedDirectives.getKind}", Nil)))
115+
Right(ExtractedDirectives(offset, strictDirectives))
116+
else {
117+
val directiveVales =
118+
usedDirectives.getFlattenedMap.values().asScala.toList.flatMap(_.asScala)
119+
val values = DirectiveUtil.stringValues(directiveVales, path, cwd) ++
120+
DirectiveUtil.numericValues(directiveVales, path, cwd)
121+
Left(new DirectiveErrors(
122+
::(s"Directive '${usedDirectives.getKind}' is not supported in the given context'", Nil),
123+
values.flatMap(_._1.positions)
124+
))
125+
}
117126
}
118127
else {
119128
val errors0 = errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions))

modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ case object JavaPreprocessor extends Preprocessor {
2424
content.toCharArray,
2525
Right(j.path),
2626
logger,
27-
Array(UsingDirectiveKind.PlainComment, UsingDirectiveKind.SpecialComment)
27+
Array(UsingDirectiveKind.PlainComment, UsingDirectiveKind.SpecialComment),
28+
scopePath
2829
))
2930
val updatedOptions = value(DirectivesProcessor.process(
3031
directives0,

modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ case object ScalaPreprocessor extends Preprocessor {
266266
logger: Logger
267267
): Either[BuildException, StrictDirectivesProcessingOutput] = either {
268268
val contentChars = content.toCharArray
269-
val ExtractedDirectives(codeOffset, directives0) = value(ExtractedDirectives.from(contentChars, path, logger, UsingDirectiveKind.values()))
269+
val ExtractedDirectives(codeOffset, directives0) =
270+
value(ExtractedDirectives.from(contentChars, path, logger, UsingDirectiveKind.values(), cwd))
270271

271272
val updatedOptions = value {
272273
DirectivesProcessor.process(

modules/build/src/main/scala/scala/build/preprocessing/directives/RequireScalaVersionDirectiveHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ case object RequireScalaVersionDirectiveHandler extends RequireDirectiveHandler
5959
Right(Some(req))
6060
case _ =>
6161
// TODO: Handle errors and conflicts
62-
Left(new DirectiveErrors(::("Match error in ScalaVersionDirectiveHandler", Nil)))
62+
Left(new DirectiveErrors(::("Match error in ScalaVersionDirectiveHandler", Nil), Seq.empty))
6363
}
6464

6565
def handleValues(

modules/build/src/main/scala/scala/build/preprocessing/directives/RequireScopeDirectiveHandler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
4040
scope = Some(BuildRequirements.ScopeRequirement(scope))
4141
)
4242
Right(Some(req))
43-
case _ => Left(new DirectiveErrors(::("No such scope", Nil)))
43+
case _ => Left(new DirectiveErrors(::("No such scope", Nil), Seq.empty))
4444
}
4545

4646
val scoped = values
@@ -54,7 +54,7 @@ case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
5454
)
5555
)
5656
Right(req)
57-
case (_, Some(_)) => Left(new DirectiveErrors(::("No such scope", Nil)))
57+
case (_, Some(_)) => Left(new DirectiveErrors(::("No such scope", Nil), Seq.empty))
5858
}
5959
.toSeq
6060
.sequence

modules/build/src/test/scala/scala/build/tests/ScalaPreprocessorTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.eed3si9n.expecty.Expecty.{assert => expect}
55
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
66

77
import scala.build.errors.Diagnostic
8-
import scala.build.preprocessing.ExtractedDirectives
8+
import scala.build.preprocessing.{ExtractedDirectives, ScopePath}
99

1010
class ScalaPreprocessorTests extends munit.FunSuite {
1111

@@ -40,7 +40,7 @@ class ScalaPreprocessorTests extends munit.FunSuite {
4040
private def testWarnings(lines: String*)(expectedWarnings: Check*): Unit = {
4141
val persistentLogger = new PersistentDiagnosticLogger(Logger.nop)
4242
val code = lines.mkString("\n").toCharArray()
43-
val res = ExtractedDirectives.from(code, Right(path), persistentLogger, UsingDirectiveKind.values())
43+
val res = ExtractedDirectives.from(code, Right(path), persistentLogger, UsingDirectiveKind.values(), ScopePath.fromPath(path))
4444
expect(res.isRight)
4545

4646
val diags = persistentLogger.diagnostics

0 commit comments

Comments
 (0)