Skip to content

Commit 38a14f2

Browse files
committed
Implement diagnostics for Scala 3 (#1602)
1 parent ac4181c commit 38a14f2

File tree

7 files changed

+120
-29
lines changed

7 files changed

+120
-29
lines changed

src/java/io/bazel/rulesscala/scalac/ScalacInvoker3.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
package io.bazel.rulesscala.scalac;
22

3+
import dotty.tools.dotc.Driver;
34
import io.bazel.rulesscala.scalac.compileoptions.CompileOptions;
4-
import java.nio.file.Paths;
5-
import java.nio.file.Files;
6-
import scala.Tuple2;
5+
import io.bazel.rulesscala.scalac.reporter.ProtoReporter;
76
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
89

9-
import dotty.tools.dotc.reporting.Reporter;
10-
import dotty.tools.dotc.Compiler;
11-
import dotty.tools.dotc.Driver;
12-
import dotty.tools.dotc.core.Contexts;
13-
import dotty.tools.io.AbstractFile;
14-
15-
//Invokes Scala 3 compiler
16-
class ScalacInvoker{
10+
// Invokes Scala 3 compiler
11+
class ScalacInvoker {
1712
public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
18-
throws IOException, Exception{
19-
13+
throws IOException, Exception {
2014

2115
ScalacInvokerResults results = new ScalacInvokerResults();
2216
Driver driver = new dotty.tools.dotc.Driver();
23-
Contexts.Context ctx = driver.initCtx().fresh();
24-
25-
Tuple2<scala.collection.immutable.List<AbstractFile>, Contexts.Context> r = driver.setup(compilerArgs, ctx).get();
26-
27-
Compiler compiler = driver.newCompiler(r._2);
17+
ProtoReporter protoReporter = new ProtoReporter();
2818

29-
results.startTime= System.currentTimeMillis();
19+
results.startTime = System.currentTimeMillis();
3020

31-
Reporter reporter = driver.doCompile(compiler, r._1, r._2);
21+
driver.process(compilerArgs, protoReporter, null);
3222

3323
results.stopTime = System.currentTimeMillis();
3424

35-
Files.createFile(
36-
Paths.get(ops.diagnosticsFile));
37-
Files.createFile(
38-
Paths.get(ops.scalaDepsFile));
25+
Files.createFile(Paths.get(ops.diagnosticsFile));
26+
Files.createFile(Paths.get(ops.scalaDepsFile));
3927

28+
protoReporter.writeTo(Paths.get(ops.diagnosticsFile));
4029

41-
if (reporter.hasErrors()) {
42-
// reporter.flush();
30+
if (protoReporter.hasErrors()) {
31+
// reporter.flush();
4332
throw new RuntimeException("Build failed");
4433
}
4534

src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ filegroup(
55
srcs = select_for_scala_version(
66
before_2_12_13 = ["before_2_12_13/DepsTrackingReporter.java"],
77
between_2_12_13_and_2_13_12 = ["after_2_12_13_and_before_2_13_12/DepsTrackingReporter.java"],
8-
since_2_13_12 = ["after_2_13_12/DepsTrackingReporter.java"],
8+
between_2_13_12_and_3 = ["after_2_13_12_and_before_3/DepsTrackingReporter.java"],
9+
since_3 = ["since_3/PlaceholderDepsTrackingReporter.java"],
910
),
1011
visibility = ["//visibility:public"],
1112
)
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.bazel.rulesscala.scalac.reporter;
2+
3+
class PlaceholderDepsTrackingRepoter {}
4+

src/java/io/bazel/rulesscala/scalac/reporter/BUILD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ java_library(
1515
],
1616
between_2_13_12_and_3 = [
1717
"//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter",
18-
"after_2_13_12/ProtoReporter.java",
18+
"after_2_13_12_and_before_3/ProtoReporter.java",
19+
],
20+
since_3 = [
21+
"//src/java/io/bazel/rulesscala/scalac/deps_tracking_reporter",
22+
"since_3/ProtoReporter.java",
1923
],
20-
default = ["PlaceholderForEmptyScala3Lib.java"],
2124
),
2225
visibility = ["//visibility:public"],
2326
deps = [
File renamed without changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.bazel.rulesscala.scalac.reporter;
2+
3+
import dotty.tools.dotc.core.Contexts.Context;
4+
import dotty.tools.dotc.interfaces.SourcePosition;
5+
import dotty.tools.dotc.reporting.AbstractReporter;
6+
import dotty.tools.dotc.reporting.ConsoleReporter;
7+
import dotty.tools.dotc.reporting.Diagnostic;
8+
import dotty.tools.dotc.reporting.Reporter;
9+
import io.bazel.rules_scala.diagnostics.Diagnostics;
10+
import java.io.IOException;
11+
import java.io.PrintWriter;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.StandardOpenOption;
15+
import java.util.*;
16+
17+
public class ProtoReporter extends AbstractReporter {
18+
19+
private final Map<String, List<Diagnostics.Diagnostic>> builder;
20+
private final Reporter delegate;
21+
22+
public ProtoReporter() {
23+
super();
24+
25+
scala.Console$ console = scala.Console$.MODULE$;
26+
this.delegate = new ConsoleReporter(console.in(), new PrintWriter(console.err(), true));
27+
builder = new LinkedHashMap<>();
28+
}
29+
30+
public void writeTo(Path path) throws IOException {
31+
Diagnostics.TargetDiagnostics.Builder targetDiagnostics =
32+
Diagnostics.TargetDiagnostics.newBuilder();
33+
for (Map.Entry<String, List<Diagnostics.Diagnostic>> entry : builder.entrySet()) {
34+
targetDiagnostics.addDiagnostics(
35+
Diagnostics.FileDiagnostics.newBuilder()
36+
.setPath(entry.getKey())
37+
.addAllDiagnostics(entry.getValue()));
38+
}
39+
Files.write(
40+
path,
41+
targetDiagnostics.build().toByteArray(),
42+
StandardOpenOption.CREATE,
43+
StandardOpenOption.APPEND);
44+
}
45+
46+
@Override
47+
public void printSummary(Context ctx) {
48+
delegate.printSummary(ctx);
49+
}
50+
51+
@Override
52+
public void doReport(Diagnostic diag, Context ctx) {
53+
delegate.doReport(diag, ctx);
54+
if (diag.position().isEmpty()) {
55+
return;
56+
}
57+
SourcePosition pos = diag.position().get();
58+
Diagnostics.Diagnostic diagnostic =
59+
Diagnostics.Diagnostic.newBuilder()
60+
.setSeverity(convertSeverity(diag.level()))
61+
.setMessage(diag.message())
62+
.setRange(positionToRange(pos))
63+
.build();
64+
65+
String uri = "workspace-root://" + pos.source().path();
66+
List<Diagnostics.Diagnostic> diagnostics = builder.computeIfAbsent(uri, key -> new ArrayList());
67+
diagnostics.add(diagnostic);
68+
}
69+
70+
private Diagnostics.Severity convertSeverity(int severity) {
71+
if (severity == Diagnostic.ERROR) {
72+
return Diagnostics.Severity.ERROR;
73+
} else if (severity == Diagnostic.WARNING) {
74+
return Diagnostics.Severity.WARNING;
75+
} else if (severity == Diagnostic.INFO) {
76+
return Diagnostics.Severity.INFORMATION;
77+
}
78+
throw new RuntimeException("Unknown severity: " + severity);
79+
}
80+
81+
private Diagnostics.Range positionToRange(SourcePosition pos) {
82+
return Diagnostics.Range.newBuilder()
83+
.setStart(
84+
Diagnostics.Position.newBuilder()
85+
.setLine(pos.startLine())
86+
.setCharacter(pos.startColumn()))
87+
.setEnd(
88+
Diagnostics.Position.newBuilder()
89+
.setLine(pos.endLine())
90+
.setCharacter(pos.endColumn())
91+
.build())
92+
.build();
93+
}
94+
}

0 commit comments

Comments
 (0)