diff --git a/bsp/src/mill/bsp/BspCompileProblemReporter.scala b/bsp/src/mill/bsp/BspCompileProblemReporter.scala index 4a4062834e0..36c9296404b 100644 --- a/bsp/src/mill/bsp/BspCompileProblemReporter.scala +++ b/bsp/src/mill/bsp/BspCompileProblemReporter.scala @@ -130,7 +130,6 @@ class BspCompileProblemReporter( pos.endColumn.orElse(pos.pointer).getOrElse[Int](start.getCharacter.intValue()) ) val diagnostic = new bsp.Diagnostic(new bsp.Range(start, end), problem.message) - diagnostic.setCode(pos.lineContent) diagnostic.setSource("mill") diagnostic.setSeverity( problem.severity match { @@ -139,6 +138,7 @@ class BspCompileProblemReporter( case mill.api.Warn => bsp.DiagnosticSeverity.WARNING } ) + problem.diagnosticCode.foreach { existingCode => diagnostic.setCode(existingCode.code) } diagnostic } diff --git a/main/api/src/mill/api/CompileProblemReporter.scala b/main/api/src/mill/api/CompileProblemReporter.scala index 6df05a81aed..af0b40f2e0a 100644 --- a/main/api/src/mill/api/CompileProblemReporter.scala +++ b/main/api/src/mill/api/CompileProblemReporter.scala @@ -30,6 +30,17 @@ trait Problem { def message: String def position: ProblemPosition + + def diagnosticCode: Option[DiagnosticCode] +} + +/** + * Unique diagnostic code given from the compiler with an optional further explanation. + */ +trait DiagnosticCode { + def code: String + + def explanation: Option[String] } /** diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincDiagnosticCode.scala b/scalalib/worker/src/mill/scalalib/worker/ZincDiagnosticCode.scala new file mode 100644 index 00000000000..218464a3bca --- /dev/null +++ b/scalalib/worker/src/mill/scalalib/worker/ZincDiagnosticCode.scala @@ -0,0 +1,12 @@ +package mill.scalalib.worker + +import mill.api.internal +import mill.api.DiagnosticCode + +import scala.jdk.OptionConverters._ + +@internal +final case class ZincDiagnosticCode(base: xsbti.DiagnosticCode) extends DiagnosticCode { + override def code: String = base.code() + override def explanation: Option[String] = base.explanation().toScala +} diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincProblem.scala b/scalalib/worker/src/mill/scalalib/worker/ZincProblem.scala index 78c1acc74a3..a1be7e70258 100644 --- a/scalalib/worker/src/mill/scalalib/worker/ZincProblem.scala +++ b/scalalib/worker/src/mill/scalalib/worker/ZincProblem.scala @@ -1,6 +1,8 @@ package mill.scalalib.worker import mill.api.{Problem, ProblemPosition, Severity, internal} +import mill.api.DiagnosticCode +import scala.jdk.OptionConverters._ @internal class ZincProblem(base: xsbti.Problem) extends Problem { @@ -15,4 +17,7 @@ class ZincProblem(base: xsbti.Problem) extends Problem { override def message: String = base.message() override def position: ProblemPosition = new ZincProblemPosition(base.position()) + + override def diagnosticCode: Option[DiagnosticCode] = + base.diagnosticCode().toScala.map(ZincDiagnosticCode) } diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala index 798b9f2443c..2c60ec4ca00 100644 --- a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala +++ b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala @@ -435,6 +435,7 @@ class ZincWorkerImpl( val newReporter = reporter match { case None => new ManagedLoggedReporter(10, logger) case Some(r) => new ManagedLoggedReporter(10, logger) { + override def logError(problem: xsbti.Problem): Unit = { r.logError(new ZincProblem(problem)) super.logError(problem)