Description
Compiler version
3.0.0
Minimized code
in pom.xml
add to dependencies:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>3.0.0</version>
</dependency>
and then try to build the app with mvn
. (please read on for detailed explanation).
Output
[WARNING] There are 1 pathException(s). The related dependencies will be ignored.
[WARNING] Dependency: /home/makingthematrix/.m2/repository/org/scala-lang/scala3-library_3/3.0.0/scala3-library_3-3.0.0.jar
- exception: Unable to derive module descriptor for /home/makingthematrix/.m2/repository/org/scala-lang/scala3-library_3/3.0.0/scala3-library_3-3.0.0.jar
- cause: scala3.library.3: Invalid module name: '3' is not a Java identifier
Expectation
There should be no warning about "'3' is not a Java identifier" and no later errors when the code tries to access the Scala library.
Details
This will happen in projects which use together Scala, Maven, and Java 9+. It's not a common combination, but currently this is the way to write programs like this one - an Android app written in Scala 3 that uses JavaFX as GUI, and is built with GraalVM native image. Currently there is no way to do it with SBT. But Maven works pretty well. Except this one issue.
Since Java 9, the Scala way of adding a suffix with the Scala version at the end of artifact id of the library is invalid - hence the error above. An easy way to fix it is to add a line to MANIFEST.MF
of the library's jar file:
Automatic-Module-Name: <library name>
This can be done from SBT. For example in case of scala3-library_3
it's enough to add something like this to dotty/project/Build.scala
:
lazy val `scala3-library` = project.in(file("library")).
asDottyLibrary(NonBootstrapped).
settings(
Compile / packageBin / packageOptions += Package.ManifestAttributes("Automatic-Module-Name" -> "scala3-library")
)
lazy val `scala3-library-bootstrapped`: Project = project.in(file("library")).
asDottyLibrary(Bootstrapped).
settings(
Compile / packageBin / packageOptions += Package.ManifestAttributes("Automatic-Module-Name" -> "scala3-library")
)
Please note that usually it's better to hardcode the value scala3-library
instead of trying to derive it from the library name.
Actually, the same problem exists for all Scala 2 libraries. I wrote a Maven plugin that fixes this downstream - i.e. it unzips the downloaded jar file of the given Scala library, modifies MANIFEST.MF
, and zips it again - but it would be better to have it fixed at the source. Here's the link to the plugin page where you can read more about this issue.
I'm planning to create a PR here in the Dotty project and when it's done I will link it to this bug report. This is the first time I do it, so sorry if I mess up something.