Skip to content

Update to Scalafmt 3.8.3 #1631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
version = "3.8.3"
runner.dialect = scala213
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be worthy to add version = "3.8.3" so it's easier to check which version of scalamft we require.

Suggested change
runner.dialect = scala213
version = "3.8.3"
runner.dialect = scala213

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, here and in the other .scalafmt.conf files.

Heh, I had version = "3.8.3" in at some point in my bzlmod working branch, because Scalafmt failed without it. Then I took it out here because it didn't seem required. But I do like the explicitness of it.

align.openParenCallSite = false
align.openParenDefnSite = false
continuationIndent.defnSite = 2
danglingParentheses.preset = true
docstrings.style = Asterisk
importSelectors = singleLine
maxColumn = 120
verticalMultiline.newlineBeforeImplicitKW = true
rewrite.redundantBraces.stringInterpolation = true
rewrite.rules = [
RedundantParens,
PreferCurlyFors,
SortImports
]
unindentTopLevelOperators = false
lineEndings=preserve
indentOperator.topLevelOnly = true
lineEndings = preserve
Copy link
Contributor

@WojciechMazur WojciechMazur Oct 23, 2024

Choose a reason for hiding this comment

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

unindentTopLevelOperators can be probably replaced with indentOperator.topLevelOnly. AFAIR it worked good enough in one project I was migrating in the past from scalafmt 2.7.5 -> 3.8.1

Suggested change
lineEndings = preserve
indentOperator.topLevelOnly = true
lineEndings = preserve

In that case we were having a bit reverse direction: unindentTopLevelOperators = true into indentOperator.topLevelOnly = false

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks! Done

24 changes: 15 additions & 9 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"//scala:scala_cross_version.bzl",
"extract_major_version",
"extract_minor_version",
"version_suffix",
_default_maven_server_urls = "default_maven_server_urls",
)
load("//third_party/repositories:repositories.bzl", "repositories")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS")

def _dt_patched_compiler_impl(rctx):
Expand Down Expand Up @@ -69,15 +69,19 @@ def _validate_scalac_srcjar(srcjar):
def dt_patched_compiler_setup(scala_version, scala_compiler_srcjar = None):
scala_major_version = extract_major_version(scala_version)
scala_minor_version = extract_minor_version(scala_version)
patch = "@io_bazel_rules_scala//dt_patches:dt_compiler_%s.patch" % scala_major_version
patch = Label("//dt_patches:dt_compiler_%s.patch" % scala_major_version)

minor_version = int(scala_minor_version)

if scala_major_version == "2.12":
if minor_version >= 1 and minor_version <= 7:
patch = "@io_bazel_rules_scala//dt_patches:dt_compiler_%s.1.patch" % scala_major_version
patch = Label(
"//dt_patches:dt_compiler_%s.1.patch" % scala_major_version,
)
elif minor_version <= 11:
patch = "@io_bazel_rules_scala//dt_patches:dt_compiler_%s.8.patch" % scala_major_version
patch = Label(
"//dt_patches:dt_compiler_%s.8.patch" % scala_major_version,
)

build_file_content = "\n".join([
"package(default_visibility = [\"//visibility:public\"])",
Expand Down Expand Up @@ -182,14 +186,16 @@ def _artifact_ids(scala_version):
"io_bazel_rules_scala_scala_parser_combinators",
"org_scalameta_semanticdb_scalac",
] if scala_version.startswith("2") else [
"io_bazel_rules_scala_scala_library",
"io_bazel_rules_scala_scala_asm",
"io_bazel_rules_scala_scala_compiler",
"io_bazel_rules_scala_scala_compiler_2",
"io_bazel_rules_scala_scala_interfaces",
"io_bazel_rules_scala_scala_library",
"io_bazel_rules_scala_scala_library_2",
"io_bazel_rules_scala_scala_parser_combinators",
"io_bazel_rules_scala_scala_reflect_2",
"io_bazel_rules_scala_scala_tasty_core",
"io_bazel_rules_scala_scala_asm",
"io_bazel_rules_scala_scala_xml",
"io_bazel_rules_scala_scala_parser_combinators",
"io_bazel_rules_scala_scala_library_2",
"org_scala_sbt_compiler_interface",
]

Expand Down
15 changes: 8 additions & 7 deletions scala/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
load("//scala/scalafmt/toolchain:setup_scalafmt_toolchain.bzl", "setup_scalafmt_toolchains")
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/scalafmt/toolchain:setup_scalafmt_toolchain.bzl", "setup_scalafmt_toolchains")
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 @@ -22,7 +20,10 @@ filegroup(

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

import java.io.File
import org.scalafmt.config.Config
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.FileOps

object ScalafmtAdapter {
def readFile(file: File)(implicit codec: scala.io.Codec): String =
FileOps.readFile(file)(codec)

def parseConfigFile(configFile: File): ScalafmtConfig =
Config.fromHoconFile(configFile).get
}
13 changes: 13 additions & 0 deletions scala/scalafmt/scalafmt/ScalafmtAdapter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.bazel.rules_scala.scalafmt

import java.io.File
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.sysops.FileOps

object ScalafmtAdapter {
def readFile(file: File)(implicit codec: scala.io.Codec): String =
FileOps.readFile(file.toPath())(codec)

def parseConfigFile(configFile: File): ScalafmtConfig =
ScalafmtConfig.fromHoconFile(configFile.toPath()).get
}
12 changes: 7 additions & 5 deletions scala/scalafmt/scalafmt/ScalafmtWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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

Expand All @@ -18,12 +16,16 @@ 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 sourceFile = namespace.getOrElse("input", new File(""))
val source = ScalafmtAdapter.readFile(sourceFile)(Codec.UTF8)

val configFile = namespace.getOrElse("config", new File(""))
val config = ScalafmtAdapter.parseConfigFile(configFile)

val config = Config.fromHoconFile(namespace.getOrElse("config", new File(""))).get
@tailrec
def format(code: String): String = {
val formatted = Scalafmt.format(code, config).get
val filePath = sourceFile.getPath()
val formatted = Scalafmt.format(code, config, Set.empty, filePath).get
if (code == formatted) code else format(formatted)
}

Expand Down
63 changes: 42 additions & 21 deletions scala/scalafmt/scalafmt_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,67 @@ def scalafmt_default_config(path = ".scalafmt.conf", **kwargs):
scalafmt_config(name = "scalafmt_default", path = "//:" + path, **kwargs)

_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_fastparse",
"org_scalameta_fastparse_utils",
"org_scalameta_parsers",
"org_scalameta_scalafmt_core",
"org_scalameta_scalameta",
"org_scalameta_trees",
"org_typelevel_paiges_core",
"com_typesafe_config",
"org_scala_lang_scalap",
"com_thesamet_scalapb_lenses",
"com_thesamet_scalapb_scalapb_runtime",
"com_lihaoyi_fansi",
"com_lihaoyi_fastparse",
"org_scalameta_fastparse_utils",
"org_scala_lang_modules_scala_collection_compat",
]

_SCALAFMT_DEPS_2_11 = [
"com_lihaoyi_pprint",
"com_lihaoyi_sourcecode",
"com_google_protobuf_protobuf_java",
"com_geirsson_metaconfig_core",
"com_geirsson_metaconfig_typesafe_config",
"org_scalameta_fastparse",
"org_scalameta_fastparse_utils",
]

def _artifact_ids(scala_version):
_SCALAFMT_DEPS_2_12 = [
"com_geirsson_metaconfig_pprint",
"org_scalameta_mdoc_parser",
"org_scalameta_scalafmt_config",
"org_scalameta_scalafmt_sysops",
]

def scalafmt_artifact_ids(scala_version):
major_version = extract_major_version(scala_version)
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

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

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_DEPS + _SCALAFMT_DEPS_2_12 + extra_deps

def scalafmt_repositories(
maven_servers = _default_maven_server_urls(),
overriden_artifacts = {}):
overriden_artifacts = {},
bzlmod_enabled = False):
for scala_version in SCALA_VERSIONS:
repositories(
scala_version = scala_version,
for_artifact_ids = _artifact_ids(scala_version),
for_artifact_ids = scalafmt_artifact_ids(scala_version),
maven_servers = maven_servers,
overriden_artifacts = overriden_artifacts,
)
_register_scalafmt_toolchains()

if not bzlmod_enabled:
_register_scalafmt_toolchains()

def _register_scalafmt_toolchains():
for scala_version in SCALA_VERSIONS:
Expand Down
25 changes: 12 additions & 13 deletions scala/scalafmt/toolchain/setup_scalafmt_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
load("//scala/scalafmt/toolchain:toolchain.bzl", "scalafmt_toolchain")
load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_artifact_ids")
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 = [
"@com_geirsson_metaconfig_core",
"@org_scalameta_common",
"@org_scalameta_parsers",
"@org_scalameta_scalafmt_core",
"@org_scalameta_scalameta",
"@org_scalameta_trees",
"@org_scala_lang_modules_scala_collection_compat",
]

def setup_scalafmt_toolchain(
name,
scalafmt_classpath,
Expand All @@ -32,9 +23,14 @@ def setup_scalafmt_toolchain(
)
native.toolchain(
name = name,
target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)],
target_settings = [
"@io_bazel_rules_scala_config//:scala_version" +
version_suffix(scala_version),
],
toolchain = ":%s_impl" % name,
toolchain_type = "//scala/scalafmt/toolchain:scalafmt_toolchain_type",
toolchain_type = Label(
"//scala/scalafmt/toolchain:scalafmt_toolchain_type",
),
visibility = visibility,
)

Expand All @@ -47,4 +43,7 @@ def setup_scalafmt_toolchains():
)

def _deps(scala_version):
return [dep + version_suffix(scala_version) for dep in _SCALAFMT_DEPS]
return [
"@" + artifact_id + version_suffix(scala_version)
for artifact_id in scalafmt_artifact_ids(scala_version)
]
4 changes: 3 additions & 1 deletion test/scalafmt/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version = "3.8.3"
runner.dialect = scala213
maxColumn = 40
lineEndings=preserve
lineEndings = preserve
4 changes: 3 additions & 1 deletion test_cross_build/scalafmt/.scalafmt2.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version = "3.8.3"
runner.dialect = scala213
maxColumn = 40
lineEndings=preserve
lineEndings = preserve
5 changes: 3 additions & 2 deletions test_cross_build/scalafmt/.scalafmt3.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
runner.dialect=scala3
version = "3.8.3"
runner.dialect = scala3
maxColumn = 40
lineEndings=preserve
lineEndings = preserve
4 changes: 2 additions & 2 deletions test_cross_build/scalafmt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ scalafmt_scala_binary(
scala_version = "2.12.20",
)

scalafmt_scala_library(
scalafmt_scala_binary(
name = "formatted-binary2",
srcs = ["formatted/formatted-binary2.scala"],
config = ":scala2-conf",
Expand All @@ -74,7 +74,7 @@ scalafmt_scala_binary(
scala_version = "3.2.2",
)

scalafmt_scala_library(
scalafmt_scala_binary(
name = "formatted-binary3",
srcs = ["formatted/formatted-binary3.scala"],
config = ":scala3-conf",
Expand Down
Loading