Skip to content

Commit d4ec354

Browse files
authored
Merge pull request #4 from CodinGame/fix-error-outside-sources
Fix crash when a compilation error/warning occurs outside provided sources
2 parents 458f95c + 6a09569 commit d4ec354

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/main/java/com/codingame/codemachine/compiler/java/CodinGameJavaCompiler.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public static void main(String... args) throws IOException {
5353
compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits);
5454
boolean success = task.call();
5555
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) {
56-
57-
LineNumberReader reader = new LineNumberReader(new StringReader(diagnostic.getSource().getCharContent(true).toString()));
58-
String line = reader.lines().skip(diagnostic.getLineNumber() - 1).limit(1).findAny().get();
59-
6056
String type = null;
6157
switch (diagnostic.getKind()) {
6258
case ERROR:
@@ -73,21 +69,34 @@ public static void main(String... args) throws IOException {
7369
continue;
7470
}
7571

76-
System.err.println(String.format("%s:%d: %s: %s\n%s\n%"+diagnostic.getColumnNumber()+"s",
77-
diagnostic.getSource().getName(),
78-
diagnostic.getLineNumber(),
79-
diagnostic.getKind().name().toLowerCase(),
80-
diagnostic.getMessage(null),
81-
line,
82-
"^"
83-
));
72+
if (diagnostic.getLineNumber() >= 0 && diagnostic.getColumnNumber() >= 0) {
73+
LineNumberReader reader = new LineNumberReader(new StringReader(diagnostic.getSource().getCharContent(true).toString()));
74+
String line = reader.lines().skip(diagnostic.getLineNumber() - 1).limit(1).findAny().get();
75+
76+
System.err.println(String.format("%s:%d: %s: %s\n%s\n%"+diagnostic.getColumnNumber()+"s",
77+
diagnostic.getSource().getName(),
78+
diagnostic.getLineNumber(),
79+
diagnostic.getKind().name().toLowerCase(),
80+
diagnostic.getMessage(null),
81+
line,
82+
"^"
83+
));
84+
} else {
85+
System.err.println(String.format("%s: %s: %s",
86+
diagnostic.getSource().getName(),
87+
diagnostic.getKind().name().toLowerCase(),
88+
diagnostic.getMessage(null)
89+
));
90+
}
8491

85-
System.out.println(String.format("CG> annotate --type \"%s\" --file \"%s\" --position \"%s\" --message \"%s\"",
86-
type,
87-
diagnostic.getSource().getName(),
88-
diagnostic.getLineNumber()+":"+diagnostic.getColumnNumber(),
89-
diagnostic.getMessage(null).replaceAll("\"","\\\"")
90-
));
92+
if (files.contains(diagnostic.getSource().getName())) {
93+
System.out.println(String.format("CG> annotate --type \"%s\" --file \"%s\" --position \"%s\" --message \"%s\"",
94+
type,
95+
diagnostic.getSource().getName(),
96+
diagnostic.getLineNumber()+":"+diagnostic.getColumnNumber(),
97+
diagnostic.getMessage(null).replaceAll("\"","\\\"")
98+
));
99+
}
91100
}
92101
resultCode = success ? 0 : 1;
93102
}

0 commit comments

Comments
 (0)