Skip to content

Fix Scalafmt default_config docs for WORKSPACE #1734

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
May 7, 2025

Conversation

mbland
Copy link
Contributor

@mbland mbland commented May 7, 2025

Description

Makes it clear that WORKSPACE users must use a target Label string with the default_config option for the scalafmt argument of scala_toolchains. Notes that the specified config file must exist within a package, including but not limited to having a BUILD file in the repository root.

Motivation

@gergelyfabian noticed this while building gergelyfabian/bazel-scala-example after #1730 landed.

This isn't fixable in code. With this call:

scala_toolchains(
    # Other toolchains...
    scalafmt = {"default_config": ".scalafmt.conf"},
)
  • scala_toolchains looks for the file under external/.scalafmt.conf.

  • Updating scala_toolchains to wrap default_config in a Label makes it relative to @rules_scala//scala. This because scala_toolchains resides in @rules_scala//scala:toolchains.bzl.

  • native.package_relative_label is only available when called from a BUILD file.

Hence default_config values under WORKSPACE must be valid target labels.

The problem doesn't exist under Bzlmod; plain file paths will work fine, so long as a BUILD file exists at the repository root.

Makes it clear that `WORKSPACE` users must use a target `Label` string
with the `default_config` option for the `scalafmt` argument of
`scala_toolchains`. Notes that the specified config file must exist
within a package, including but not limited to having a `BUILD` file in
the repository root.

---

@gergelyfabian noticed this while building
gergelyfabian/bazel-scala-example after bazel-contrib#1730 landed.

This isn't fixable in code. With this call:

```py
scala_toolchains(
    # Other toolchains...
    scalafmt = {"default_config": ".scalafmt.conf"},
)
```

- `scala_toolchains` looks for the file under `external/.scalafmt.conf`.

- Updating `scala_toolchains` to wrap `default_config` in a `Label`
  makes it relative to `@rules_scala//scala`. This because
  `scala_toolchains` resides in `@rules_scala//scala:toolchains.bzl`.

- `native.package_relative_label` is only available when called from a
  `BUILD` file.

Hence `default_config` values under `WORKSPACE` must be valid target
labels.

The problem doesn't exist under Bzlmod; plain file paths will work fine,
so long as a `BUILD` file exists at the repository root.
@mbland mbland requested review from liucijus and simuons as code owners May 7, 2025 15:14
@gergelyfabian
Copy link
Contributor

Thank you!

Copy link
Collaborator

@simuons simuons left a comment

Choose a reason for hiding this comment

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

Thanks

@simuons simuons merged commit f2adbac into bazel-contrib:master May 7, 2025
1 check passed
@mbland mbland deleted the scalafmt-default-config-doc branch May 7, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants