Skip to content

Commit fee073f

Browse files
authored
Merge pull request #52 from lucidsoftware/multiplex-sandbox
Adds multiplex sandbox support and improves argument parsing
2 parents 5eb0406 + 80843d2 commit fee073f

File tree

46 files changed

+1121
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1121
-455
lines changed

.bazelrc_shared

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ build --tool_java_runtime_version="remotejdk_21"
1414
build --strategy=ScalaCompile=worker
1515
build --worker_max_instances=4
1616
build --worker_sandboxing
17+
build --experimental_worker_multiplex_sandboxing
1718
build --verbose_failures
1819

1920
test --test_output=all

rules/private/phases/phase_coverage_jacoco.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ def phase_coverage_jacoco(ctx, g):
4444
outputs = [in_out_pair[1] for in_out_pair in in_out_pairs],
4545
executable = code_coverage_configuration.instrumentation_worker.files_to_run.executable,
4646
input_manifests = worker_input_manifests,
47-
execution_requirements = _resolve_execution_reqs(ctx, {"supports-workers": "1"}),
47+
execution_requirements = _resolve_execution_reqs(
48+
ctx,
49+
{
50+
"supports-multiplex-workers": "1",
51+
"supports-workers": "1",
52+
"supports-multiplex-sandboxing": "1",
53+
},
54+
),
4855
arguments = [args],
4956
)
5057

rules/private/phases/phase_zinc_compile.bzl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ def phase_zinc_compile(ctx, g):
7878
execution_requirements_tags = {
7979
"supports-multiplex-workers": "1",
8080
"supports-workers": "1",
81+
"supports-multiplex-sandboxing": "1",
8182
}
8283

83-
# Disable sandboxing if incremental compilation features are going to be used
84-
# because they require stashing files outside the sandbox that Bazel isn't
85-
# aware of.
84+
# Disable several things if incremental compilation features are going to be used
85+
# because incremental compilation require stashing files outside the sandbox that
86+
# Bazel isn't aware of and is less deterministic than ideal.
8687
if zinc_configuration.incremental:
8788
execution_requirements_tags["no-sandbox"] = "1"
89+
execution_requirements_tags["no-cache"] = "1"
90+
execution_requirements_tags["no-remote"] = "1"
91+
execution_requirements_tags["supports-multiplex-sandboxing"] = "0"
8892

8993
# todo: different execution path for nosrc jar?
9094
ctx.actions.run(

rules/private/phases/phase_zinc_depscheck.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ def phase_zinc_depscheck(ctx, g):
5050
outputs = [deps_check],
5151
executable = deps_configuration.worker.files_to_run.executable,
5252
input_manifests = worker_input_manifests,
53-
execution_requirements = _resolve_execution_reqs(ctx, {"supports-workers": "1"}),
53+
execution_requirements = _resolve_execution_reqs(
54+
ctx,
55+
{
56+
"supports-multiplex-workers": "1",
57+
"supports-workers": "1",
58+
"supports-multiplex-sandboxing": "1",
59+
},
60+
),
5461
arguments = [deps_args],
5562
)
5663
deps_checks[name] = deps_check

rules/scala/private/doc.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ def scaladoc_implementation(ctx):
6262
ctx.actions.run(
6363
arguments = [args],
6464
executable = ctx.attr._runner.files_to_run.executable,
65-
execution_requirements = _resolve_execution_reqs(ctx, {"supports-workers": "1"}),
65+
execution_requirements = _resolve_execution_reqs(
66+
ctx,
67+
{
68+
"supports-multiplex-workers": "1",
69+
"supports-workers": "1",
70+
"supports-multiplex-sandboxing": "1",
71+
},
72+
),
6673
input_manifests = input_manifests,
6774
inputs = depset(
6875
src_jars + srcs + [zinc_configuration.compiler_bridge],

rules/scala_proto/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ scala_binary(
2121
deps = [
2222
"//src/main/scala/higherkindness/rules_scala/common/args",
2323
"//src/main/scala/higherkindness/rules_scala/common/error",
24+
"//src/main/scala/higherkindness/rules_scala/common/sandbox",
2425
"//src/main/scala/higherkindness/rules_scala/common/worker",
2526
"@annex//:net_sourceforge_argparse4j_argparse4j",
2627
"@annex_proto//:com_github_os72_protoc_jar",

rules/scala_proto/private/ScalaProtoWorker.scala

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,52 @@
11
package annex.scala.proto
22

3+
import higherkindness.rules_scala.common.args.ArgsUtil
4+
import higherkindness.rules_scala.common.args.ArgsUtil.PathArgumentType
35
import higherkindness.rules_scala.common.args.implicits._
46
import higherkindness.rules_scala.common.error.AnnexWorkerError
7+
import higherkindness.rules_scala.common.sandbox.SandboxUtil
58
import higherkindness.rules_scala.common.worker.WorkerMain
69
import java.io.{File, PrintStream}
7-
import java.nio.file.{Files, Paths}
10+
import java.nio.file.{Files, Path, Paths}
811
import java.util.Collections
912
import net.sourceforge.argparse4j.ArgumentParsers
1013
import net.sourceforge.argparse4j.impl.Arguments
11-
import net.sourceforge.argparse4j.inf.ArgumentParser
14+
import net.sourceforge.argparse4j.inf.{ArgumentParser, Namespace}
1215
import protocbridge.{ProtocBridge, ProtocRunner}
1316
import scala.jdk.CollectionConverters._
1417
import scalapb.ScalaPbCodeGenerator
1518

1619
object ScalaProtoWorker extends WorkerMain[Unit] {
1720

21+
private class ScalaProtoRequest private (
22+
val isGrpc: Boolean,
23+
val outputDir: Path,
24+
val protoPaths: List[Path],
25+
val sources: List[Path],
26+
)
27+
28+
private object ScalaProtoRequest {
29+
def apply(workDir: Path, namespace: Namespace): ScalaProtoRequest = {
30+
new ScalaProtoRequest(
31+
isGrpc = namespace.getBoolean("grpc"),
32+
outputDir = SandboxUtil.getSandboxPath(workDir, namespace.get[Path]("output_dir")),
33+
protoPaths = SandboxUtil.getSandboxPaths(workDir, namespace.getList[Path]("proto_paths")),
34+
sources = SandboxUtil.getSandboxPaths(workDir, namespace.getList[Path]("sources")),
35+
)
36+
}
37+
}
38+
1839
private val argParser: ArgumentParser = {
1940
val parser = ArgumentParsers.newFor("proto").addHelp(true).fromFilePrefix("@").build
2041
parser
2142
.addArgument("--output_dir")
2243
.help("Output dir")
2344
.metavar("output_dir")
24-
.`type`(Arguments.fileType.verifyCanCreate)
45+
.`type`(PathArgumentType.apply())
2546
parser
2647
.addArgument("--proto_paths")
2748
.nargs("*")
28-
.`type`(Arguments.fileType.verifyIsDirectory)
49+
.`type`(PathArgumentType.apply())
2950
.setDefault_(Collections.emptyList)
3051
parser
3152
.addArgument("--grpc")
@@ -35,28 +56,26 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
3556
.help("Source files")
3657
.metavar("source")
3758
.nargs("*")
38-
.`type`(Arguments.fileType.verifyCanRead.verifyIsFile)
59+
.`type`(PathArgumentType.apply())
3960
.setDefault_(Collections.emptyList)
4061
parser
4162
}
4263

4364
override def init(args: Option[Array[String]]): Unit = ()
4465

45-
protected def work(ctx: Unit, args: Array[String], out: PrintStream): Unit = {
46-
val namespace = argParser.parseArgs(args)
47-
val sources = namespace.getList[File]("sources").asScala.toList
48-
val protoPaths = namespace.getList[File]("proto_paths").asScala.toList
66+
protected def work(ctx: Unit, args: Array[String], out: PrintStream, workDir: Path): Unit = {
67+
val workRequest = ScalaProtoRequest(workDir, ArgsUtil.parseArgsOrFailSafe(args, argParser, out))
4968

50-
val scalaOut = namespace.get[File]("output_dir").toPath
69+
val scalaOut = workRequest.outputDir
5170
Files.createDirectories(scalaOut)
52-
val outOptions = if (namespace.getBoolean("grpc")) {
71+
val outOptions = if (workRequest.isGrpc) {
5372
"grpc:"
5473
} else {
5574
""
5675
}
5776
val params = List(s"--scala_out=${outOptions}${scalaOut}")
58-
::: protoPaths.map(dir => s"--proto_path=${dir.toString}")
59-
::: sources.map(_.getPath.toString)
77+
::: workRequest.protoPaths.map(dir => s"--proto_path=${dir.toString}")
78+
::: workRequest.sources.map(_.toString)
6079

6180
class MyProtocRunner[ExitCode] extends ProtocRunner[Int] {
6281
def run(args: Seq[String], extraEnv: Seq[(String, String)]): Int = {

rules/scala_proto/private/core.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ def scala_proto_library_implementation(ctx):
4949
tools = compiler_inputs,
5050
input_manifests = input_manifests,
5151
progress_message = "Compiling %{label} protobuf into Scala source",
52-
execution_requirements = _resolve_execution_reqs(ctx, {"supports-workers": supports_workers}),
52+
execution_requirements = _resolve_execution_reqs(
53+
ctx,
54+
{
55+
"supports-multiplex-workers": supports_workers,
56+
"supports-workers": supports_workers,
57+
"supports-multiplex-sandboxing": supports_workers,
58+
},
59+
),
5360
arguments = [args],
5461
)
5562

rules/scalafmt/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ scala_binary(
2424
scala = "//src/main/scala:bootstrap",
2525
visibility = ["//visibility:public"],
2626
deps = [
27+
"//src/main/scala/higherkindness/rules_scala/common/sandbox",
2728
"//src/main/scala/higherkindness/rules_scala/common/worker",
2829
"//src/main/scala/higherkindness/rules_scala/workers/common",
2930
"@annex//:net_sourceforge_argparse4j_argparse4j",

rules/scalafmt/private/test.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ def build_format(ctx):
5050
input_manifests = runner_manifests,
5151
inputs = [ctx.file.config, src],
5252
tools = runner_inputs,
53-
execution_requirements = _resolve_execution_reqs(ctx, {"supports-multiplex-workers": "1", "supports-workers": "1"}),
53+
execution_requirements = _resolve_execution_reqs(
54+
ctx,
55+
{
56+
"supports-multiplex-workers": "1",
57+
"supports-workers": "1",
58+
"supports-multiplex-sandboxing": "1",
59+
},
60+
),
5461
mnemonic = "ScalaFmt",
5562
)
5663
manifest_content.append("{} {}".format(src.short_path, file.short_path))

0 commit comments

Comments
 (0)