scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.87k stars 1.06k forks source link

In mixed compilation, Java parser can't handle JUnit annotation #20026

Closed SethTisue closed 1 month ago

SethTisue commented 7 months ago

This came up at Lightbend over at https://github.com/akka/alpakka-kafka/pull/1727

% cat S.scala
class S

% cat J.java
//> using scala 3.4.0
//> using dep org.junit.jupiter:junit-jupiter-api:5.10.2
@TestInstance(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS)
class J { }

% scala-cli compile .
Compiling project (Scala 3.4.0, JVM (17))
[error] ./J.java:6:1
[error] ')' expected but eof found.

Next, I will see if I can construct a reproducer that doesn't involve an external library.

som-snytt commented 7 months ago

The linked source is ordinary sbt build, in a Java package, with a correct import. That works in a trivial sbt project.

SethTisue commented 7 months ago

The linked source is ordinary sbt build, in a Java package, with a correct import. That works in a trivial sbt project.

I successfully reproduced the problem in sbt first before switching to scala-cli. Note that you do have to have both Scala and Java sources.

som-snytt commented 7 months ago

No doubt it's one of those things.

➜  i20026-annot git:(master) cat src/main/scala/main.scala

class C {
}
➜  i20026-annot git:(master) cat ./src/main/java/trial/J.java

package trial;

import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class J { }
➜  i20026-annot git:(master) cat build.sbt

scalaVersion := "3.4.0"

ThisBuild / scalacOptions ++= Seq(
  //"-Vprint:all",
)

ThisBuild / libraryDependencies ++= Seq(
  "org.junit.jupiter" % "junit-jupiter-api" % "5.10.2"
)
➜  i20026-annot git:(master) sbt compile
[info] welcome to sbt 1.9.9 (Eclipse Adoptium Java 21.0.2)
[info] loading project definition from .../i20026-annot/project
[info] loading settings for project i20026-annot from build.sbt ...
[info] set current project to i20026-annot
[info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 1 Scala source and 1 Java source to .../i20026-annot/target/scala-3.4.0/classes ...
[success] Total time: 3 s, completed Mar 26, 2024, 12:41:13 PM
SethTisue commented 7 months ago

The plot thickens! The difference between your version and mine is the presence or absence of package trial;.

Which is peculiar, since naturally the original alpakka-kafka code does have a package declaration! So this seems to be rather fragile/subtle...

SethTisue commented 7 months ago

@som-snytt when you were looking into this (thank you!), did you discover whether there's a workaround?