Skip to content

Commit

Permalink
Improve error message when directive is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostbuster91 committed Feb 21, 2022
1 parent 1b56405 commit eb215fe
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package scala.build.errors

final class DirectiveErrors(errors: ::[String]) extends BuildException(
"Directives errors: " + errors.mkString(", ")
import scala.build.Position

final class DirectiveErrors(errors: ::[String], positions: Seq[Position]) extends BuildException(
"Directives errors: " + errors.mkString(", "),
positions = positions
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs}
import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor}

import scala.build.errors._
import scala.build.preprocessing.directives.StrictDirective
import scala.build.preprocessing.directives.{DirectiveUtil, StrictDirective}
import scala.build.{Logger, Position}
import scala.collection.mutable
import scala.jdk.CollectionConverters._
Expand All @@ -29,7 +29,8 @@ object ExtractedDirectives {
contentChars: Array[Char],
path: Either[String, os.Path],
logger: Logger,
supportedDirectives: Array[UsingDirectiveKind]
supportedDirectives: Array[UsingDirectiveKind],
cwd: ScopePath
): Either[BuildException, ExtractedDirectives] = {
val errors = new mutable.ListBuffer[Diagnostic]
val reporter = CustomDirectivesReporter.create(path) { diag =>
Expand Down Expand Up @@ -111,9 +112,17 @@ object ExtractedDirectives {
if (usedDirectives.getKind() != UsingDirectiveKind.Code) 0
else usedDirectives.getCodeOffset()
if (supportedDirectives.contains(usedDirectives.getKind()))
Right(ExtractedDirectives(offset, strictDirectives))
else
Left(new DirectiveErrors(::(s"Unsupported using directive kind ${usedDirectives.getKind}", Nil)))
Right(ExtractedDirectives(offset, strictDirectives))
else {
val directiveVales =
usedDirectives.getFlattenedMap.values().asScala.toList.flatMap(_.asScala)
val values = DirectiveUtil.stringValues(directiveVales, path, cwd) ++
DirectiveUtil.numericValues(directiveVales, path, cwd)
Left(new DirectiveErrors(
::(s"Directive '${usedDirectives.getKind}' is not supported in the given context'", Nil),
values.flatMap(_._1.positions)
))
}
}
else {
val errors0 = errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ case object JavaPreprocessor extends Preprocessor {
content.toCharArray,
Right(j.path),
logger,
Array(UsingDirectiveKind.PlainComment, UsingDirectiveKind.SpecialComment)
Array(UsingDirectiveKind.PlainComment, UsingDirectiveKind.SpecialComment),
scopePath
))
val updatedOptions = value(DirectivesProcessor.process(
directives0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ case object ScalaPreprocessor extends Preprocessor {
logger: Logger
): Either[BuildException, StrictDirectivesProcessingOutput] = either {
val contentChars = content.toCharArray
val ExtractedDirectives(codeOffset, directives0) = value(ExtractedDirectives.from(contentChars, path, logger, UsingDirectiveKind.values()))
val ExtractedDirectives(codeOffset, directives0) =
value(ExtractedDirectives.from(contentChars, path, logger, UsingDirectiveKind.values(), cwd))

val updatedOptions = value {
DirectivesProcessor.process(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ case object RequireScalaVersionDirectiveHandler extends RequireDirectiveHandler
Right(Some(req))
case _ =>
// TODO: Handle errors and conflicts
Left(new DirectiveErrors(::("Match error in ScalaVersionDirectiveHandler", Nil)))
Left(new DirectiveErrors(::("Match error in ScalaVersionDirectiveHandler", Nil), Seq.empty))
}

def handleValues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
scope = Some(BuildRequirements.ScopeRequirement(scope))
)
Right(Some(req))
case _ => Left(new DirectiveErrors(::("No such scope", Nil)))
case _ => Left(new DirectiveErrors(::("No such scope", Nil), Seq.empty))
}

val scoped = values
Expand All @@ -54,7 +54,7 @@ case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
)
)
Right(req)
case (_, Some(_)) => Left(new DirectiveErrors(::("No such scope", Nil)))
case (_, Some(_)) => Left(new DirectiveErrors(::("No such scope", Nil), Seq.empty))
}
.toSeq
.sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.eed3si9n.expecty.Expecty.{assert => expect}
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind

import scala.build.errors.Diagnostic
import scala.build.preprocessing.ExtractedDirectives
import scala.build.preprocessing.{ExtractedDirectives, ScopePath}

class ScalaPreprocessorTests extends munit.FunSuite {

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

val diags = persistentLogger.diagnostics
Expand Down

0 comments on commit eb215fe

Please sign in to comment.