Linker error in ScalaJS: "Referring to non-existent class" - scala.js

I am getting a bunch of linker errors when trying to link to FastParse in a Scala project which I've just tried to build a ScalaJS web app in.
I added the ScalaJS dependency for FastParse into my build.sbt: here's approximately the relevant line (full file here):
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "fastparse" % "0.4.1"
)
My errors are voluminous, but here's a representative one:
[error] Referring to non-existent class fastparse.Implicits$Repeater$
[error] called from parsers.MainParser$$anonfun$impls$1.apply()fastparse.core.Parser
[error] called from parsers.MainParser$$anonfun$impls$1.apply()java.lang.Object
[error] called from scala.Option.getOrElse(scala.Function0)java.lang.Object
[error] called from fastparse.StringReprOps$.errorMessage(fastparse.utils.ParserInput,java.lang.String,scala.Int)java.lang.String
[error] called from fastparse.core.ParseError.<init>(fastparse.core.Parsed$Failure)
[error] called from fastparse.Api.<init>(scala.reflect.ClassTag,fastparse.utils.ElemSetHelper,fastparse.utils.ReprOps,scala.math.Ordering)
[error] called from fastparse.StringApi.<init>()
[error] called from fastparse.all$.<init>()
[error] called from parsers.MainParser$.<init>()
[error] called from webapp.WebApp$.makeChoices(java.lang.String,java.lang.String)scala.collection.immutable.List
[error] called from webapp.WebApp$.$$js$exported$meth$makeChoices(java.lang.String,java.lang.String)java.lang.Object
[error] called from webapp.WebApp$.makeChoices
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] parsers.MainParser$$anonfun$impls$1
[error] scala.None$
[error] scala.Some
[error] fastparse.StringReprOps$
[error] webapp.WebApp$
What am I doing wrong?

The problem was that I was requiring the JVM version of FastParse two lines up in my build.sbt. This broke my build apparently. I removed that line and cleaned my build and now both the ScalaJS and ScalaJVM versions of the project are building nicely.
(Thanks heaps to Li Haoyi for spotting the problem for me)

Related

ScalaJs + ZIO: Program works with sbt 1.2.8 but not >= 1.3

