Skip to content

Commit 394c0aa

Browse files
authored
Catch macro expansion exceptions in ScalacWorker (#1489)
* Catch macro expansion exceptions in ScalacWorker * E2E test for persistent worker * Add new test to test_rules_scala.sh
1 parent 068122a commit 394c0aa

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ private static String[] getPluginParamsFrom(CompileOptions ops) {
248248
return pluginParams.toArray(new String[pluginParams.size()]);
249249
}
250250

251+
private static boolean isMacroException(Throwable ex) {
252+
for (StackTraceElement elem : ex.getStackTrace()) {
253+
if (elem.getMethodName().equals("macroExpand")) {
254+
return true;
255+
}
256+
}
257+
return false;
258+
}
259+
251260
private static void compileScalaSources(CompileOptions ops, String[] scalaSources, Path classes)
252261
throws IOException {
253262

@@ -269,6 +278,8 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
269278
} catch (Throwable ex) {
270279
if (ex.toString().contains("scala.reflect.internal.Types$TypeError")) {
271280
throw new RuntimeException("Build failure with type error", ex);
281+
} else if (isMacroException(ex)) {
282+
throw new RuntimeException("Build failure during macro expansion", ex);
272283
} else {
273284
throw ex;
274285
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# shellcheck source=./test_runner.sh
2+
3+
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
4+
. "${dir}"/test_runner.sh
5+
. "${dir}"/test_helper.sh
6+
runner=$(get_test_runner "${1:-local}")
7+
8+
PERSISTENT_WORKER_FLAGS="--strategy=Scalac=worker --worker_sandboxing"
9+
10+
test_persistent_worker_success() {
11+
# shellcheck disable=SC2086
12+
bazel build //test:ScalaBinary $PERSISTENT_WORKER_FLAGS
13+
}
14+
15+
test_persistent_worker_failure() {
16+
action_should_fail "build //test_expect_failure/diagnostics_reporter:error_file $PERSISTENT_WORKER_FLAGS"
17+
}
18+
19+
test_persistent_worker_handles_exception_in_macro_invocation() {
20+
command="bazel build //test_expect_failure/scalac_exceptions:bad_macro_invocation $PERSISTENT_WORKER_FLAGS"
21+
output=$(${command} 2>&1)
22+
! (echo "$output" | grep -q -- "---8<---8<---") && (echo "$output" | grep -q "Build failure during macro expansion")
23+
24+
RESPONSE_CODE=$?
25+
if [ $RESPONSE_CODE -ne 0 ]; then
26+
echo -e "${RED} Scalac persistent worker does not handle uncaught error in macro expansion. $NC"
27+
exit 1
28+
fi
29+
}
30+
31+
$runner test_persistent_worker_success
32+
$runner test_persistent_worker_failure
33+
$runner test_persistent_worker_handles_exception_in_macro_invocation
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("//scala:scala.bzl", "scala_library", "scala_macro_library")
2+
3+
scala_macro_library(
4+
name = "bad_macro",
5+
srcs = ["BadMacro.scala"],
6+
)
7+
8+
scala_library(
9+
name = "bad_macro_invocation",
10+
srcs = ["BadMacroInvocation.scala"],
11+
deps = [":bad_macro"],
12+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.language.experimental.macros
2+
3+
object BadMacro {
4+
def badMacro(): Unit = macro badMacroImpl
5+
6+
def badMacroImpl(c: scala.reflect.macros.blackbox.Context)(): c.Tree = {
7+
throw new NoSuchMethodError()
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object BadMacroInvocation {
2+
BadMacro.badMacro()
3+
}

test_rules_scala.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ $runner bazel build //test_statsfile:SimpleNoStatsFile_statsfile --extra_toolcha
5555
. "${test_dir}"/test_compiler_dependency_tracking.sh
5656
. "${test_dir}"/test_twitter_scrooge.sh
5757
. "${test_dir}"/test_inherited_environment.sh
58+
. "${test_dir}"/test_persistent_worker.sh

0 commit comments

Comments
 (0)