Skip to content

Commit b827c3d

Browse files
author
Jaden Peterson
committed
fixup! Catch ClassFormatErrors in ScalacInvoker
1 parent cc2f0d8 commit b827c3d

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private static String[] getPluginParamsFrom(CompileOptions ops) {
237237
}
238238
if (ops.directTargets.length > 0) {
239239
pluginParams.add(
240-
"-P:dependency-analyzer:direct-targets:" + encodeStringSeqPluginParam(ops.directTargets));
240+
"-P:dependency-analyzer:direct-targets:" + encodeStringSeqPluginParam(ops.directTargets));
241241
}
242242
if (ops.indirectJars.length > 0) {
243243
pluginParams.add(
@@ -269,20 +269,7 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
269269
String[] compilerArgs =
270270
merge(ops.scalaOpts, pluginArgs, constParams, pluginParams, scalaSources);
271271

272-
ScalacInvokerResults compilerResults;
273-
274-
try {
275-
compilerResults = ScalacInvoker.invokeCompiler(ops, compilerArgs);
276-
} catch (CompilationFailed exception) {
277-
if (exception.getCause() instanceof ClassFormatError) {
278-
throw new Exception(
279-
"You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target.",
280-
exception
281-
);
282-
}
283-
284-
throw exception;
285-
}
272+
ScalacInvokerResults compilerResults = ScalacInvoker.invokeCompiler(ops, compilerArgs);
286273

287274
if (ops.printCompileTime) {
288275
System.err.println("Compiler runtime: " + (compilerResults.stopTime - compilerResults.startTime) + "ms.");

src/java/io/bazel/rulesscala/scalac/scala_2/ScalacInvoker.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
//Invokes Scala 2 compiler
1313
class ScalacInvoker{
14-
14+
1515
public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
1616
throws IOException, Exception{
1717

1818
ReportableMainClass comp = new ReportableMainClass(ops);
1919

2020
ScalacInvokerResults results = new ScalacInvokerResults();
21-
21+
2222
results.startTime = System.currentTimeMillis();
2323
try {
2424
comp.process(compilerArgs);
@@ -28,7 +28,15 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
2828
} else if (ex.toString().contains("java.lang.StackOverflowError")) {
2929
throw new ScalacWorker.CompilationFailed("with StackOverflowError", ex);
3030
} else if (isMacroException(ex)) {
31-
throw new ScalacWorker.CompilationFailed("during macro expansion", ex);
31+
String reason;
32+
33+
if (ex instanceof ClassFormatError) {
34+
reason = "during macro expansion. You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target";
35+
} else {
36+
reason = "during macro expansion";
37+
}
38+
39+
throw new ScalacWorker.CompilationFailed(reason, ex);
3240
} else {
3341
throw ex;
3442
}
@@ -37,7 +45,7 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
3745
}
3846

3947
results.stopTime = System.currentTimeMillis();
40-
48+
4149
ConsoleReporter reporter = (ConsoleReporter) comp.getReporter();
4250
if (reporter == null) {
4351
// Can happen only when `ReportableMainClass::newCompiler` was not invoked,

test/shell/test_macros.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ runner=$(get_test_runner "${1:-local}")
66

77
incorrect_macro_user_does_not_build() {
88
(! bazel build //test/macros:incorrect-macro-user) |&
9-
grep --fixed-strings 'java.lang.Exception: You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target.'
9+
grep --fixed-strings 'Build failure during macro expansion. You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target'
1010
}
1111

1212
correct_macro_user_builds() {

0 commit comments

Comments
 (0)