I'm trying out scala.js with zio using the sample app at
https://github.com/wongelz/zio-scalajs-solarsystem
as soon as I update the sbt version from 1.2.8 to 1.3.13 or 1.4.4 I'm getting the following error:
[error] Referring to non-existent method java.time.LocalTime$.NANOS_PER_SECOND()long
[error] called from private java.time.LocalDateTime.plusWithOverflow(java.time.LocalDate,long,long,long,long,int)java.time.LocalDateTime
[error] called from java.time.LocalDateTime.plusNanos(long)java.time.LocalDateTime
[error] called from java.time.LocalDateTime.plus(long,java.time.temporal.TemporalUnit)java.time.LocalDateTime
[error] called from java.time.LocalDateTime.plus(long,java.time.temporal.TemporalUnit)java.time.temporal.Temporal
[error] called from java.time.temporal.ChronoUnit.addTo(java.time.temporal.Temporal,long)java.time.temporal.Temporal
[error] called from java.time.OffsetDateTime.plus(long,java.time.temporal.TemporalUnit)java.time.OffsetDateTime
[error] called from java.time.OffsetDateTime.plus(long,java.time.temporal.TemporalUnit)java.time.temporal.Temporal
[error] called from java.time.Duration.addTo(java.time.temporal.Temporal)java.time.temporal.Temporal
[error] called from java.time.OffsetDateTime.plus(java.time.temporal.TemporalAmount)java.time.OffsetDateTime
[error] called from private zio.Schedule$.$anonfun$fixed$2(scala.Option,java.time.OffsetDateTime,long,java.time.Duration,long,scala.runtime.LazyRef)zio.Schedule$Decision
[error] called from private zio.Schedule$.$anonfun$fixed$1(scala.Option,long,java.time.Duration,long,scala.runtime.LazyRef,java.time.OffsetDateTime,java.lang.Object)zio.ZIO
[error] called from private zio.Schedule$.loop$23(scala.Option,long,long,java.time.Duration,scala.runtime.LazyRef)scala.Function2
[error] called from zio.Schedule$.fixed(java.time.Duration)zio.Schedule
[error] called from private SolarSystemExample$.$anonfun$run$1(SolarSystemExample$SolarSystem)zio.ZIO
[error] called from SolarSystemExample$.run(scala.collection.immutable.List)zio.ZIO
[error] called from private zio.App.$anonfun$main$1([java.lang.String)zio.ZIO
[error] called from zio.App.main([java.lang.String)void
[error] called from SolarSystemExample$.main([java.lang.String)void
[error] called from static SolarSystemExample.main([java.lang.String)void
[error] called from core module module initializers
[error] involving instantiated classes:
[error] java.time.LocalDateTime
[error] java.time.temporal.ChronoUnit
[error] java.time.OffsetDateTime
[error] java.time.Duration
[error] zio.Schedule$
[error] SolarSystemExample$
Why does this bug happen? And where should I report it?
To answer my own question (for anybody strugling with the same problem):
Make sure you don't have scalajs-java-time (1.0.0) as a dependency in your classpath.
It is a incomplete library and if it is picked before scala-java-time you will receive the error posted in the question.
The reason this error occurred, was, that at least on my system, the ordering of the classpath changed from sbt 1.2.8 to sbt 1.3.x, which resulted in the scalajs-java-time library being picked before scala-java-time which resulted in the
Referring to non-existent method java.time.LocalTime$.NANOS_PER_SECOND()long
Error

Scalac option Yopt-log-inline make errors

With scala 2.12.4 when I use theses parameters
scalacOptions := Seq("-unchecked", "-deprecation", "-opt:l:inline","-opt-inline-from:bfd.DateTime$","-Yopt-log-inline")
I have error which doesn't exist when I don't use "-Yopt-log-inline"
[info] Compiling 7 Scala sources to /Users/admin/Workspace/bfd/target/scala-2.12/classes ...
[error] error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
[error] ## Exception when compiling 7 sources to /Users/admin/Workspace/bfd/target/scala-2.12/classes
[error] object scala in compiler mirror not found.
[error] scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:17)
[error] scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:18)
[error] scala.reflect.internal.Mirrors$RootsBase.$anonfun$getModuleOrClass$4(Mirrors.scala:54)
[error] scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:54)
[error] scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:66)
[error] scala.reflect.internal.Mirrors$RootsBase.getPackage(Mirrors.scala:172)
[error] scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage$lzycompute(Definitions.scala:169)
[error] scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage(Definitions.scala:169)
[error] scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass$lzycompute(Definitions.scala:170)
[error] scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass(Definitions.scala:170)
[error] scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1423)
[error] scala.tools.nsc.Global$Run.<init>(Global.scala:1164)
[error] xsbt.ZincCompiler$ZincRun.<init>(CallbackGlobal.scala:63)
[error] xsbt.CachedCompiler0.run(CompilerInterface.scala:129)
[error] xsbt.CachedCompiler0.run(CompilerInterface.scala:106)
[error] xsbt.CompilerInterface.run(CompilerInterface.scala:32)
[error] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...
I think the error is not related to the usage of inline. Maybe the options override the scala dependency definition.
Is it a bug in the compiler or just a bad setting with an unclear feedback?
-Yopt-log-inline expect a parameter. The equivalent to the wild card of -opt-inline-from:** is the underscore _.
So to correct this error add this parameter to scalacOptions like this.
scalacOptions ++= Seq("-opt:l:inline","-opt-inline-from:bfd.DateTime$","-Yopt-log-inline", "_")
edit:
What hepping here is a problem between scalac and sbt, sbt gives the class pass next to Scala option, so scalac think the classpath is the option of -Yopt-log-inline and start without actual classpath, which make a crach.
If you are using sbt and don't want to fall in this kind of error you can use "-Y" as your last option.

How to use scala.sys.process with scala.js

