Skip to content

Commit

Permalink
WIP: Bzlmod part the ninety-second
Browse files Browse the repository at this point in the history
Got all Scala 2 versions to run the `//test/scalafmt/...` tests, even
though they all fail. Scala 3 won't work to begin with because the test
files are all using Scala 2 syntax.

Updated `scalafmt_repositories.bzl` to import all the libraries needed
by Scalafmt under Scala 2.11. This made the file neater, actually, since
there more libraries were in common than previously.

Added the conditionally compiled `ScalafmtWorker-2_11.scala` file to fix
the following compiler error:

```txt
$ bazel test --repo_env=SCALA_VERSION=2.11.12 //test/scalafmt/...

ERROR: .../scala/scalafmt/BUILD:22:13:
  scala @@//scala/scalafmt:scalafmt [for tool] failed: (Exit 1):
  scalac failed: error executing Scalac command
  (from target //scala/scalafmt:scalafmt)
  bazel-bin/src/java/io/bazel/rulesscala/scalac/scalac
  @bazel-bin/scala/scalafmt/scalafmt.jar-0.params

scala/scalafmt/scalafmt/ScalafmtWorker.scala:8:
  error: object sysops is not a member of package org.scalafmt
import org.scalafmt.sysops.FileOps
                    ^
scala/scalafmt/scalafmt/ScalafmtWorker.scala:21:
  error: not found: value FileOps
    val source = FileOps.readFile(
                 ^
scala/scalafmt/scalafmt/ScalafmtWorker.scala:25:
  error: value fromHoconFile is not a member of
  object org.scalafmt.config.ScalafmtConfig
    val config = ScalafmtConfig.fromHoconFile(
                                ^
Build failure with errors.
```

Then downgraded to `com.lihaoyi:pprint_2.11:0.5.4` for Scala 2.11 to fix
this error:

```txt
$ bazel test --repo_env=SCALA_VERSION=2.11.12 //test/scalafmt/...

INFO: Analyzed 9 targets (43 packages loaded, 2484 targets configured).
ERROR: .../test/scalafmt/BUILD:63:23: ScalaFmt
  test/scalafmt/test/scalafmt/unformatted/unformatted-custom-conf.scala.fmt.output
  failed: Worker process did not return a WorkResponse:

---8<---8<--- Start of log, file at .../bazel-workers/worker-99-ScalaFmt.log ---8<---8<---
Exception in thread "main" java.lang.NoSuchMethodError:
  'pprint.TPrint pprint.TPrint$.lambda(scala.Function1)'
    at org.scalafmt.config.Docstrings$.<init>(Docstrings.scala:77)
    at org.scalafmt.config.Docstrings$.<clinit>(Docstrings.scala)
    at org.scalafmt.config.ScalafmtConfig$.apply$default$3(ScalafmtConfig.scala:104)
    at org.scalafmt.config.ScalafmtConfig$.<init>(ScalafmtConfig.scala:236)
    at org.scalafmt.config.ScalafmtConfig$.<clinit>(ScalafmtConfig.scala)
    at org.scalafmt.config.Config$.fromHoconFile$default$3(Config.scala:42)
    at io.bazel.rules_scala.scalafmt.ScalafmtWorker$.work(ScalafmtWorker-2_11.scala:25)
    at io.bazel.rulesscala.worker.Worker.persistentWorkerMain(Worker.java:96)
    at io.bazel.rulesscala.worker.Worker.workerMain(Worker.java:49)
    at io.bazel.rules_scala.scalafmt.ScalafmtWorker$.main(ScalafmtWorker-2_11.scala:14)
    at io.bazel.rules_scala.scalafmt.ScalafmtWorker.main(ScalafmtWorker-2_11.scala)
---8<---8<--- End of log ---8<---8<---
```

Now all Scala 2 versions are failing with the following:

```txt
$ bazel test --repo_env=SCALA_VERSION=2.11.12 //test/scalafmt/...

INFO: Analyzed 9 targets (80 packages loaded, 3307 targets configured).
ERROR: .../test/scalafmt/BUILD:43:20: ScalaFmt
  test/scalafmt/test/scalafmt/formatted/formatted-test.scala.fmt.output
  failed: Worker process did not return a WorkResponse:

---8<---8<--- Start of log, file at .../bazel-workers/worker-134-ScalaFmt.log ---8<---8<---
Exception in thread "main" java.lang.NoSuchMethodError:
  'boolean com.google.protobuf.GeneratedMessageV3.isStringEmpty(java.lang.Object)'
    at com.google.devtools.build.lib.worker.WorkerProtocol$WorkResponse.getSerializedSize(WorkerProtocol.java:2704)
    at com.google.protobuf.AbstractMessageLite.writeDelimitedTo(AbstractMessageLite.java:89)
    at io.bazel.rulesscala.worker.Worker.persistentWorkerMain(Worker.java:109)
    at io.bazel.rulesscala.worker.Worker.workerMain(Worker.java:49)
    at io.bazel.rules_scala.scalafmt.ScalafmtWorker$.main(ScalafmtWorker-2_11.scala:14)
    at io.bazel.rules_scala.scalafmt.ScalafmtWorker.main(ScalafmtWorker-2_11.scala)
---8<---8<--- End of log ---8<---8<---
```

This issue seems to suggest a library bump may fix it; will look into
it:

- protocolbuffers/protobuf#9236
  • Loading branch information
