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

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...)

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

Gatling compilation failed

I'm trying to build https://github.com/gatling/gatling but the compilation fails. The steps I undertook:
Installed sbt using the documentation.
Clone the github project.
Execute sbt compile in the directory gatling.
I get the following error:
[error] /home/groot/gatling/gatling-commons/src/main/scala/io/gatling
/commons/util/Arrays.scala:36:32: macro implementation not found: cfor
[error] (the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them)
[error] cfor(length)(_ > 1, _ - 1) { i =>
[error] ^
[error] /home/groot/gatling/gatling-commons/src/main/scala/io/gatling/commons/util/StringHelper.scala:40:23: macro implementation not found: fast
[error] (the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them)
[error] val EmptyFastring = fast""
[error] ^
[error] /home/groot/gatling/gatling-commons/src/main/scala/io/gatling/commons/validation/package.scala:38:20: macro implementation not found: info
[error] (the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them)
[error] logger.info(message, e)
[error] ^
[error] three errors found
[error] (gatling-commons / Compile / compileIncremental) Compilation failed
[error] Total time: 10 s, completed May 4, 2018, 11:30:14 AM
I do not know enough of scala to debug this, how can I fix this?
Try IntelliJ Idea with scala plugin
Just use IntelliJ Idea open the project folder
I've executed as sudo (sudo sbt compile) and now the compilation is succesful.

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

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)

Can't build a simple project with Lift and Scala.js

I made this simple project: https://github.com/eje211/scalajswithlift
I tried to create a JSON object with Lift DSL and print it in the HTML console with Scala.js . But I get this error:
[info] Compiling 1 Scala source to /Users/eeytan/src/scalajs/target/scala-2.11/classes...
[info] Fast optimizing /Users/eeytan/src/scalajs/target/scala-2.11/scala-js-tutorial-fastopt.js
[error] Referring to non-existent class Lnet_liftweb_json_JsonDSL$
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent class Lnet_liftweb_json_package$
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent class Lnet_liftweb_json_JsonAST$
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonAST$.render__Lnet_liftweb_json_JsonAST$JValue__s_text_Document
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.int2jvalue__I__Lnet_liftweb_json_JsonAST$JInt
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.pair2Assoc__T2__F1__Lnet_liftweb_json_JsonDSL$JsonAssoc
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_JsonDSL$.string2jvalue__T__Lnet_liftweb_json_JsonAST$JString
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[error] Referring to non-existent method Lnet_liftweb_json_package$.pretty__s_text_Document__T
[error] called from Ltutorial_webapp_TutorialApp$.main__V
[error] called from sjs_js_JSApp$class.$$js$exported$meth$main__sjs_js_JSApp__O
[error] called from Ltutorial_webapp_TutorialApp$.$$js$exported$meth$main__O
[error] called from Ltutorial_webapp_TutorialApp$.main
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] Ltutorial_webapp_TutorialApp$
[trace] Stack trace suppressed: run last compile:fastOptJS for the full output.
[error] (compile:fastOptJS) There were linking errors
[error] Total time: 1 s, completed Oct 30, 2015 5:21:03 PM
I don't understand why.
If I remove the comment from build.sbt, I get this error:
> ~fastOptJS
[trace] Stack trace suppressed: run last compile:scalaJSPreLinkClasspath for the full output.
[error] (compile:scalaJSPreLinkClasspath) org.scalajs.core.tools.classpath.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.js
[error] Possible paths found on the classpath:
[error] - scala/tools/nsc/doc/html/resource/lib/jquery.js
[error] - META-INF/resources/webjars/jquery/2.1.3/jquery.js
[error] originating from: scalajs:compile
[error] Total time: 1 s, completed Oct 30, 2015 5:35:09 PM
I don't understand what's going on. Can't I use the Lift JSON DSL in Scala.js?
Lift, and Lift JSON, are JVM-only projects. They have not been ported/built for Scala.js, as far as I know. So no, you cannot use them in Scala.js.
In general, libraries need to be cross-compiled and published for both Scala/JVM and Scala.js for you to be able to use them. If it makes sense, you could ask Lift developers to cross-compile (some of) their libraries.
That said, in Scala.js, you could typically use JS APIs to work with JSON data structures:
import scala.scalajs.js
import js.Dynamic.{literal => lit}
val j = lit(a = 2, b = "Hello.")
println(js.JSON.stringify(j, space = 2))
Moreover, typically, you wouldn't even manipulate JSON directly at all. Instead, you would use any of the serialization libraries available for Scala.js, such as uPickle, Prickle or BooPickle.

Bad Symbolic reference to reactivemongo.api.collections.GenericHandlers encountered in class file 'JSONGenericHandlers.class'

I'm having my apis in play 2.3 with reactive mongo. Recently, i tried to cleaned the project and during the process, some things got updated. Later, when i tried to run or compile that, i'm getting these errors. Apart from clean, i didn't do anything. Kindly help me.
[info] Compiling 48 Scala sources and 1 Java source to /home/Ruthvick/zcapi/zceapi /target /scala-2.11/classes...
[error] bad symbolic reference to reactivemongo.api.collections.GenericHandlers encountered in class file 'JSONGenericHandlers.class'.
[error] Cannot access type GenericHandlers in package reactivemongo.api.collections. The current classpath may be
[error] missing a definition for reactivemongo.api.collections.GenericHandlers, or JSONGenericHandlers.class may have been compiled against a version that's
[error] incompatible with the one found on the current classpath.
[error] /home/Ruthvick/zcapi/zceapi/app/controllers/Application.scala:28: type arguments [play.modules.reactivemongo.json.collection.JSONCollection] do not conform to method collection's type parameter bounds [C <: reactivemongo.api.Collection]
[error] def collection: JSONCollection = db.collection[JSONCollection]("shoppage")
[error] ^
[error] /home/Ruthvick/zcapi/zceapi/app/controllers/Application.scala:47: could not find implicit value for parameter writer: GenericCollection.this.pack.Writer[play.api.libs.json.JsObject]
[error] collection.insert(result).map { lastError =>
[error] ^
[error] 60 errors found
[error] (compile:compile) Compilation failed
[error] application -
Thanks,
Your version of Play is not compatible with the newest Snapshot of Play Reactive Mongo. You could just use Version 0.10.5.
Add this to your Library Dependencies
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23"