This means that, as Scala.js was analyzing the program for all classes and methods reachable from the main method (or from the tests), if found a call to a method that does not exist, or a new for a class that does not exist, etc.

Linking errors can have several root causes, which we detail here.The appropriate fix will depend on what the root cause is.

A common cause for linking errors is to use %% instead of %%% when depending on another Scala.js library.Having the JVM version of a library on the classpath will allow compilation to succeed, but linking will fail.For example, with the following setting:

  1. libraryDependencies += "io.suzaku" %% "boopickle" % "1.3.1"

and the following code:

  1. import boopickle.Default._
  2. val data = Seq("Hello", "World!")
  3. val buf = Pickle.intoBytes(data)

compilation will succeed but linking will fail with:

  1. [error] Referring to non-existent class boopickle.Default$
  2. [error] called from helloworld.HelloWorld$.main()scala.Unit
  3. [error] called from core module module initializers
  4. [error] involving instantiated classes:
  5. [error] helloworld.HelloWorld$
  6. [error] Referring to non-existent class boopickle.PickleState$
  7. [error] called from helloworld.HelloWorld$.main()scala.Unit
  8. [error] called from core module module initializers
  9. [error] involving instantiated classes:
  10. [error] helloworld.HelloWorld$
  11. [error] Referring to non-existent method boopickle.PickleState$.pickleStateSpeed()boopickle.PickleState
  12. [error] called from helloworld.HelloWorld$.main()scala.Unit
  13. [error] involving instantiated classes:
  14. [error] helloworld.HelloWorld$
  15. [error] Referring to non-existent method boopickle.Default$.stringPickler()boopickle.Pickler
  16. [error] called from helloworld.HelloWorld$.main()scala.Unit
  17. [error] called from core module module initializers
  18. [error] involving instantiated classes:
  19. [error] helloworld.HelloWorld$
  20. [error] Referring to non-existent method boopickle.Default$.Pickle()boopickle.PickleImpl$
  21. [error] called from helloworld.HelloWorld$.main()scala.Unit
  22. [error] called from core module module initializers
  23. [error] involving instantiated classes:
  24. [error] helloworld.HelloWorld$
  25. [error] Referring to non-existent method boopickle.Default$.iterablePickler(boopickle.Pickler,scala.collection.generic.CanBuildFrom)boopickle.Pickler
  26. [error] called from helloworld.HelloWorld$.main()scala.Unit
  27. [error] called from core module module initializers
  28. [error] involving instantiated classes:
  29. [error] helloworld.HelloWorld$

If the Referring to non-existent lines mention a library that you depend on, and the first corresponding called from lines mention your code, it is very likely that an erroneous %% is the cause.

Solution

Use %%% instead of %% when depending on other Scala.js librarys:

  1. libraryDependencies += "io.suzaku" %%% "boopickle" % "1.3.1"

The JavaScript platform does not support blocking at all.Consequently, trying to use blocking APIs such Await.result is not supported, and will result in a linking error.A typical example was shown in the introduction, and is reproducible with the following code:

  1. import scala.concurrent.{Await, Future}
  2. import scala.concurrent.ExecutionContext.Implicits.global
  3. import scala.concurrent.duration.Duration
  4. val f = Future { 5 }
  5. println(Await.result(f, Duration.Inf))
  1. [error] Referring to non-existent method scala.concurrent.impl.Promise$CompletionLatch.releaseShared(scala.Int)scala.Boolean
  2. [error] called from scala.concurrent.impl.Promise$CompletionLatch.apply(scala.util.Try)scala.Unit
  3. [error] called from scala.concurrent.impl.Promise$CompletionLatch.apply(java.lang.Object)java.lang.Object
  4. [error] called from scala.util.Success.$$anonfun$map$1(scala.Function1)java.lang.Object
  5. [error] called from scala.util.Success.map(scala.Function1)scala.util.Try
  6. [error] called from scala.concurrent.Future.$$anonfun$map$1(scala.Function1,scala.util.Try)scala.util.Try
  7. [error] called from scala.concurrent.impl.Promise$DefaultPromise.$$anonfun$map$1(scala.Function1,scala.util.Try)scala.util.Try
  8. [error] called from scala.concurrent.Future.map(scala.Function1,scala.concurrent.ExecutionContext)scala.concurrent.Future
  9. [error] called from scala.concurrent.impl.Promise$KeptPromise$Successful.map(scala.Function1,scala.concurrent.ExecutionContext)scala.concurrent.Future
  10. [error] called from scala.concurrent.Future$.apply(scala.Function0,scala.concurrent.ExecutionContext)scala.concurrent.Future
  11. [error] called from helloworld.HelloWorld$.main()scala.Unit

