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 3.9.0 doesn't format code with minimal configuration #4827

Closed
Gedochao opened this issue Feb 19, 2025 · 14 comments
Closed

scalafmt 3.9.0 doesn't format code with minimal configuration #4827

Gedochao opened this issue Feb 19, 2025 · 14 comments

Comments

@Gedochao
Copy link

Required: Configuration

version = "3.9.0"
runner.dialect = scala213
version = "3.9.0"
runner.dialect = scala212
version = "3.9.0"
runner.dialect = scala3

Required: Command-line parameters

none

Steps

Given code like this:

package foo

object Foo       extends       java.lang.Object  {
  def           get()             = 2
}

Problem

Scalafmt 3.9.0 formats code like this (as in, seemingly not at all):

package foo

object Foo       extends       java.lang.Object  {
  def           get()             = 2
}

Expectation

I would like the formatted output to look like this (3.8.6 and before did so):

package foo

object Foo extends java.lang.Object {
  def get() = 2
}

Notes

Even when setting the version to 3.8.6 in the configuration, using the newest scalafmt-cli launcher (3.9.0) seems to not format the code.

version = "3.8.6"
runner.dialect = scala3

Discovered while bumping scalafmt in scala-cli:

@kitbellew
Copy link
Collaborator

Discovered while bumping scalafmt in scala-cli:

what exactly in that pr suggests the formatter behaves that way? all i see is it removes trailing whitespace, something that @tgodzik already reported in #4825.

@Gedochao
Copy link
Author

Discovered while bumping scalafmt in scala-cli:

what exactly in that pr suggests the formatter behaves that way? all i see is it removes trailing whitespace, something that @tgodzik already reported in #4825.

integration.test.jvm 9 tests failed: 
  scala.cli.integration.FmtTests.simple scala.cli.integration.FmtTests.simple
  scala.cli.integration.FmtTests.no inputs scala.cli.integration.FmtTests.no inputs
  scala.cli.integration.FmtTests.with --check scala.cli.integration.FmtTests.with --check
  scala.cli.integration.FmtTests.filter correctly with --check scala.cli.integration.FmtTests.filter correctly with --check
  scala.cli.integration.FmtTests.--save-scalafmt-conf scala.cli.integration.FmtTests.--save-scalafmt-conf
  scala.cli.integration.FmtTests.--scalafmt-dialect scala.cli.integration.FmtTests.--scalafmt-dialect
  scala.cli.integration.FmtTests.creating workspace conf file scala.cli.integration.FmtTests.creating workspace conf file
  scala.cli.integration.FmtTests.scalafmt conf without version scala.cli.integration.FmtTests.scalafmt conf without version
  scala.cli.integration.FixTestsDefault.semantic rule scala.cli.integration.FixTestsDefault.semantic rule

the test cases are here:
https://github.com/VirtusLab/scala-cli/blob/bf3753e433f8020c24dd6e66dfb0c0dc3dddbb81/modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala#L7

not sure if it's the same thing as #4825

@tgodzik
Copy link
Contributor

tgodzik commented Feb 19, 2025

Looks like the 3.9.0 runner doesn't do anything currently. Running with 3.8.6 runner and new version set does work.

@Gedochao
Copy link
Author

Gedochao commented Feb 19, 2025

That would explain it, since Scala CLI downloads the runner associated with the version.

@kitbellew
Copy link
Collaborator

@Gedochao the issue you are reporting is with scala-cli, not scalafmt, correct? and what you are observing is that the way scala-cli uses scalafmt doesn't work, would that be accurate?

3.9.0 CLI is not perfect (reads a lot of files first before starting to format, and does not exit if there are exceptions) but it does format. at least in my testing.

so, perhaps, the way to use it in scala-cli should change, now that scalafmt is asynchronous?

@Gedochao
Copy link
Author

Gedochao commented Feb 19, 2025

@Gedochao the issue you are reporting is with scala-cli, not scalafmt, correct? and what you are observing is that the way scala-cli uses scalafmt doesn't work, would that be accurate?

No. I can replicate the issue with the scalafmt launcher itself.

@kitbellew note that if you use an older scalafmt launcher (3.8.6) with .scalafmt.conf pointing at 3.9.0, it will actually work fine. If you use the newest launcher, however, the issue I describe occurs.

What Scala CLI does is it downloads the launcher matching the scalafmt version in the configuration and runs that.
The issue lies with the scalafmt launcher.

@kitbellew
Copy link
Collaborator

No. I can replicate the issue with the scalafmt launcher itself.

How, specifically? Without scala-cli, preferably.

@kitbellew note that if you use an older scalafmt launcher (3.8.6) with .scalafmt.conf pointing at 3.9.0, it will actually work fine. If you use the newest launcher, however, the issue I describe occurs.

Clearly, I used the actual 3.9.0, coursier-installed and also locally built, via sbt cli/assembly, they both work for me. With and without the matching version in configuration.

@kitbellew
Copy link
Collaborator

What Scala CLI does is it downloads the launcher matching the scalafmt version in the configuration and runs that. The issue lies with the scalafmt launcher.

what do you mean by "launcher"? do you mean the scalafmt-cli package or something else?

and what do you mean by "runs that"? do you run it by creating a new process and running the executable above? or are you loading the jar and invoking the main method directly?

@tgodzik
Copy link
Contributor

tgodzik commented Feb 19, 2025

For me what I did was:
cs install scalafmt:3.9.0 and then scalafmt . which didn't fix the file.

After doing cs install scalafmt:3.8.6 it started working again.

@kitbellew
Copy link
Collaborator

For me what I did was: cs install scalafmt:3.9.0 and then scalafmt . which didn't fix the file.

After doing cs install scalafmt:3.8.6 it started working again.

@tgodzik I did the same :( in this repository, specifically, I added some spaces here and there and ran the coursier-installed scalafmt. And it worked.

Which repo did you use? What's the config file there? Are there any files which cannot be formatted (bad scala or search state exploding etc.)?

@tgodzik
Copy link
Contributor

tgodzik commented Feb 19, 2025

Huh, the file was very simple:

//> using scala "3.6.3"
//> using dep "org.scala-lang:scala3-compiler_3:3.6.3"

package foo

object Foo       extends       java.lang.Object  {
  def           get()             = 2
}

I can try and dig in tomorrow to see what is going on.

@Gedochao
Copy link
Author

No. I can replicate the issue with the scalafmt launcher itself.

How, specifically? Without scala-cli, preferably.

@kitbellew cs install scalafmt:3.9.0, same as @tgodzik.

@kitbellew
Copy link
Collaborator

please check https://github.com/scalameta/scalafmt/releases/tag/v3.9.1

@tgodzik
Copy link
Contributor

tgodzik commented Feb 24, 2025

Looks to be working both native image and Scala native. Thanks!

@tgodzik tgodzik closed this as completed Feb 24, 2025
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

No branches or pull requests

3 participants