Add a REPL for each Scala target, for debugging. (#9643)

* Add a REPL for each Scala target, for debugging.

For each Scala library target, this adds a `*_repl` target that can be
built and run to provide a Scala REPL with access to the library and all
its dependencies.

To use:

```
$ bazel build //ledger/ledger-on-memory:ledger-on-memory_repl
INFO: Invocation ID: f1c4ec07-68e7-4bc2-a182-40f6ea035110
INFO: Analyzed target //ledger/ledger-on-memory:ledger-on-memory_repl (32 packages loaded, 714 targets configured).
INFO: Found 1 target...
Target //ledger/ledger-on-memory:ledger-on-memory_repl up-to-date:
  bazel-bin/ledger/ledger-on-memory/ledger-on-memory_repl.jar
  bazel-bin/ledger/ledger-on-memory/ledger-on-memory_repl
INFO: Build completed successfully, 7 total actions

$ ./bazel-bin/ledger/ledger-on-memory/ledger-on-memory_repl
Welcome to Scala 2.12.13 (OpenJDK 64-Bit Server VM, Java 1.8.0_272).
Type in expressions for evaluation. Or try :help.

scala> import scala.concurrent.ExecutionContext.Implicits.global
scala> import com.google.protobuf.ByteString
scala> import com.daml.ledger.on.memory._
scala> import com.daml.ledger.participant.state.kvutils.OffsetBuilder
scala> import com.daml.ledger.participant.state.kvutils.Raw
scala> import com.daml.ledger.participant.state.kvutils.api.LedgerRecord

scala> val state = InMemoryState.empty
state: com.daml.ledger.on.memory.InMemoryState = com.daml.ledger.on.memory.InMemoryState@ff21443

scala> state.write { (log, state) => Future { log += LedgerRecord(OffsetBuilder.fromLong(1), Raw.LogEntryId(ByteString.copyFromUtf8("A")), Raw.Envelope(ByteString.EMPTY)) } }
```

The REPL target has the tag "manual", so will only be built on demand.

CHANGELOG_BEGIN
CHANGELOG_END

* Turns out you can run a REPL with `bazel run`.
This commit is contained in:
Samir Talwar 2021-05-11 16:11:58 +02:00 committed by GitHub
parent 22b36b0b01
commit d3106682dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View File

@ -464,6 +464,14 @@ detailed information.
bazel run //ledger/sandbox:sandbox-binary -- --help
```
### Running a REPL
- Run a Scala REPL which has the library on the classpath:
```
bazel run //ledger/ledger-on-memory:ledger-on-memory_repl
```
### Querying Targets
The Bazel query language is described in detail in the [official Bazel

View File

@ -6,6 +6,7 @@ load(
"scala_binary",
"scala_library",
"scala_library_suite",
"scala_repl",
"scala_test",
"scala_test_suite",
)
@ -222,11 +223,21 @@ def _wrap_rule(
**kwargs
)
def _wrap_rule_no_plugins(rule, deps = [], scala_deps = [], versioned_scala_deps = {}, scalacopts = [], **kwargs):
def _wrap_rule_no_plugins(
rule,
deps = [],
scala_deps = [],
versioned_scala_deps = {},
runtime_deps = [],
scala_runtime_deps = [],
scalacopts = [],
**kwargs):
deps = resolve_scala_deps(deps, scala_deps, versioned_scala_deps)
runtime_deps = resolve_scala_deps(runtime_deps, scala_runtime_deps)
rule(
scalacopts = common_scalacopts + scalacopts,
deps = deps,
runtime_deps = runtime_deps,
**kwargs
)
@ -511,6 +522,27 @@ def _create_scaladoc_jar(name, srcs, plugins = [], deps = [], scala_deps = [], v
tags = ["scaladoc"],
)
def _create_scala_repl(
name,
deps = [],
scala_deps = [],
versioned_scala_deps = {},
runtime_deps = [],
scala_runtime_deps = [],
tags = [],
# hiding the following from the `scala_repl` rule
main_class = None,
exports = None,
scala_exports = None,
scalac_opts = None,
generated_srcs = None,
**kwargs):
name = name + "_repl"
deps = resolve_scala_deps(deps, scala_deps, versioned_scala_deps)
runtime_deps = resolve_scala_deps(runtime_deps, scala_runtime_deps)
tags = tags + ["manual"]
scala_repl(name = name, deps = deps, runtime_deps = runtime_deps, tags = tags, **kwargs)
def da_scala_library(name, **kwargs):
"""
Define a Scala library.
@ -527,8 +559,8 @@ def da_scala_library(name, **kwargs):
arguments = _set_compile_jvm_flags(arguments)
_wrap_rule(scala_library, name, **arguments)
_create_scala_source_jar(name = name, **arguments)
_create_scaladoc_jar(name = name, **arguments)
_create_scala_repl(name = name, **kwargs)
if "tags" in arguments:
for tag in arguments["tags"]: