Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch macro expansion exceptions in ScalacWorker #1489

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/ScalacWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ private static String[] getPluginParamsFrom(CompileOptions ops) {
return pluginParams.toArray(new String[pluginParams.size()]);
}

private static boolean isMacroException(Throwable ex) {
for (StackTraceElement elem : ex.getStackTrace()) {
if (elem.getMethodName().equals("macroExpand")) {
return true;
}
}
return false;
}

private static void compileScalaSources(CompileOptions ops, String[] scalaSources, Path classes)
throws IOException {

Expand All @@ -269,6 +278,8 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
} catch (Throwable ex) {
if (ex.toString().contains("scala.reflect.internal.Types$TypeError")) {
throw new RuntimeException("Build failure with type error", ex);
} else if (isMacroException(ex)) {
throw new RuntimeException("Build failure during macro expansion", ex);
} else {
throw ex;
}
Expand Down
33 changes: 33 additions & 0 deletions test/shell/test_persistent_worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# shellcheck source=./test_runner.sh

dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
runner=$(get_test_runner "${1:-local}")

PERSISTENT_WORKER_FLAGS="--strategy=Scalac=worker --worker_sandboxing"

test_persistent_worker_success() {
# shellcheck disable=SC2086
bazel build //test:ScalaBinary $PERSISTENT_WORKER_FLAGS
}

test_persistent_worker_failure() {
action_should_fail "build //test_expect_failure/diagnostics_reporter:error_file $PERSISTENT_WORKER_FLAGS"
}

test_persistent_worker_handles_exception_in_macro_invocation() {
command="bazel build //test_expect_failure/scalac_exceptions:bad_macro_invocation $PERSISTENT_WORKER_FLAGS"
output=$(${command} 2>&1)
! (echo "$output" | grep -q -- "---8<---8<---") && (echo "$output" | grep -q "Build failure during macro expansion")

RESPONSE_CODE=$?
if [ $RESPONSE_CODE -ne 0 ]; then
echo -e "${RED} Scalac persistent worker does not handle uncaught error in macro expansion. $NC"
exit 1
fi
}

$runner test_persistent_worker_success
$runner test_persistent_worker_failure
$runner test_persistent_worker_handles_exception_in_macro_invocation
12 changes: 12 additions & 0 deletions test_expect_failure/scalac_exceptions/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//scala:scala.bzl", "scala_library", "scala_macro_library")

scala_macro_library(
name = "bad_macro",
srcs = ["BadMacro.scala"],
)

scala_library(
name = "bad_macro_invocation",
srcs = ["BadMacroInvocation.scala"],
deps = [":bad_macro"],
)
9 changes: 9 additions & 0 deletions test_expect_failure/scalac_exceptions/BadMacro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.language.experimental.macros

object BadMacro {
def badMacro(): Unit = macro badMacroImpl

def badMacroImpl(c: scala.reflect.macros.blackbox.Context)(): c.Tree = {
throw new NoSuchMethodError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object BadMacroInvocation {
BadMacro.badMacro()
}
1 change: 1 addition & 0 deletions test_rules_scala.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ $runner bazel build //test_statsfile:SimpleNoStatsFile_statsfile --extra_toolcha
. "${test_dir}"/test_compiler_dependency_tracking.sh
. "${test_dir}"/test_twitter_scrooge.sh
. "${test_dir}"/test_inherited_environment.sh
. "${test_dir}"/test_persistent_worker.sh