mbland committed Oct 13, 2024
1 parent 97a373a commit c3501da
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
13 changes: 7 additions & 6 deletions scala/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
load("//scala/scalafmt/toolchain:toolchain.bzl", "export_scalafmt_deps")
load("//scala/scalafmt:phase_scalafmt_ext.bzl", "scalafmt_singleton")
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:scala_cross_version_select.bzl", "select_for_scala_version")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
load(
"//scala/scalafmt:phase_scalafmt_ext.bzl",
"scalafmt_singleton",
)

filegroup(
name = "runner",
Expand All @@ -21,7 +19,10 @@ filegroup(

scala_binary(
name = "scalafmt",
srcs = ["scalafmt/ScalafmtWorker.scala"],
srcs = select_for_scala_version(
before_2_12_0 = ["scalafmt/ScalafmtWorker-2_11.scala"],
since_2_12_0 = ["scalafmt/ScalafmtWorker.scala"],
),
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtWorker",
visibility = ["//visibility:public"],
deps = [
Expand Down
47 changes: 47 additions & 0 deletions scala/scalafmt/scalafmt/ScalafmtWorker-2_11.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.bazel.rules_scala.scalafmt

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 scala.annotation.tailrec
import scala.io.Codec

object ScalafmtWorker extends Worker.Interface {

def main(args: Array[String]): Unit = Worker.workerMain(args, ScalafmtWorker)

def work(args: Array[String]): Unit = {
val argName = List("config", "input", "output")
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 config = Config.fromHoconFile(
namespace.getOrElse("config", new File(""))
).get

@tailrec
def format(code: String): String = {
val formatted = Scalafmt.format(code, config).get
if (code == formatted) code else format(formatted)
}

val output = try {
format(source)
} catch {
case e @ (_: org.scalafmt.Error | _: scala.meta.parsers.ParseException) => {
System.out.println("Unable to format file due to bug in scalafmt")
System.out.println(e.toString)
source
}
}

Files.write(namespace.getOrElse("output", new File("")).toPath, output.getBytes)
}
}
41 changes: 19 additions & 22 deletions scala/scalafmt/scalafmt_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,54 @@ def scalafmt_default_config(path = ".scalafmt.conf"):
path = "",
)

_SCALAFMT_BASE_DEPS = [
_SCALAFMT_DEPS = [
"com_geirsson_metaconfig_core",
"com_geirsson_metaconfig_typesafe_config",
"com_google_protobuf_protobuf_java",
"com_lihaoyi_fansi",
"com_lihaoyi_fastparse",
"com_lihaoyi_sourcecode",
"com_thesamet_scalapb_lenses",
"com_thesamet_scalapb_scalapb_runtime",
"com_typesafe_config",
"org_scala_lang_modules_scala_collection_compat",
"org_scala_lang_scalap",
"org_scalameta_common",
"org_scalameta_parsers",
"org_scalameta_scalafmt_core",
"org_scalameta_scalameta",
"org_scalameta_trees",
"org_typelevel_paiges_core",
]

_SCALAFMT_CORE_DEPS_2_11 = [
"com_geirsson_metaconfig_core",
"com_geirsson_metaconfig_typesafe_config",
"org_scalameta_scalameta",
_SCALAFMT_DEPS_2_11 = [
"com_lihaoyi_pprint",
"com_lihaoyi_fansi",
"com_lihaoyi_sourcecode",
"org_typelevel_paiges_core",
"org_scalameta_fastparse",
"org_scalameta_fastparse_utils",
]

_SCALAFMT_CORE_DEPS = [
"com_geirsson_metaconfig_core",
_SCALAFMT_DEPS_2_12 = [
"com_geirsson_metaconfig_pprint",
"com_geirsson_metaconfig_typesafe_config",
"com_lihaoyi_fansi",
"com_lihaoyi_sourcecode",
"com_typesafe_config",
"org_scalameta_common",
"com_lihaoyi_geny",
"org_scalameta_mdoc_parser",
"org_scalameta_parsers",
"org_scalameta_scalafmt_config",
"org_scalameta_scalafmt_sysops",
"org_scalameta_scalameta",
"org_scalameta_trees",
"org_typelevel_paiges_core",
]

def scalafmt_artifact_ids(scala_version):
major_version = extract_major_version(scala_version)

if major_version == "2.11":
return _SCALAFMT_BASE_DEPS + _SCALAFMT_CORE_DEPS_2_11
return _SCALAFMT_DEPS + _SCALAFMT_DEPS_2_11

extra_deps = ["com_lihaoyi_geny"]
extra_deps = []

if major_version == "2.12":
extra_deps.append("com_github_bigwheel_util_backports")
else:
extra_deps.append("io_bazel_rules_scala_scala_parallel_collections")

return _SCALAFMT_BASE_DEPS + _SCALAFMT_CORE_DEPS + extra_deps
return _SCALAFMT_DEPS + _SCALAFMT_DEPS_2_12 + extra_deps

def scalafmt_repositories(
maven_servers = _default_maven_server_urls(),
Expand Down
4 changes: 2 additions & 2 deletions third_party/repositories/scala_2_11.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ artifacts = {
],
},
"com_lihaoyi_pprint": {
"artifact": "com.lihaoyi:pprint_2.11:0.8.1",
"sha256": "3755b8e5d1423931219ab607e8fd9b6db472740dd5f815f8b0715312e11d06fe",
"artifact": "com.lihaoyi:pprint_2.11:0.5.4",
"sha256": "9b31150ee7f07212a825e02fca56e0999789d2b8ec720a84b75ce29ca7e195b5",
"deps": [
"@com_lihaoyi_fansi",
"@com_lihaoyi_sourcecode",
Expand Down

0 comments on commit c3501da

Please sign in to comment.