Skip to content
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

scalafmt rewrites scala 2 syntax into scala 3 in build.sbt file #3787

Closed
mtomko opened this issue Feb 20, 2024 · 5 comments · Fixed by #3795
Closed

scalafmt rewrites scala 2 syntax into scala 3 in build.sbt file #3787

mtomko opened this issue Feb 20, 2024 · 5 comments · Fixed by #3795

Comments

@mtomko
Copy link
Contributor

mtomko commented Feb 20, 2024

Configuration (required)

See https://github.com/mtomko/scalafmt-repro2

Please paste the contents of your .scalafmt.conf file here:

version=3.8.0
runner.dialect = scala3
rewrite.scala3.removeOptionalBraces = true

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt

Steps

Given code like this:

ThisBuild / scalaVersion := "3.3.1"

lazy val versions = new {
  val cats = "2.10.0"
}

libraryDependencies += "org.typelevel" %% "cats-core" % versions.cats

Problem

Scalafmt formats code like this:

ThisBuild / scalaVersion := "3.3.1"

lazy val versions = new:
  val cats = "2.10.0"

libraryDependencies += "org.typelevel" %% "cats-core" % versions.cats

Expectation

I would like the formatted output to look like this:

ThisBuild / scalaVersion := "3.3.1"

lazy val versions = new {
  val cats = "2.10.0"
}

libraryDependencies += "org.typelevel" %% "cats-core" % versions.cats

Notes

Briefly discussed on discord: https://discord.com/channels/632642981228314653/632665341864181780/1209538770303721533

mtomko added a commit to broadinstitute/poolq that referenced this issue Feb 22, 2024
mtomko added a commit to broadinstitute/poolq that referenced this issue Feb 22, 2024
* Port to Scala 3

* Refactor reference package object

* Convert package objects to toplevel

* Set Scala 3 rewrite rules

* Run scalafmt

* Update copyright

* Set version to 3.12.0-SNAPSHOT

* Update changelog

* Update GH actions

* Workaround for scalafmt issue

See scalameta/scalafmt#3787
@mzuehlke
Copy link

This fix doesn't work for files in the project folder.
They still get converted to braceless style which isn't working for sbt

@kitbellew
Copy link
Collaborator

This fix doesn't work for files in the project folder. They still get converted to braceless style which isn't working for sbt

can you please be more specific? which files?

@mzuehlke
Copy link

This is a minimal repo: https://github.com/mzuehlke/scalafmt-sbt-project-bug

Invoking sbt scalafmtSbt leads to the following diff:

diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 749733c..911726f 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -1,5 +1,4 @@
 import sbt.*

-object Dependencies {
+object Dependencies:
   val cats = "org.typelevel" %% "cats-core" % "2.10.0"
-}

@kitbellew
Copy link
Collaborator

@mzuehlke i think you have unrealistic expectations. this is not an sbt file, it's a scala file. if you specify in your configuration that your dialect is scala3, at least for scala files that's what it will be.

@mzuehlke
Copy link

Thanks for the clarification. If this is the expected behaviour that's fine. 👍

Maybe an addition to the config would be helpful so that everyone who adds scala3 specific options know that a fileOverride for the "project" folder is needed.

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 a pull request may close this issue.

3 participants