Skip to content

Commit

Permalink
WIP: Bzlmod part the thirty-fifth
Browse files Browse the repository at this point in the history
As it turns out, we're using a very old version of Scalafmt that breaks
completely:

```txt
ERROR: rules_scala/test/scalafmt/BUILD:43:20:
  ScalaFmt test/scalafmt/test/scalafmt/formatted/formatted-test.scala.fmt.output failed:
  (Exit 1): scalafmt failed: error executing ScalaFmt command
  (from target //test/scalafmt:formatted-test)
bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/scala/scalafmt/scalafmt
  '--jvm_flag=-Dfile.encoding=UTF-8' ... (remaining 1 argument skipped)

java.util.NoSuchElementException: last of empty IndexedSeq
```

This matches:

- scala/community-build#1680

Which mentions apparent fixes in:

- scalameta/scalameta#3235
- scalameta/scalafmt#3581

So the fix is to update Scalafmt, but given how we don't use
rules_jvm_external, that means a lot of manual updates to
third_party/repositories/scala_*.bzl. There doesn't appear to be a way
to automate this; there's no indication that the most recent update was
automated in any way:

- bazelbuild#1543

So I'll plow through all the JARs and make the necessary changes:

- https://mvnrepository.com/artifact/org.scalameta/scalafmt-core

I can't update scala_2_11, since there's not a more recent compatible
version. But the changes to scala_2_12 and scala_2_13 should be similar,
and the changes to 2_13 should apply to 3_{1,2,3,4} as well.

I had to hack .scalafmt.conf and ScalafmtWorker.scala a bit. This may
make it incompatible with 2.11.
  • Loading branch information
mbland committed Oct 1, 2024
1 parent 59d05e2 commit b05ea3a
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 184 deletions.
4 changes: 2 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ danglingParentheses.preset = true
docstrings.style = Asterisk
importSelectors = singleLine
maxColumn = 120
verticalMultiline.newlineBeforeImplicitKW = true
#verticalMultiline.newlineBeforeImplicitKW = true
rewrite.redundantBraces.stringInterpolation = true
rewrite.rules = [
RedundantParens,
PreferCurlyFors,
SortImports
]
unindentTopLevelOperators = false
#unindentTopLevelOperators = false
lineEndings=preserve
441 changes: 299 additions & 142 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scala/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("//scala:scala.bzl", "scala_binary")
load("//scala:scala_cross_version.bzl", "version_suffix")
load("//scala/scalafmt/toolchain:toolchain.bzl", "export_scalafmt_deps")
load("//scala/scalafmt/toolchain:setup_scalafmt_toolchain.bzl", "setup_scalafmt_toolchains")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load("//scala/scalafmt/toolchain:setup_scalafmt_toolchain.bzl", "setup_scalafmt_toolchains")
load(
"//scala/scalafmt:phase_scalafmt_ext.bzl",
"scalafmt_singleton",
Expand Down
13 changes: 9 additions & 4 deletions scala/scalafmt/scalafmt/ScalafmtWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import io.bazel.rulesscala.worker.Worker
import java.io.File
import java.nio.file.Files
import org.scalafmt.Scalafmt
import org.scalafmt.config.Config
import org.scalafmt.util.FileOps
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.sysops.FileOps
import scala.annotation.tailrec
import scala.io.Codec

Expand All @@ -18,9 +18,14 @@ object ScalafmtWorker extends Worker.Interface {
val argFile = args.map{x => new File(x)}
val namespace = argName.zip(argFile).toMap

val source = FileOps.readFile(namespace.getOrElse("input", new File("")))(Codec.UTF8)
val source = FileOps.readFile(
namespace.getOrElse("input", new File("")).toPath()
)(Codec.UTF8)

val config = ScalafmtConfig.fromHoconFile(
namespace.getOrElse("config", new File("")).toPath()
).get

val config = Config.fromHoconFile(namespace.getOrElse("config", new File(""))).get
@tailrec
def format(code: String): String = {
val formatted = Scalafmt.format(code, config).get
Expand Down
10 changes: 9 additions & 1 deletion scala/scalafmt/scalafmt_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ _SCALAFMT_DEPS = [
"com_geirsson_metaconfig_typesafe_config",
]

_SCALAFMT_DEPS_2_11 = [
"com_geirsson_metaconfig_pprint",
"org_scalameta_mdoc_parser",
"org_scalameta_scalafmt_config",
"org_scalameta_scalafmt_sysops",
]

def _artifact_ids(scala_version):
major_version = extract_major_version(scala_version)
extra_deps = _SCALAFMT_DEPS_2_11 if major_version != "2.11" else []
geny = ["com_lihaoyi_geny"] if major_version != "2.11" else []
parallel_collections = ["io_bazel_rules_scala_scala_parallel_collections"] if major_version == "2.13" or major_version.startswith("3") else []
return _SCALAFMT_DEPS + geny + parallel_collections
return _SCALAFMT_DEPS + extra_deps + geny + parallel_collections

def scalafmt_repositories(
maven_servers = _default_maven_server_urls(),
Expand Down
14 changes: 12 additions & 2 deletions scala/scalafmt/toolchain/setup_scalafmt_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("//scala:providers.bzl", "declare_deps_provider")
load("//scala:scala_cross_version.bzl", "version_suffix")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")

_SCALAFMT_DEPS = [
_SCALAFMT_DEPS_2_11 = [
"@com_geirsson_metaconfig_core",
"@org_scalameta_common",
"@org_scalameta_parsers",
Expand All @@ -12,6 +12,12 @@ _SCALAFMT_DEPS = [
"@org_scalameta_trees",
]

_SCALAFMT_DEPS = _SCALAFMT_DEPS_2_11 + [
"@org_scalameta_mdoc_parser",
"@org_scalameta_scalafmt_config",
"@org_scalameta_scalafmt_sysops",
]

def setup_scalafmt_toolchain(
name,
scalafmt_classpath,
Expand Down Expand Up @@ -48,4 +54,8 @@ def setup_scalafmt_toolchains():
)

def _deps(scala_version):
return [dep + version_suffix(scala_version) for dep in _SCALAFMT_DEPS]
deps = _SCALAFMT_DEPS
if scala_version.startswith("2.11"):
deps = _SCALAFMT_DEPS_2_11

return [dep + version_suffix(scala_version) for dep in deps]
97 changes: 65 additions & 32 deletions third_party/repositories/scala_2_13.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ artifacts = {
"sha256": "8846baaa8cf43b1b19725ab737abff145ca58d14a4d02e75d71ca8f7ca5f2926",
},
"io_bazel_rules_scala_scala_parallel_collections": {
"artifact": "org.scala-lang.modules:scala-parallel-collections_2.13:1.0.3",
"sha256": "5b9f705652d14005cdc535270547305a4e41d3664f6d15c21b7e0c807f8d6605",
"artifact": "org.scala-lang.modules:scala-parallel-collections_2.13:1.0.4",
"sha256": "68f266c4fa37cb20a76e905ad940e241190ce288b7e4a9877f1dd1261cd1a9a7",
},
"io_bazel_rules_scala_scalatest": {
"artifact": "org.scalatest:scalatest_2.13:3.2.9",
Expand Down Expand Up @@ -74,8 +74,8 @@ artifacts = {
"sha256": "5c285b72e6dc0a98e99ae0a1ceeb4027dab9adfa441844046bd3f19e0efdcb54",
},
"org_scalameta_common": {
"artifact": "org.scalameta:common_2.13:4.4.27",
"sha256": "882d82f7f547aec5d9b55ef3173188ce3d55b28272cd9e36b6558f55e1389d26",
"artifact": "org.scalameta:common_2.13:4.10.1",
"sha256": "501df5667f192ae2338e8ba3a4559225858b3a75ad6788889af21f5a8946abac",
"deps": [
"@com_lihaoyi_sourcecode",
"@io_bazel_rules_scala_scala_library",
Expand Down Expand Up @@ -109,60 +109,82 @@ artifacts = {
"sha256": "ca3857a3f95266e0d87e1a1f26c8592c53c12ac7203f911759415f6c8a43df7d",
},
"org_scala_lang_modules_scala_collection_compat": {
"artifact": "org.scala-lang.modules:scala-collection-compat_2.13:2.4.3",
"sha256": "7f71f1404ce6b54b2f854b0f6c5a5e06c0d915043e44b697a25adf2da573a09e",
"artifact": "org.scala-lang.modules:scala-collection-compat_2.13:2.11.0",
"sha256": "0c1108883b7b97851750e8932f9584346ccb23f1260c197f97295ac2e6c56cec",
"deps": [
"@io_bazel_rules_scala_scala_library",
],
},
"org_scalameta_mdoc_parser": {
"artifact": "org.scalameta:mdoc-parser_2.13:2.6.1",
"sha256": "93c4b6628219ccea71cdf190b11967e313daa276f265f0ad1097c62ec4a9fcec",
"deps": [
"@io_bazel_rules_scala_scala_library",
],
},
"org_scalameta_parsers": {
"artifact": "org.scalameta:parsers_2.13:4.4.27",
"sha256": "f375cbda5c0ee65bcd9af8a9a2a2afb042ca244d79880db904aa03c98a0d7553",
"artifact": "org.scalameta:parsers_2.13:4.10.1",
"sha256": "657b2465014a7b75fbf98b6179b44888396f4ab201f404a843c1743a775d2e64",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@org_scalameta_trees",
],
},
"org_scalameta_scalafmt_core": {
"artifact": "org.scalameta:scalafmt-core_2.13:3.0.0",
"sha256": "c71697f9b6bf41109f7f31ddddd924198b0769e94240aad2aa05006071607b1e",
"org_scalameta_scalafmt_config": {
"artifact": "org.scalameta:scalafmt-config_2.13:3.8.3",
"sha256": "175c7e345baccb75e0f79aa9d8c821834b4b232d3917039c44ca2f0265f2110a",
"deps": [
"@com_geirsson_metaconfig_core",
"@com_geirsson_metaconfig_typesafe_config",
],
},
"org_scalameta_scalafmt_core": {
"artifact": "org.scalameta:scalafmt-core_2.13:3.8.3",
"sha256": "c214d16a746ceab8ac47b97c18d2817f726174dd58da75d43472d045ddc25009",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@org_scalameta_mdoc_parser",
"@org_scalameta_scalafmt_config",
"@org_scalameta_scalafmt_sysops",
"@org_scalameta_scalameta",
],
},
"org_scalameta_scalafmt_sysops": {
"artifact": "org.scalameta:scalafmt-sysops_2.13:3.8.3",
"sha256": "981b5455b956ece0e7f2c0825241c6f99b2d70cc2352700a2fcffa5c01ed6633",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_parallel_collections",
],
},
"org_scalameta_scalameta": {
"artifact": "org.scalameta:scalameta_2.13:4.4.27",
"sha256": "878cfea72e1df90bf8a49cdac0deed8226b0b96d11d537131400f0e17efcd1f5",
"artifact": "org.scalameta:scalameta_2.13:4.10.1",
"sha256": "52abf9be27c6d609497e3b587675d603f708dd92237bd34a8a567c5756d16328",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@org_scala_lang_scalap",
"@org_scalameta_parsers",
],
},
"org_scalameta_trees": {
"artifact": "org.scalameta:trees_2.13:4.4.27",
"sha256": "9a78a879c0fa88cdfed6e2dc83c47c3d0f9994927be84dffa44f0bbe18ca311a",
"artifact": "org.scalameta:trees_2.13:4.10.1",
"sha256": "5b8701aecfb4474febd0e0be481eb2c22a1c2fb95a23d4dd604b175808dd9092",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@org_scalameta_common",
"@org_scalameta_fastparse",
],
},
"org_typelevel_paiges_core": {
"artifact": "org.typelevel:paiges-core_2.13:0.4.1",
"sha256": "3c4968ee11aa929d937fc666db62cf7bbc3909ba08c853909d93fea08b214569",
"artifact": "org.typelevel:paiges-core_2.13:0.4.4",
"sha256": "ffbd59d3648e71c5b8f4474a54121fb3512707e7901245831669aa9e85f3bbf0",
"deps": [
"@io_bazel_rules_scala_scala_library",
],
},
"com_typesafe_config": {
"artifact": "com.typesafe:config:1.4.1",
"sha256": "4c0aa7e223c75c8840c41fc183d4cd3118140a1ee503e3e08ce66ed2794c948f",
"artifact": "com.typesafe:config:1.4.3",
"sha256": "8ada4c185ce72416712d63e0b5afdc5f009c0cdf405e5f26efecdf156aa5dfb6",
},
"org_scala_lang_scalap": {
"artifact": "org.scala-lang:scalap:2.13.6",
Expand All @@ -189,8 +211,8 @@ artifacts = {
],
},
"com_lihaoyi_fansi": {
"artifact": "com.lihaoyi:fansi_2.13:0.2.12",
"sha256": "d92e5b0ea4d946f6567db57fbeffc34d4020597c675cb804ade6cc38a198ff35",
"artifact": "com.lihaoyi:fansi_2.13:0.4.0",
"sha256": "0eb11a2a905d608033ec1642b0a9f0d8444daa4ad465f684b50bdc7e7a41bf53",
"deps": [
"@com_lihaoyi_sourcecode",
],
Expand All @@ -211,31 +233,42 @@ artifacts = {
],
},
"com_lihaoyi_sourcecode": {
"artifact": "com.lihaoyi:sourcecode_2.13:0.2.5",
"sha256": "b8f816ad81e3546e4a75cdc74d322f02e6d84fbfa5eb1ea5e1eca666cede524c",
"artifact": "com.lihaoyi:sourcecode_2.13:0.4.2",
"sha256": "fbace2b994a7184f6b38ee98630be61f21948008a4a56cd83c7f86c1c1de743d",
},
"com_google_protobuf_protobuf_java": {
"artifact": "com.google.protobuf:protobuf-java:3.10.0",
"sha256": "161d7d61a8cb3970891c299578702fd079646e032329d6c2cabf998d191437c9",
},
"com_geirsson_metaconfig_core": {
"artifact": "com.geirsson:metaconfig-core_2.13:0.9.14",
"sha256": "aab728395055a095d1134f76191d40076eaf9d5c9ffc722005da044580269acf",
"artifact": "com.geirsson:metaconfig-core_2.13:0.12.0",
"sha256": "2c91199ae5b206afdd52538f8c4da67c1794bcce0b5b06cf25679db00cf32c19",
"deps": [
"@com_lihaoyi_pprint",
"@io_bazel_rules_scala_scala_library",
"@org_typelevel_paiges_core",
"@io_bazel_rules_scala_scala_reflect",
"@com_geirsson_metaconfig_pprint",
"@org_scala_lang_modules_scala_collection_compat",
"@org_typelevel_paiges_core",
],
},
"com_geirsson_metaconfig_pprint": {
"artifact": "com.geirsson:metaconfig-pprint_2.13:0.12.0",
"sha256": "6d8b0b4279116c11d4f29443bd2a9411bedb3d86ccaf964599a9420f530ed062",
"deps": [
"@io_bazel_rules_scala_scala_compiler",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@com_lihaoyi_fansi",
],
},
"com_geirsson_metaconfig_typesafe_config": {
"artifact": "com.geirsson:metaconfig-typesafe-config_2.13:0.9.14",
"sha256": "e4ca5cfb44dc3ee0fff222eafbe86c7f431b0f692b7fd0b832da521462a693cd",
"artifact": "com.geirsson:metaconfig-typesafe-config_2.13:0.12.0",
"sha256": "b4c5dbf863dadde363d8bd24333ce1091fec94fc5b88efd04607a26f3eab61b8",
"deps": [
"@io_bazel_rules_scala_scala_library",
"@com_geirsson_metaconfig_core",
"@com_typesafe_config",
"@io_bazel_rules_scala_scala_library",
],
]
},
"io_bazel_rules_scala_org_openjdk_jmh_jmh_core": {
"artifact": "org.openjdk.jmh:jmh-core:1.36",
Expand Down

0 comments on commit b05ea3a

Please sign in to comment.