Skip to content

Commit

Permalink
fixup! Catch ClassFormatErrors in ScalacInvoker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaden Peterson committed Jan 22, 2025
1 parent cc2f0d8 commit b827c3d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
17 changes: 2 additions & 15 deletions src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private static String[] getPluginParamsFrom(CompileOptions ops) {
}
if (ops.directTargets.length > 0) {
pluginParams.add(
"-P:dependency-analyzer:direct-targets:" + encodeStringSeqPluginParam(ops.directTargets));
"-P:dependency-analyzer:direct-targets:" + encodeStringSeqPluginParam(ops.directTargets));
}
if (ops.indirectJars.length > 0) {
pluginParams.add(
Expand Down Expand Up @@ -269,20 +269,7 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
String[] compilerArgs =
merge(ops.scalaOpts, pluginArgs, constParams, pluginParams, scalaSources);

ScalacInvokerResults compilerResults;

try {
compilerResults = ScalacInvoker.invokeCompiler(ops, compilerArgs);
} catch (CompilationFailed exception) {
if (exception.getCause() instanceof ClassFormatError) {
throw new Exception(
"You may have declared a target containing a macro as a `scala_library` target instead of a `scala_macro_library` target.",
exception
);
}

throw exception;
}
ScalacInvokerResults compilerResults = ScalacInvoker.invokeCompiler(ops, compilerArgs);

if (ops.printCompileTime) {
System.err.println("Compiler runtime: " + (compilerResults.stopTime - compilerResults.startTime) + "ms.");
Expand Down
16 changes: 12 additions & 4 deletions src/java/io/bazel/rulesscala/scalac/scala_2/ScalacInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

//Invokes Scala 2 compiler
class ScalacInvoker{

public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] compilerArgs)
throws IOException, Exception{

ReportableMainClass comp = new ReportableMainClass(ops);

ScalacInvokerResults results = new ScalacInvokerResults();

results.startTime = System.currentTimeMillis();
try {
comp.process(compilerArgs);
Expand All @@ -28,7 +28,15 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
} else if (ex.toString().contains("java.lang.StackOverflowError")) {
throw new ScalacWorker.CompilationFailed("with StackOverflowError", ex);
} else if (isMacroException(ex)) {
throw new ScalacWorker.CompilationFailed("during macro expansion", ex);
String reason;

if (ex instanceof ClassFormatError) {
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";
} else {
reason = "during macro expansion";
}

throw new ScalacWorker.CompilationFailed(reason, ex);
} else {
throw ex;
}
Expand All @@ -37,7 +45,7 @@ public static ScalacInvokerResults invokeCompiler(CompileOptions ops, String[] c
}

results.stopTime = System.currentTimeMillis();

ConsoleReporter reporter = (ConsoleReporter) comp.getReporter();
if (reporter == null) {
// Can happen only when `ReportableMainClass::newCompiler` was not invoked,
Expand Down
2 changes: 1 addition & 1 deletion test/shell/test_macros.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ runner=$(get_test_runner "${1:-local}")

incorrect_macro_user_does_not_build() {
(! bazel build //test/macros:incorrect-macro-user) |&
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.'
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'
}

correct_macro_user_builds() {
Expand Down

0 comments on commit b827c3d

Please sign in to comment.