Hi, I’m curious about some strange behavior when c...
# general
r
Hi, I’m curious about some strange behavior when compiling Scala I’ve noticed while updating from 1.30.4 to 2.15. According to https://docs.scala-lang.org/style/files.html it seems it’s common to put sealed traits and their case classes in the same file, which compiles fine with 1.30.4, but in 2.15 it complains that there’s a dependency cycle. I can workaround this by explicitly excluding the target as a dependency from itself, but that seems a bit awkward. Is this intended behavior?
ex.
Copy code
scala_source(
    name="test-trait-target",
    source="TestTrait.scala",
    dependencies=[
        "!:test-trait-target",
    ]
)
w
what does the error look like? cycles should be fine in general
and sorry, i need to log off: but if you post the error and a bit more of the reproduction here or in a ticket, i’ll look at it tomorrow morning.
👍 1
r
Thank you, no hurry. The error is as follows:
Copy code
[ERROR] 1 Exception encountered:

Engine traceback:
  in `check` goal
  in Check compilation for Scala - scalac
  in Resolve coarsened targets

CycleException: The dependency graph contained a cycle:
-> src/scala/com:test-trait-target
-> src/scala/com:test-trait-target

To fix this, first verify if your code has an actual import cycle. If it does, you likely need to re-architect your code to avoid the cycle.

If there is no cycle in your code, then you may need to use more granular targets. Split up the problematic targets into smaller targets with more granular `sources` fields so that you can adjust the `dependencies` fields to avoid introducing a cycle.

Alternatively, use Python dependency inference (`--python-infer-imports`), rather than explicit `dependencies`. Pants will infer dependencies on specific files, rather than entire targets. This extra precision means that you will only have cycles if your code actually does have cycles in it.
As for the advice in the message, I wouldn't expect this to constitute an import cycle. I could probably split it up into 2 or 3 files if there were also a companion object, but as it is there's only 1 file so I don't think it's possible to make any more granular targets. And when I try to use the recommended flag I get an error for an unknown flag. src/scala/com/TestTrait.scala
Copy code
package com

sealed trait TestTrait
case object TestTraitA extends TestTrait
case object TestTraitB extends TestTrait
case object TestTraitC extends TestTrait
src/scala/com/BUILD
Copy code
scala_source(
    name="test-trait-target",
    source="TestTrait.scala",
)