Skip to content

Replace @scalafmt_default with toolchains target #1725

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 1 commit into from
Apr 29, 2025
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
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files([".scalafmt.conf"])
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ use_repo(
dev_deps,
"scala_proto_rules_scalapb_compilerplugin",
"scala_proto_rules_scalapb_protoc_bridge",
"scalafmt_default",
)

# Default versions of version specific repos needed by some of our tests. Tests
Expand Down
25 changes: 22 additions & 3 deletions docs/phase_scalafmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ A phase extension `phase_scalafmt` can format Scala source code via [Scalafmt](h

## How to set up

Add this snippet to `WORKSPACE`
Add this snippet to `MODULE.bazel`:

```py
# MODULE.bazel
scala_deps = use_extension(
"@rules_scala//scala/extensions:deps.bzl",
"scala_deps",
)
scala_deps.scalafmt()
```

or to `WORKSPACE`:

```py
# WORKSPACE
load(
"@rules_scala//scala:toolchains.bzl",
"scala_register_toolchains",
Expand Down Expand Up @@ -59,16 +71,23 @@ bazel run <TARGET>.format-test

to check the format (without modifying source code).

The extension provides default configuration, but there are 2 ways to use custom configuration
The extension provides a default configuration, but there are 2 ways to use
a custom configuration:

- Put `.scalafmt.conf` at the root of your workspace
- Pass `.scalafmt.conf` in via `scala_toolchains`:

```py
# MODULE.bazel
scala_deps.scalafmt(
default_config = "//path/to/my/custom:scalafmt.conf",
)

# WORKSPACE
scala_toolchains(
# Other toolchains settings...
scalafmt = True,
scalafmt_default_config_path = "//path/to/my/custom:scalafmt.conf",
scalafmt_default_config = "//path/to/my/custom:scalafmt.conf",
)
```

Expand Down
11 changes: 4 additions & 7 deletions scala/extensions/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,13 @@ _compiler_srcjar_attrs = {
}

_scalafmt_defaults = {
"default_config_path": ".scalafmt.conf",
"default_config": "//:.scalafmt.conf",
}

_scalafmt_attrs = {
"default_config_path": attr.string(
default = _scalafmt_defaults["default_config_path"],
doc = (
"The relative path to the default Scalafmt config file " +
"within the repository"
),
"default_config": attr.label(
default = _scalafmt_defaults["default_config"],
doc = "The default config file for Scalafmt targets",
),
}

Expand Down
2 changes: 1 addition & 1 deletion scala/scalafmt/phase_scalafmt_ext.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ext_scalafmt = {
"attrs": {
"config": attr.label(
allow_single_file = [".conf"],
default = "@scalafmt_default//:config",
default = "@rules_scala_toolchains//scalafmt:config",
doc = "The Scalafmt configuration file.",
),
"format": attr.bool(
Expand Down
19 changes: 0 additions & 19 deletions scala/scalafmt/scalafmt_repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
load("//scala:scala_cross_version.bzl", "extract_major_version")
load("//scala_proto/default:repositories.bzl", "SCALAPB_COMPILE_ARTIFACT_IDS")

def _scalafmt_config_impl(repository_ctx):
config_path = repository_ctx.attr.path
build = []
build.append("filegroup(")
build.append(" name = \"config\",")
build.append(" srcs = [\"{}\"],".format(config_path.name))
build.append(" visibility = [\"//visibility:public\"],")
build.append(")\n")

repository_ctx.file("BUILD", "\n".join(build), executable = False)
repository_ctx.symlink(repository_ctx.path(config_path), config_path.name)

scalafmt_config = repository_rule(
implementation = _scalafmt_config_impl,
attrs = {
"path": attr.label(mandatory = True, allow_single_file = True),
},
)

_SCALAFMT_DEPS = [
"com_lihaoyi_fansi",
"com_typesafe_config",
Expand Down
16 changes: 4 additions & 12 deletions scala/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ load(
"scala_version_artifact_ids",
"setup_scala_compiler_sources",
)
load(
"//scala/scalafmt:scalafmt_repositories.bzl",
"scalafmt_artifact_ids",
"scalafmt_config",
)
load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_artifact_ids")
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
load("//scala:toolchains_repo.bzl", "scala_toolchains_repo")
load("//scala_proto/default:repositories.bzl", "scala_proto_artifact_ids")
Expand Down Expand Up @@ -40,7 +36,7 @@ def scala_toolchains(
junit = False,
specs2 = False,
scalafmt = False,
scalafmt_default_config_path = ".scalafmt.conf",
scalafmt_default_config = Label("//:.scalafmt.conf"),
scala_proto = False,
scala_proto_options = [],
jmh = False,
Expand Down Expand Up @@ -87,8 +83,7 @@ def scala_toolchains(
junit: whether to instantiate the JUnit toolchain
specs2: whether to instantiate the Specs2 JUnit toolchain
scalafmt: whether to instantiate the Scalafmt toolchain
scalafmt_default_config_path: the relative path to the default Scalafmt
config file within the repository
scalafmt_default_config: the default config file for Scalafmt targets
scala_proto: whether to instantiate the scala_proto toolchain
scala_proto_options: protobuf options, like 'scala3_sources' or 'grpc';
`scala_proto` must also be `True` for this to take effect
Expand All @@ -112,10 +107,6 @@ def scala_toolchains(

setup_scala_compiler_sources(scala_compiler_srcjars)

if scalafmt:
scalafmt_conf_target = "//:" + scalafmt_default_config_path
scalafmt_config(name = "scalafmt_default", path = scalafmt_conf_target)

if specs2:
junit = True

Expand Down Expand Up @@ -187,6 +178,7 @@ def scala_toolchains(
junit = junit,
specs2 = specs2,
scalafmt = scalafmt,
scalafmt_default_config = scalafmt_default_config,
scala_proto = scala_proto,
scala_proto_options = scala_proto_options,
jmh = jmh,
Expand Down
11 changes: 11 additions & 0 deletions scala/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _scala_toolchains_repo_impl(repository_ctx):
format_args = {
"rules_scala_repo": Label("//:all").repo_name,
"proto_options": repo_attr.scala_proto_options,
"scalafmt_default_config": repo_attr.scalafmt_default_config,
}
toolchains = {}

Expand Down Expand Up @@ -94,6 +95,10 @@ _scala_toolchains_repo = repository_rule(
"junit": attr.bool(doc = "Instantiate the JUnit toolchain"),
"specs2": attr.bool(doc = "Instantiate the Specs2 toolchain"),
"scalafmt": attr.bool(doc = "Instantiate the Scalafmt toolchain"),
"scalafmt_default_config": attr.label(
doc = "Default Scalafmt config file",
allow_single_file = True,
),
"scala_proto": attr.bool(
doc = "Instantiate the scala_proto toolchain",
),
Expand Down Expand Up @@ -199,6 +204,12 @@ load(
"setup_scalafmt_toolchains",
)

alias(
name = "config",
actual = "{scalafmt_default_config}",
visibility = ["//visibility:public"],
)

setup_scalafmt_toolchains()
"""

Expand Down