How do I stop scalafmt from messing with my imports?

I’d like to stop scalafmt from rearranging/deleting/replacing my imports. I haven’t used the rewrite rules SortImports or AsciiSortImports (and I don’t think any of my other settings should mess with imports either).

One problem I have is that scalafmt rearranges imports in an order I don’t like (I have them separated into three groups with blank lines in between, and scalafmt turns them into two).

A more annoying issue is that Scala 3 wildcards are deleted. An import like import com.foo.* becomes import com.foo, and an import like import Foo.* just gets deleted. This means that every time I format the file, I have to go back and fix all the imports.

I understand that Scala 3 is new and support for it won’t be perfect yet, but this is really hampering my productivity. Is there any way to disable import-related fixes?

Here is my .scalafmt.conf file, if it matters:

version = 3.0.0-RC2
runner.dialect = scala3
maxColumn = 100
align.preset = some
continuationIndent.callSite = 4
continuationIndent.defnSite = 4
continuationIndent.extendSite = 4
rewrite.rules = [RedundantParens]
optIn.breakChainOnFirstMethodDot = false
newlines.afterCurlyLambda = never
newlines.alwaysBeforeElseAfterCurlyIf = false
newlines.alwaysBeforeMultilineDef = false
verticalMultiline.atDefnSite = false
verticalMultiline.newlineAfterOpenParen = false
rewrite.scala3.convertToNewSyntax = true

Maybe this info helps somehow: It’s probably not a general problem with scalafmt x Scala 3 (i.e. may indeed be specific to your config) - I don’t see these issues, and I also use the new import syntax.

That’s interesting. If it’s not too much trouble, could you share your config so I can tell what I need to do?

Here it is - unfortunately, there is not much going on here:

version = "3.0.0-RC2"
runner.dialect = scala3

align.preset = more
maxColumn = 100
trailingCommas = keep

Thanks! Unfortunately, it still removes .*, even with rewrite.scala3.convertToNewSyntax = true. I guess the only thing I can do right now is use _ instead.

You might want to check here if there is already an issue for this Issues · scalameta/scalafmt · GitHub and if not perhaps open an issue where you try to provide a minimized code the demonstrates the bug.

2 Likes