Skip to content

Commit

Permalink
Implement diagnostics for Scala 3 (bazelbuild#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
susliko committed Sep 4, 2024
1 parent ac4181c commit 9e591a4
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 28 deletions.
41 changes: 15 additions & 26 deletions src/java/io/bazel/rulesscala/scalac/ScalacInvoker3.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
package io.bazel.rulesscala.scalac;

import dotty.tools.dotc.Driver;
import io.bazel.rulesscala.scalac.compileoptions.CompileOptions;
import java.nio.file.Paths;
import java.nio.file.Files;
import scala.Tuple2;
import io.bazel.rulesscala.scalac.reporter.ProtoReporter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import dotty.tools.dotc.reporting.Reporter;
import dotty.tools.dotc.Compiler;
import dotty.tools.dotc.Driver;
import dotty.tools.dotc.core.Contexts;
import dotty.tools.io.AbstractFile;

//Invokes Scala 3 compiler
class ScalacInvoker{
// Invokes Scala 3 compiler
class ScalacInvoker {
public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
throws IOException, Exception{

throws IOException, Exception {

ScalacInvokerResults results = new ScalacInvokerResults();
Driver driver = new dotty.tools.dotc.Driver();
Contexts.Context ctx = driver.initCtx().fresh();

Tuple2<scala.collection.immutable.List<AbstractFile>, Contexts.Context> r = driver.setup(compilerArgs, ctx).get();

Compiler compiler = driver.newCompiler(r._2);
ProtoReporter protoReporter = new ProtoReporter();

results.startTime= System.currentTimeMillis();
results.startTime = System.currentTimeMillis();

Reporter reporter = driver.doCompile(compiler, r._1, r._2);
driver.process(compilerArgs, protoReporter, null);

results.stopTime = System.currentTimeMillis();

Files.createFile(
Paths.get(ops.diagnosticsFile));
Files.createFile(
Paths.get(ops.scalaDepsFile));
Files.createFile(Paths.get(ops.diagnosticsFile));
Files.createFile(Paths.get(ops.scalaDepsFile));

protoReporter.writeTo(Paths.get(ops.diagnosticsFile));

if (reporter.hasErrors()) {
// reporter.flush();
if (protoReporter.hasErrors()) {
// reporter.flush();
throw new RuntimeException("Build failed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ filegroup(
srcs = select_for_scala_version(
before_2_12_13 = ["before_2_12_13/DepsTrackingReporter.java"],
between_2_12_13_and_2_13_12 = ["after_2_12_13_and_before_2_13_12/DepsTrackingReporter.java"],
since_2_13_12 = ["after_2_13_12/DepsTrackingReporter.java"],
between_2_13_12_and_3 = ["after_2_13_12_and_before_3/DepsTrackingReporter.java"],
since_3 = ["since_3/PlaceholderDepsTrackingReporter.java"],
),
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.bazel.rulesscala.scalac.reporter;

class PlaceholderDepsTrackingRepoter {}

5 changes: 4 additions & 1 deletion src/java/io/bazel/rulesscala/scalac/reporter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ java_library(
"//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter",
"after_2_13_12/ProtoReporter.java",
],
default = ["PlaceholderForEmptyScala3Lib.java"],
since_3 = [
"//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter",
"since_3/ProtoReporter.java",
],
),
visibility = ["//visibility:public"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.bazel.rulesscala.scalac.reporter;

import dotty.tools.dotc.core.Contexts.Context;
import dotty.tools.dotc.interfaces.SourcePosition;
import dotty.tools.dotc.reporting.AbstractReporter;
import dotty.tools.dotc.reporting.ConsoleReporter;
import dotty.tools.dotc.reporting.Diagnostic;
import dotty.tools.dotc.reporting.Reporter;
import io.bazel.rules_scala.diagnostics.Diagnostics;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;

public class ProtoReporter extends AbstractReporter {

private final Map<String, List<Diagnostics.Diagnostic>> builder;
private final Reporter delegate;

public ProtoReporter() {
super();

scala.Console$ console = scala.Console$.MODULE$;
this.delegate = new ConsoleReporter(console.in(), new PrintWriter(console.err(), true));
builder = new LinkedHashMap<>();
}

public void writeTo(Path path) throws IOException {
Diagnostics.TargetDiagnostics.Builder targetDiagnostics =
Diagnostics.TargetDiagnostics.newBuilder();
for (Map.Entry<String, List<Diagnostics.Diagnostic>> entry : builder.entrySet()) {
targetDiagnostics.addDiagnostics(
Diagnostics.FileDiagnostics.newBuilder()
.setPath(entry.getKey())
.addAllDiagnostics(entry.getValue()));
}
Files.write(
path,
targetDiagnostics.build().toByteArray(),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
}

@Override
public void printSummary(Context ctx) {
delegate.printSummary(ctx);
}

@Override
public void doReport(Diagnostic diag, Context ctx) {
delegate.doReport(diag, ctx);
if (diag.position().isEmpty()) {
return;
}
SourcePosition pos = diag.position().get();
System.out.println("HERE " + diag.position().get());
System.out.println(positionToRange(pos));
System.out.println(convertSeverity(diag.level()));
System.out.println(diag.message());
System.out.println(pos.source());
System.out.println(pos.source().path());
Diagnostics.Diagnostic diagnostic =
Diagnostics.Diagnostic.newBuilder()
.setSeverity(convertSeverity(diag.level()))
.setMessage(diag.message())
.setRange(positionToRange(pos))
.build();

String uri = "workspace-root://" + pos.source().path();
System.out.println(uri);
List<Diagnostics.Diagnostic> diagnostics = builder.computeIfAbsent(uri, key -> new ArrayList());
diagnostics.add(diagnostic);
}

private Diagnostics.Severity convertSeverity(int severity) {
if (severity == Diagnostic.ERROR) {
return Diagnostics.Severity.ERROR;
} else if (severity == Diagnostic.WARNING) {
return Diagnostics.Severity.WARNING;
} else if (severity == Diagnostic.INFO) {
return Diagnostics.Severity.INFORMATION;
}
throw new RuntimeException("Unknown severity: " + severity);
}

private Diagnostics.Range positionToRange(SourcePosition pos) {
return Diagnostics.Range.newBuilder()
.setStart(
Diagnostics.Position.newBuilder()
.setLine(pos.startLine())
.setCharacter(pos.startColumn()))
.setEnd(
Diagnostics.Position.newBuilder()
.setLine(pos.endLine())
.setCharacter(pos.endColumn())
.build())
.build();
}
}

0 comments on commit 9e591a4

Please sign in to comment.