Skip to content

Add Hocon toolchain, worker support, update rules_scala_annex and Bazel#34

Open
jjudd wants to merge 1 commit into
masterfrom
jjudd-annex-toolchain-update
Open

Add Hocon toolchain, worker support, update rules_scala_annex and Bazel#34
jjudd wants to merge 1 commit into
masterfrom
jjudd-annex-toolchain-update

Conversation

@jjudd

@jjudd jjudd commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@jjudd jjudd force-pushed the jjudd-annex-toolchain-update branch from 93f5471 to f4bcb00 Compare June 12, 2026 19:21
import higherkindness.rules_scala.common.worker.{WorkTask, WorkerMain}
import java.io.{File, FileWriter, PrintStream}
import java.nio.file.{Files, Path, Paths}
import org.rogach.scallop.*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use argparse4j in rules_scala_annex, scopt in our private monorepo, and scallop here. Out of curiosity, why'd you opt for that framework instead of argparse4j or scopt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scallop was already used by the existing code. I didn't change that bit. This diff makes it look like this file is entirely new, but it's about 80% copied from the old implementation.

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

protected def work(task: WorkTask[Unit]): Unit = {
// WorkerMain does not expand param files, so we do it here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to do this in this PR, but we should really consider doing this automatically in WorkerMain.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It's something we do often enough it'd be nice if it did it automagically. Not something I want to do as part of this PR, though.


writeConfig(finalConfig, renderOptions, opts.output(), opts.header)
} catch {
case NonFatal(e) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe WorkerMain already catches exceptions in work, so this can be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any harm in including it, though. If you haven't read through WorkerMain or your'e an LLM that doesn't know about it, then it would likely look like an error to throw uncaught exceptions.

val builder = Set.newBuilder[String]
val source = scala.io.Source.fromFile(file)
try {
for (line <- source.getLines()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not just do source.getLines().toSet?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you copied this from the old Compiler, so feel free to ignore this comment if it's out of scope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try and see if it causes any trouble.

@tmccombs tmccombs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand all of the bazel stuff, but it looks ok to me

load(":create-toolchain.bzl", "create_hocon_toolchain")

toolchain_type(
name = "toolchain_type",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with bazel toolchains, but this seems like an odd name to me.

"""Defines and configures a Hocon toolchain.

Args:
name: Name of the generated `toolchain` target. Register it with `register_toolchains`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do we register it?

Comment thread scala/BUILD.bazel
"@hocon_maven//:org_scala_lang_scala_compiler",
"@hocon_maven//:org_scala_lang_scala_library",
"@hocon_maven//:org_scala_lang_scala_reflect",
compiler_classpath_3 = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really worht adding the "_3" suffixes to these?

Comment thread scripts/format.sh

_scalafmt() {
find hocon-compiler -name '*.scala' -exec $scalafmtbin --config "$PWD/.scalafmt.conf" "$PWD/{}" "$PWD/{}" \;
find hocon-compiler-cli -name '*.scala' -exec $scalafmtbin --config "$PWD/.scalafmt.conf" "$PWD/{}" "$PWD/{}" \;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a significance of the "-cli" suffix?

Comment thread rules/hocon.bzl
output = header_file,
content = ctx.attr.header,
)
args.add("-h", header_file)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be better to use a different option for this. lik --header or -H, sicne -h is conventionally used for printing help info.

Comment on lines +24 to +30
val base = opt[File]().map(SandboxUtil.getSandboxFile(workDir, _))
val output = opt[File](required = true).map(SandboxUtil.getSandboxFile(workDir, _))
val include = opt[List[File]](default = Some(Nil)).map(_.map(SandboxUtil.getSandboxFile(workDir, _)))
val envKeyLists = opt[List[File]](
"env-key-lists",
default = Some(Nil),
).map(_.map(SandboxUtil.getSandboxFile(workDir, _)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth making a local helper:

val inSandbox = (file: File) => SandboxUtil.getSandboxFile(workDir, file)

so all these could be written as

_.map(inSandbox)

?

// top-level command, so render the full help from the builder (as Scallop's default does).
val helpText =
if (subcommand.isEmpty) builder.getFullHelpString()
else builder.findSubbuilder(subcommand).get.getFullHelpString()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have any subcommands?

val includes = opts.include()
val configParser = new ConfigParser(includes, opts.optionalInclude())
val baseConfig = opts.base.toOption.map(configParser.parse)
val mainConfig = configParser.parse(opts.src())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth doing a throwIfInterrupted between parsing and merging?

Comment thread tests/jvm_flags/test.sh

set -euo pipefail

cd "$(bazel info workspace 2> /dev/null)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this actually necessary, since we run the test using an absolute label?

Comment thread BUILD.bazel
)

default_java_toolchain(
name = "repository_default_toolchain_21",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we upgrade to 25?

maybe in a separate PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants