Skip to content

Commit f32b0cd

Browse files
authored
Merge pull request #80 from lucidsoftware/fix-workermain-return-code-issue
Return the proper exit code when a worker is run directly
2 parents 6c7b46c + 97c46ca commit f32b0cd

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

src/main/scala/higherkindness/rules_scala/common/worker/WorkerMain.scala

+26-17
Original file line numberDiff line numberDiff line change
@@ -218,28 +218,37 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
218218
}
219219

220220
case args =>
221-
Using.Manager { use =>
221+
val returnCode = Using.Manager { use =>
222222
val outStream = use(new ByteArrayOutputStream())
223223
val out = use(new PrintStream(outStream))
224-
try {
225-
work(
226-
init(args = None),
227-
args.toArray,
228-
out,
229-
workDir = Path.of(""),
230-
verbosity = 0,
231-
)
232-
} catch {
233-
// This error means the work function encountered an error that we want to not be caught
234-
// inside that function. That way it stops work and exits the function. However, we
235-
// also don't want to crash the whole program.
236-
case e: AnnexWorkerError => e.print(out)
237-
} finally {
238-
out.flush()
239-
}
224+
val returnCode =
225+
try {
226+
work(
227+
init(args = None),
228+
args.toArray,
229+
out,
230+
workDir = Path.of(""),
231+
verbosity = 0,
232+
)
233+
234+
0
235+
} catch {
236+
// This error means the work function encountered an error that we want to not be caught
237+
// inside that function. That way it stops work and exits the function. However, we
238+
// also don't want to crash the whole program.
239+
case e: AnnexWorkerError =>
240+
e.print(out)
241+
e.code
242+
} finally {
243+
out.flush()
244+
}
240245

241246
outStream.writeTo(System.err)
247+
248+
returnCode
242249
}.get
250+
251+
sys.exit(returnCode)
243252
}
244253
}
245254
}

tests/worker-error/BUILD

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_scala_annex//rules:scala.bzl", "scala_library", "scala_test")
1+
load("@rules_scala_annex//rules:scala.bzl", "scala_binary", "scala_library", "scala_test")
22

33
scala_library(
44
name = "fatal-error-spec-workers",
@@ -48,3 +48,12 @@ scala_test(
4848
"@rules_scala_annex//third_party/bazel/src/main/protobuf:worker_protocol_java_proto",
4949
],
5050
)
51+
52+
scala_binary(
53+
name = "worker-throwing-annexworkererror",
54+
srcs = ["WorkerThrowingAnnexWorkerError.scala"],
55+
deps = [
56+
"@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/error",
57+
"@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/worker",
58+
],
59+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package anx.cancellation
2+
3+
import higherkindness.rules_scala.common.error.AnnexWorkerError
4+
import higherkindness.rules_scala.common.worker.WorkerMain
5+
import java.io.PrintStream
6+
import java.nio.file.Path
7+
8+
object WorkerThrowingAnnexWorkerError extends WorkerMain[Unit] {
9+
override def init(arguments: Option[Array[String]]): Unit = {}
10+
override def work(
11+
context: Unit,
12+
arguments: Array[String],
13+
output: PrintStream,
14+
workingDirectory: Path,
15+
verbosity: Int,
16+
): Unit = throw new AnnexWorkerError(1)
17+
}

tests/worker-error/test

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/bin/bash -e
22
. "$(dirname "$0")"/../common.sh
33

4+
worker_run_directly_returns_proper_exit_code() {
5+
bazel run :worker-throwing-annexworkererror ||
6+
7+
[ $? -eq 1 ]
8+
}
9+
410
bazel test :error-spec
511
bazel test :fatal-error-spec |& grep -q 'java.lang.OutOfMemoryError'
12+
worker_run_directly_returns_proper_exit_code

0 commit comments

Comments
 (0)