scalafix icon indicating copy to clipboard operation
scalafix copied to clipboard

`OrganizeImports` rule can break compilation

Open satorg opened this issue 8 months ago • 2 comments

Actual Behavior

A real example. Consider a project with the following two dependencies:

Also consider a scala file with the following imports section:

import io.circe._
import fs2._

After applying the OrganizeImports rule the section will be simply re-arranged:

import fs2._
import io.circe._

Which breaks the compilation, because there's io sub-package inside fs2 and therefore the compiler will be assuming that we're trying to import fs2.io.circe._ at the second line.

Expected Behavior

I can think of two strategies of how it could be mitigated:

  1. Do not push import io.whatever below import fs2._ if the latter already contains io. E.g., the following imports sections:
    import gg.hh.ii._
    import io.circe._
    import aa.bb.cc._
    import fs2._
    
    could be re-arranged this way in such a case:
    import aa.bb.cc._
    import io.circe._
    import fs2._
    import gg.hh.ii._
    
  2. Prefix conflicting imports with _root_.

I think, these two approaches can be made configurable.

satorg avatar Jun 06 '24 20:06 satorg