what to do if sbt compile goes on indefinitely? - scala

I am using sbt 1.6.2 with Scala 3 on a very small code base of 5 files and a few hundred lines of code. Sometimes it compiles fine within a few seconds, and other times it goes into some infinite loop, showing
| => root / Compile / compileIncremental 677s
I can't even figure out how to stop it. I tried control-c and control-d, but neither works, so I end up having to kill the shell. What am I doing wrong?

Related

Error: Could not find or load main class Main Scala

I've recently installed Scala as a part of my functional programming course and I've encountered a problem: IntelliJ IDEA 2017.2.1 (Java version 9, build 9+181) doesn't run any of my scala code, exitting with
Error: Could not find or load main class Main
This code is an example.
object Main {
def length[A](list:List[A]):Int = {
if (list == Nil) 0
else 1 + length(list.tail)
}
def main(args: Array[String]): Unit = {
length(List[Int](1, 4, 5, 12, -1))
}
}
It's really simple, yet IntelliJ refuses to run it. Windows CMD doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command,
operable program or batch file.
even though I have it installed on my computer. If I call Scala Console inside of IntelliJ everything works fine and compiles as expected. I've tried switching to JDK 1.8 inside of IntelliJ, yet it led to no result.
What could be the problem?
For me it turns out that the src/main was not marked as Sources Root
which causes the following error
...
One of the two will be used. Which one is undefined.
Error: Could not find or load main class Main
Process finished with exit code 1
So of course after I mark the src/main as Sources Root, the Scala Hello World example runs happy again.
Notice the blue color of directory src/main when it's marked as Sources Root
Are you using the little green arrow to run the program from inside of your Main object?
How did you create the program? It could be that your build file SBT configuration of the project is a different Scala version than what's installed on your computer.
It's really simple, yet IntelliJ refuses to run it. Windows CMD
doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command, operable program or batch file.
This means that Scala is not added to your class path in your terminal. Look up how to do that and see if that doesn't help out your IntelliJ problem too.

compilation nap - How can I make sbt to ring a bell when compilation is ready?

How can I make sbt to ring a bell (or play a sound, or execute some system command, a shell command) when compilation is ready ?
Compilation usually takes 40 seconds or more, so I'd like to take a little nap while waiting, 100 compilations is already one hour sleep.
you might want to stay on the warm JVM and stay in the SBT console,
There is an SBT plugin for what you're looking for:
https://github.com/orrsella/sbt-sound
It comes with some nice configurable options:
sound.play(compile in Compile, Sounds.Basso) // play the 'Basso' sound whenever compile completes (successful or not)
sound.play(compile in Compile, Sounds.None, Sounds.Pop) // play the 'Pop' sound only when compile fails
sound.play(test in Test, Sounds.Purr, "/Users/me/Sounds/my-sound.wav") // play 'Purr' when test completes successfully
// or the wav file 'my-sound' when it fails
There is a growler plugin which calls growl/notify with test results. This will give you an idea of what to do. Otherwise, a custom task would work as well.
I use simple bash commands:
sbt compile && say "Finish"
If you want notification even when it fails, replace by
sbt compile; say "Finish"

Running scala shell script : Nothing happens

If I run the below script (saved as jarAccessTest.sh, which is an executable file) in a command line, nothing happens:
#!/bin/sh
# exec scala -classpath "/usr/local/google/home/vvasuki/sanskritnlpjava/target/sanskritnlp-1.0-SNAPSHOT.jar " "$0" "$#"
exec scala "$0" "$#"
!#
# import sanskritnlp.transliteration._;
print "hello"
I just see no output. Nothing. Command prompt does not return either.
What is happening here? I have to type Ctrl+C to stop whatever is happening.
EDIT: Using scala 2.9 in ubuntu 14.04 : http://i.imgur.com/VMYKnUX.png
Your pre-packaged version of Scala is from a long time ago and a galaxy far, far away.
You'll do better to download the latest 2.11.6 from the website.
When the scala runner runs a script, it starts a compile server process. If that process is borked, you may have to kill it or run fsc -shutdown to ask it nicely to go away.
You can try adding the -nc option to your script to eliminate that factor, i.e., scala -nc says no compile daemon.
Finally, if you're just testing your library, it's more common to run scala -cp my.jar and experiment from the REPL. Or if you are using SBT, start the console from there.
Anonymous suggests adding: "They seriously let you type anything on here"
To which I'll append the advice from the other answer, to run fsc -verbose. And note that they threaten to stop supporting fsc because it has issues which are maddening when they occur. For that reason, I prefer scala -nc for brief testing and scalac -d script.jar script.scala where script is an App.
The problem on the original computer was never really explained, though it is pointed out that use of the '-nc' flag worked.
The original problem might with the compilation daemon's ability to open a port. From the man page for fsc:
The first time it is executed, the daemon is started automatically.
On subsequent runs, the same daemon can be reused, thus resulting in a faster compilation. The tool is especially effective when repeatedly
compiling with the same class paths, because the
compilation daemon can reuse a compiler instance.
One way to check if this is the problem is to run fsc on its own with the verbose flag. For example, make a script called hello.scala with
println("Hello")
in it, and run
fsc -verbose hello.scala
If this shows the compile server unable to connect, you've identified your problem. A first thing to try in that case would be to check that your hosts file is kosher.
(I had a similar problem, and this is what I found).
you have a few changes to do on your code, mainly on the # line which is not a comment in scala.
#!/bin/sh
# exec scala -classpath "/usr/local/google/home/vvasuki/sanskritnlpjava/target/sanskritnlp-1.0- SNAPSHOT.jar " "$0" "$#"
exec scala "$0" "$#"
!#
// import sanskritnlp.transliteration._;
print ("hello\n")

Scala readLine with prompt displays prompt after line input in SBT

I'm running Scala 2.10 program through sbt run from Windows 7 command line and I see an unexpected behavior while calling readLine overload with the prompt. The prompt is shown after the actual line input.
Source
object MyExample extends App {
readLine("This prompt is shown after the readline!")
}
build.sbt
name := "hello"\n
\n
version := "1.0"\n
\n
Output
asdf
This prompt is shown after the readline!
Is there something I don't understand or is it a bug? It seems to be working as expected from IDEA.
Sbt version: 0.13.1
I've run into this before with giter8. The work around is to do your own print, flush the output stream, and then read. See this pull request for an example of the workaround.
Someone has fixed it in the scala source about a month ago. I don't have any idea when we will see that fix, tho.

strange behaviour of the Eclipse

I have some problems with Eclipse:
Sometimes I write the code and compile it and all works perfectly, after I begin to change my code and compile one more time, but Eclipse does nothing.
Only after I delete the folder Debug it compiles the program
My question is why? And how can I switch the behaviour to compile every time from the beginning.
For example:
if(x == 0) # ... I compile, and it works...
# after I do one change to my code
if(x==0 && y==0) # ... Eclipse do nothing even if y is not declared
I suppose you are talking about a CDT project (C/C++), not a JDT one (Java).
If so, not that if you delete the debug folder, you cannot run directly the application (bug 296243).
Check if the 'Build (Build Incremental)' is disabled or not: bug 198097.