and other errors.

Hint to recognize this cause

If the Referring to non-existent line mentions CompletionLatch, it is probably related to trying to block on a Future.

Always use asynchronous combinators, such as map, filter, flatMap, foreach, and/or for comprehensions, when using Future.Do not use scala.concurrent.Await.

Scala.js reimplements parts of the JDK libraries, but not everything is supported.For example, the following code

  1. println(new java.io.File("foo.txt").exists())

which directly uses java.io.File, results in

  1. [error] Referring to non-existent class java.io.File
  2. [error] called from helloworld.HelloWorld$.main()scala.Unit
  3. [error] called from core module module initializers
  4. [error] involving instantiated classes:
  5. [error] helloworld.HelloWorld$
  6. [error] Referring to non-existent method java.io.File.exists()scala.Boolean
  7. [error] called from helloworld.HelloWorld$.main()scala.Unit
  8. [error] called from core module module initializers
  9. [error] helloworld.HelloWorld$
  10. [error] Referring to non-existent method java.io.File.<init>(java.lang.String)
  11. [error] called from helloworld.HelloWorld$.main()scala.Unit
  12. [error] called from core module module initializers
  13. [error] involving instantiated classes:
  14. [error] helloworld.HelloWorld$
  15. [error] There were linking errors

The following code indirectly uses java.io.File through scala.io:

yields

  1. [error] Referring to non-existent class java.io.File
  2. [error] called from scala.io.Source$.fromFile(java.lang.String,scala.io.Codec)scala.io.BufferedSource
  3. [error] called from scala.io.Source$.fromFile(java.lang.String,java.lang.String)scala.io.BufferedSource
  4. [error] called from helloworld.HelloWorld$.main()scala.Unit
  5. [error] called from core module module initializers
  6. [error] ...
  7. [error] There were linking errors

Hint to recognize this cause

If the Referring to non-existent line mentions a class from the java.* packages, you are probably trying to use an unsupported API.

Solution

  1. libraryDependencies += "io.github.cquiroz" %%% "scala-java-locales" % "0.5.2-cldr31"

You can find a list of JDK extensions in the Scala libraries list.

If the API you want to use is not provided by any third party (e.g., java.io.File), then you are out of luck.You must avoid that API, possibly using a JavaScript library instead (e.g., ).

Sometimes, the incremental compiler of Scala, called Zinc and used by most build tools, exhibits under-compilation bugs.In that case, it is possible that the compiled .sjsir files are not completely in sync with the sources, and cannot be linked together.

  1. [error] Referring to non-existent method helloworld.Foo.bar(java.lang.String)scala.Unit
  2. [error] called from helloworld.HelloWorld$.main()scala.Unit
  3. [error] called from core module module initializers
  4. [error] involving instantiated classes:
  5. [error] helloworld.HelloWorld$
  6. [error] There were linking errors

If the Referring to non-existent line mentions a class from your own codebase, you are probably facing an incremental compilation issue.

Solution

clean the project (or possibly all projects) in your sbt build (or the equivalent operation in other build tools).Retrying after clean should fix the issue.

When depending on multiple libraries, it is possible that several of them transitively depend on two incompatible versions of the same library.Here is a hypothetical example:

  1. [error] Referring to non-existent method somelib.SomeLib.bar(java.lang.String)scala.Unit
  2. [error] called from someotherlib.SomeOtherLib.foo(java.lang.String)scala.Unit
  3. [error] called from helloworld.HelloWorld$.main()scala.Unit
  4. [error] called from core module module initializers
  5. [error] ...

In this case, someotherlib was compiled against some version x of somelib, but a new version y > x of somelib is used.In that new version, the method SomeLib.bar(String) does not exist anymore, causing a binary incompatibility.

Hint to recognize this cause

If the line mentions one library, and the first called from line mentions another library, there is probably a binary incompatibility between those two libraries.

Find a set of versions for your libraries that are binary compatible.This may involve upgrading and/or downgrading some of them.