I have imported scala.sys.process._ into my scala.js project. That alone doesn't cause any problems, but if I add a simple command such as println("ls".!!) I get a gazillion errors, such as
[error] Referring to non-existent class java.lang.ProcessBuilder
[error] called from scala.sys.process.ProcessCreation.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessCreation.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessCreation.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessImplicits.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.package$.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from draw.Main$.main(org.scalajs.dom.raw.HTMLCanvasElement)scala.Unit
[error] called from draw.Main$.$$js$exported$meth$main(org.scalajs.dom.raw.HTMLCanvasElement)java.lang.Object
[error] called from draw.Main$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] scala.sys.process.Process$
[error] scala.sys.process.package$
[error] draw.Main$
[error] Referring to non-existent class java.io.File
[error] called from scala.sys.process.ProcessCreation.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(scala.collection.Seq,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessCreation.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(java.lang.String,scala.Option,scala.collection.Seq)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessCreation.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.Process$.apply(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.ProcessImplicits.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from scala.sys.process.package$.stringToProcess(java.lang.String)scala.sys.process.ProcessBuilder
[error] called from draw.Main$.main(org.scalajs.dom.raw.HTMLCanvasElement)scala.Unit
[error] called from draw.Main$.$$js$exported$meth$main(org.scalajs.dom.raw.HTMLCanvasElement)java.lang.Object
[error] called from draw.Main$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] scala.sys.process.Process$
[error] scala.sys.process.package$
[error] draw.Main$
[error] Referring to non-existent method java.lang.ProcessBuilder.environment()java.util.Map
Importing extra classes such as java.lang.ProcessBuilder and java.io.File has no impact on the content of these errors. Is there something very simple that I am missing here?
Thanks!
You can't simply import arbitrary Scala libraries into Scala.js -- while the language is the same, the environment is very different. Many standard Scala libraries simply don't exist in the SJS world, and many of them can't, because of the limitations of the JavaScript environment it runs in. It's syntactically legal, so it will compile, but it can't run before there isn't a Scala.js version of the library.
By and large, you should assume that libraries like this don't exist in the SJS world, unless you find that someone has specifically ported it. (I honestly don't know whether someone has ported scala.sys.process for Node.js; it doesn't make much sense in the browser environment...)

ScalaJS and MongoDB driver: Referring to non-existent class

Learning Scala since yesterday, I've build a project to interact with a MongoDB index (which works fine) and another one using ScalaJS to do the same thing.
Here is my build.sbt :
enablePlugins(ScalaJSPlugin)
name := "demographics"
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.2.1"
libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.9.1"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.1"
skip in packageJSDependencies := false
jsDependencies +=
"org.webjars" % "jquery" % "3.1.1" / "3.1.1/jquery.js"
and my main file:
package demographics.webapp
import org.mongodb.scala._
import scala.scalajs.js.JSApp
object DemographicsApp extends JSApp {
def main(): Unit = {
// connect to the bdd
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("demographics")
val collection: MongoCollection[Document] = database.getCollection("demographics")
}
}
Very basic: just connect to the collection.
Sadly, when I launch a "fastOptJS" on the sbt shell, the following error appears:
[info] Compiling 1 Scala source to C:\...\target\scala-2.12\classes...
[info] Fast optimizing C:\...\target\scala-2.12\untitled1-fastopt.js
[error] Referring to non-existent class org.mongodb.scala.bson.DefaultHelper$DefaultsTo$
[error] called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error] called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.DemographicsApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] demographics.webapp.PrenomApp$
[error] Referring to non-existent class org.mongodb.scala.MongoClient$
[error] called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error] called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.DemographicsApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] demographics.webapp.DemographicsApp$
[error] Referring to non-existent class org.mongodb.scala.bson.collection.immutable.Document
[error] called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error] called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.DemographicsApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] demographics.webapp.DemographicsApp$
[error] Referring to non-existent method org.mongodb.scala.bson.DefaultHelper$DefaultsTo$.$default()org.mongodb.scala.bson.DefaultHelper$DefaultsTo
[error] called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error] called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.DemographicsApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] demographics.webapp.DemographicsApp$
[error] Referring to non-existent method org.mongodb.scala.MongoClient$.apply()org.mongodb.scala.MongoClient
[error] called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error] called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error] called from demographics.webapp.DemographicsApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] demographics.webapp.DemographicsApp$
[trace] Stack trace suppressed: run last compile:fastOptJS for the full output.
[error] (compile:fastOptJS) There were linking errors
The same code (with adaptations in the main file) is working on the vanilla Scala project, why this error appears?
It appears because you use a JVM-only library. mongo-scala-driver is not published for Scala.js. It it were, you could use a %%% dependency to depend on the Scala.js version of that library, which would work with Scala.js. But you cannot use a JVM-only library in a Scala.js project.
(Conversely, you cannot use a Scala.js-only library in a Scala/JVM project, for example, scalajs-dom.)

Class Resolving in one namespace, but not the other

When running play compile, I get a compile-time error.
[error] test.scala:14: object BOMInputStream is not a member of package org.ap
ache.commons.io.input
[error] import org.apache.commons.io.input.BOMInputStream
[error] ^
[error] test.scala:80: not found: type BOMInputStream
[error] val bomIn = new BOMInputStream(fileInpStream, false)
[error] ^
[error] two errors found
However, I successfully ran a scalatest test using the BOMInputStream in the same play project within the /test directory.
When I comment out the offending lines in the above compile-time error, the test succeeds.
Note that I've updated my /project/Build.scala appropriately:
"org.apache.commons" % "commons-io" % "1.3.2"
After deleting a JAR, which contained the BOMInputStream class, from my PLAY-PROJECT/lib/ directory, I was able to compile.