bazelbuild / rules_scala

Scala rules for Bazel
Apache License 2.0
359 stars 266 forks source link

use newer version of scalafmt #1575

Open nyoungstudios opened 2 months ago

nyoungstudios commented 2 months ago

I setup scalafmt with my bazel project successfully following the instructions in this repo and some other examples on GitHub. Here is a minimal example .scalafmt.conf file I was using.

# .scalafmt.conf
# https://scalameta.org/scalafmt/docs/configuration.html
maxColumn = 100
version = 3.7.17
runner.dialect = scala212

# this doesn't work
rewrite.trailingCommas.style = always
# this works
# trailingCommas = always

However, I noticed that it does not download the same version as specified in the .scalafmt.conf file. I noticed this because the rewrite.trailingCommas.style setting is not available; instead, it was looking for the setting located at trailingCommas. Instead, it downloads version 3.0.0 per this line of code: https://github.com/bazelbuild/rules_scala/blob/98689d0bb1cbe2e684b95d44dd9962fa54aef5fa/third_party/repositories/scala_2_12.bzl#L123

I was able to override the version like this in my WORKSPACE file:

load("@io_bazel_rules_scala//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories")

scalafmt_default_config()

scalafmt_repositories(overriden_artifacts = {
    "org_scalameta_scalafmt_core": {
        "artifact": "org.scalameta:scalafmt-core_2.12:3.7.17",
        "sha256": "284bdc4da7969eca7910b0fc2c9c626a2acdc83c8f0be7709964dad4e3dbfeb9",
    },
})

However, after overriding this artifact, I still got this error.

external/io_bazel_rules_scala/scala/scalafmt/scalafmt/ScalafmtWorker.scala:8: error: object FileOps is not a member of package org.scalafmt.util
import org.scalafmt.util.FileOps
       ^
external/io_bazel_rules_scala/scala/scalafmt/scalafmt/ScalafmtWorker.scala:21: error: not found: value FileOps
    val source = FileOps.readFile(namespace.getOrElse("input", new File("")))(Codec.UTF8)
                 ^
external/io_bazel_rules_scala/scala/scalafmt/scalafmt/ScalafmtWorker.scala:23: error: value fromHoconFile is not a member of object org.scalafmt.config.Config
    val config = Config.fromHoconFile(namespace.getOrElse("config", new File(""))).get

Looking at that file in the scalafmt repo, you can see that they moved the FileOps object from the scalafmt-core to the scalafmt-sysops package; thus, it is published as a separate Maven package.

I couldn't figure out how to also include that package when running the formatter, but I imagine it is probably better to support it by adding it here.

I imagine the package would need to be added in a handful of spots including (I'm probably missing some spots):

Aside from those updates, I think it would be cool for this automatically download the correct version. But I imagine that would be more complicated.