bug icon indicating copy to clipboard operation
bug copied to clipboard

Usage of Java annotation-with-class-parameter on Scala 3 class causes exception when compiling Scala 2 class

Open jveldhuizen opened this issue 1 month ago • 1 comments

Usage of Java annotation-with-class-parameter on Scala 3 class causes exception when compiling Scala 2 class. This occurs since Scala 2.13.17.

Reproduction steps

Scala version: 2.13.17 and 2.13.18

build.sbt:

ThisBuild / scalaVersion := "3.7.4"

lazy val a = (project in file("a"))
  .settings(
    libraryDependencies += "org.osgi" % "osgi.cmpn" % "7.0.0",
  )

lazy val b = (project in file("b"))
  .settings(
    scalaVersion := "2.13.17",
    scalacOptions ++= Seq(
      "-Ytasty-reader",
      "-Wunused:patvars",
      "-Wunused:implicits",
      "-Wunused:locals",
      "-Wunused:privates",
    ),
  )
  .dependsOn(a)

lazy val root = (project in file("."))
  .aggregate(a, b)

a/src/main/scala/A.scala:

import org.osgi.service.component.annotations.Component

@Component(service = Array(classOf[A]))
class A

b/src/main/scala/B.scala:

class B {
  val a = new A
}

Problem

sbt compile shows

[info] compiling 1 Scala source to a/target/scala-3.7.4/classes ...
[info] compiling 1 Scala source to b/target/scala-2.13/classes ...
[error] ## Exception when compiling 1 sources to b/target/scala-2.13/classes
[error] scala.reflect.internal.Types$TypeError: could not find package <special-ops> whilst reading annotation of class A; perhaps it is missing from the classpath.
[error]
[error]
[error] stack trace is suppressed; run last b / Compile / compileIncremental for the full output
[error] (b / Compile / compileIncremental) scala.reflect.internal.Types$TypeError: could not find package <special-ops> whilst reading annotation of class A; perhaps it is missing from the classpath.

To fix this compile error, you have to

  • compile project 'b' with Scala 2.13.16 or
  • remove all four -Wunused settings or
  • remove the service argument of the @Component annotation on class A.

jveldhuizen avatar Dec 08 '25 07:12 jveldhuizen

What is package <special-ops>?

Scala 3 warns

2 |package `p-q`
  |        ^^^^^
  |The package name `p-q` will be encoded on the classpath, and can lead to undefined behaviour.

som-snytt avatar Dec 08 '25 10:12 som-snytt