TL;DR: when using Scala.js or Scala Native, you must specify your dependencies slightly differently
When using Scala.js or Scala Native, make sure your dependencies are specified using %%%
, for
example
// Works for JVM, Scala.js and Scala Native
libraryDependencies += "org.scodec" %%% "scodec-bits" % "1.1.32"
Whereas in JVM projects you would see dependencies specified with %%
:
// JVM ONLY!
libraryDependencies += "org.scodec" %% "scodec-bits" % "1.1.32"
Hot tip: if you have Scala.js or Scala Native in any of your SBT modules, you can use %%%
for every dependency. This way both JVM and SJS/SN dependencies will work perfectly.
With Scala.js or Scala Native modules, you need to make sure that your dependency
specification contains two sets of ::
.
For example, this will work for all the platforms:
def ivyDeps = Agg(ivy"com.lihaoyi::scalatags::0.6.7")
but this will work only for JVM:
def ivyDeps = Agg(ivy"com.lihaoyi::scalatags:0.6.7")
Same rules as with Mill!
For example, this set of directives will work for all platforms (note the double set of ::
):
//> using scala 3.3.1
//> using dep org.scodec::scodec-bits::1.1.32
(allowing you to run it with --js
and --native
flags, or without flags at all)
Whereas this specification will only work on the JVM:
//> using scala 3.3.1
//> using dep org.scodec::scodec-bits:1.1.32
Exceptions like this during linking phase:
Referring to non-existent class scodec.bits.ByteVector
called from test$package$.hello()void
called from static hello.main([java.lang.String)void
called from core module module initializers
involving instantiated classes:
test$package$
Referring to non-existent class scodec.bits.ByteVector
called from test$package$.bytes()scodec.bits.ByteVector
called from test$package$.hello()void
called from static hello.main([java.lang.String)void
called from core module module initializers
involving instantiated classes:
test$package$
Referring to non-existent class scodec.bits.ByteVector
called from test$package$.hello()void
called from static hello.main([java.lang.String)void
called from core module module initializers
involving instantiated classes:
test$package$
...
Exceptions like this during linking phase:
[error] Found 5 missing definitions while linking
[error] Not found Top(scodec.bits.Bases$HexAlphabet)
[error] at /private/var/folders/my/drt5584s2w59ncgxgxpq7v040000gn/T/tmp.V0mIJQWj/test.scala:6
[error] Not found Top(scodec.bits.ByteVector)
[error] at /private/var/folders/my/drt5584s2w59ncgxgxpq7v040000gn/T/tmp.V0mIJQWj/test.scala:8
[error] Not found Top(scodec.bits.ByteVector$)
[error] at /private/var/folders/my/drt5584s2w59ncgxgxpq7v040000gn/T/tmp.V0mIJQWj/test.scala:1
[error] Not found Member(Top(scodec.bits.ByteVector$),D12fromValidHexL16java.lang.StringL29scodec.bits.Bases$HexAlphabetL22scodec.bits.ByteVectorEO)
[error] at /private/var/folders/my/drt5584s2w59ncgxgxpq7v040000gn/T/tmp.V0mIJQWj/test.scala:6
[error] Not found Member(Top(scodec.bits.ByteVector$),D22fromValidHex$default$2L29scodec.bits.Bases$HexAlphabetEO)
[error] at /private/var/folders/my/drt5584s2w59ncgxgxpq7v040000gn/T/tmp.V0mIJQWj/test.scala:6
Exception in thread "main" scala.scalanative.linker.LinkingException: Undefined definitions found in reachability phase
at scala.scalanative.linker.Reach.fail(Reach.scala:976)
at scala.scalanative.linker.Reach.reportMissing(Reach.scala:971)
sbt:cats-effect-testing> scalatestJS/test
[success] Total time: 0 s, completed Jun 21, 2022, 2:47:26 PM
Yes that's right! Your tests won't run and it will claim success!!