Run Scala application in Scala IDE with compilation errors - eclipse

Is there a way to run a Scala application or unit test within the Scala IDE 2.0.2 (Eclipse 3.7) if there are (unrelated!) compilation errors in the project?
In Java, this is no problem, but Scala IDE keeps telling me:
Project contains compilation errors (therefore, no binaries have been produced).
All I want to do is to run a small test during a major refactoring process, but I can't fix all compilation errors right now. And I don't want to start commenting things out, as those compilation errors are good reminders of tasks that still need to be done.

Currently the Scala IDE does not support this behavior, but it would be nice if it could.
To be aware of this, I created a ticket:
JDT allows to execute Java code despite of compilation problems in the sources. Internally JDT replaces the defective code with an exception, which is thrown if the relevant code is executed. But if the defective code is never called during execution, nothing bad happens. It would be nice if SDT supports similar behavior.

If you are using Scala 2.10, a good option is to use the newly introduced ??? notation. This way your code will compile even if the function is not yet implemented.
There is certainly a better way but this one will still be better than commenting code.
As suggested by Jesper, you can still implement ??? if you are working with previous versions of Scala:
def ??? : Nothing = throw new Error("Not implemented")

Related

IntelliJ suddenly stops highlighting errors in Scala/Dotty project [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
TLDR; IntelliJ isn't highlighting any errors (syntax/type mismatch) in my Scala project. I've tried type-aware highlighting, setting highlighting level to "Inspections", and enabling "Experimental Features" according to StackOverflow answers, and reloading the project.
I have a Scala 2.13 project (sbt) that was working just fine in IntelliJ 2019.3. I made some changes after that (I did not check to see if it compiled, and it had several errors in it) and then I decided to port it to Dotty (and also updated IntelliJ to 2020.1 EAP). I created a new Dotty project, which obviously had no errors.
I then copied all my source files to this project and modified the build.sbt file. When I did this, I could see a few syntax errors highlighted, and the given keyword was not detected as a keyword, most likely because the plugin thought the language was supposed to be Scala 2, not Scala 3.
After that, I imported the project from sbt, and the wrong syntax errors disappeared, so I happily closed that file and started fixing the others. I'm not sure if error highlighting was working at first and then turned off, or if it never worked. After a while, I realized that unresolved references weren't highlighted - for example, when I mispelled a class name and tried to go to the declaration, it didn't do anything, although when I corrected it, it could resolve the reference, presumably because that was one of the files that had no errors. Then I saw that syntax errors weren't recognized either.
I have type-aware highlighting enabled, and the highlighting level is set to inspections. I am using IntelliJ 2020.1 EAP with Scala plugin build 2020.1.977 (EAP). The build.sbt contains scala version "0.23.0-RC1". IntelliJ wasn't running any other processes, and there weren't any resource-intensive programs on my computer at the time.
I got multiple error messages at the bottom right, such as
java.lang.AssertionError: assertion failed (A couple times)
com.intellij.psi.PsiInvalidElementAccessException: Element: class org.jetbrains.plugins.scala.lang.psi.impl.statements.params.ScTypeParamImpl #Scala because: different providers: org.jetbrains.plugins.scala.lang.psi.ScFileViewProvider{vFile=file:<The path>/Pattern.scala, content=VirtualFileContent{size=1716}, eventSystemEnabled=true}(a24437); org.jetbrains.plugins.scala.lang.psi.ScFileViewProvider{vFile=file:<Same path>/Pattern.scala, content=VirtualFileContent{size=1716}, eventSystemEnabled=true}(b39181a) invalidated at (This one came up several times)
I have a feeling this is just a bug with Dotty that will be resolved soon. The Scala Nightly plugin had similar behavior, which is why I switched to EAP and also created an entirely new project, which I assumed would be less buggy. Any suggestions would be really appreciated.
The same thing is happening with my old Scala 2 project also when I open it in IntelliJ 2020.1 EAP but not in 2019.3. It now seems it's not a Dotty-specific thing.
Dotty oficialy is not supported yet because its syntax is not frozen, a thing like optional braces is being changed now. The only place where everything works relatively fine is an official vscode, and it used by compiler devs. So, if you want to play with dotty, a vscode and all the env probably would be your choice: https://dotty.epfl.ch/docs/usage/ide-support.html
As of IntelliJ Scala Plugin 2020.1 Scala 3/dotty is getting support
The Scala plugin now includes preliminary support for Scala 3: the new syntax, error highlighting, navigation, type info, and many other bread-and-butter IDE features.
Because both the language and the language support are still works in progress, we recommend using plugin nightly builds if you want to get new features and bug fixes as quickly as possible.
More details can be found in the IntelliJ Scala Plugin Blog

scala sbt compilation horror due to slowness and various issues

I am having a very horrible time coding in scala using sbt.
I know compilation in scala is inherently slow as compared to java.
I have tried both eclipse and intellij, to do development in scala + sbt, and both the well knows IDE sucks at the job. Please guide me with the following horrors I am facing:
How to reduce the compilation time to milliseconds in scala(just like it was for java) even for big projects....in the IDEs(eclipse/intellij)....given that I am using sbt
How to ensure the builds in the IDEs work incrementally and on changed files only. I observe that full build happens in the IDE on every code change/save of the file.....again keep in mind that I am using sbt
Please suggest the same for the command line, after every code change, when I do sbt package, and it seems sbt recompiles the complete project rather than changed files.
In intellij, when I try to run a unit test cases, the whole sbt based project is compiled again
Also, if I import a project in intellij and use sbt command line at the same time, the intellij build and the command line build don't play well with each other and I start to observe full compilations in intellij and weired compilation errors
This is only a partial answer, because it depends a lot on what you're doing and what your code looks like - but I tend not to use an IDE for compilation/tests. Instead, I run this in sbt (sometimes with other commands, depending on what I'm doing)
> ~test-quick
which (quoting the docs):
The tests that failed in the previous run
The tests that were not run before
The tests that have one or more transitive dependencies, maybe in a different project, recompiled.
The tilde makes it run every time there has been a code change
For me anyway, it's very fast and importantly only runs what could have changed
Another thing which I have heard (but can't promise will speed things up) is to explicitly type as much as you can. The type inferrer is meant to be quite slow (which makes sense when you think about what it must have to do)
Edit as requested by Saby
Before I go through the points you've raised, I just want to point out that these aren't really horrors and eclipse/intellij don't actually suck at what they do. In fact, when you think about it, it's impressive they work at all.
Scala is a far more complicated language than java and that means the compiler has to do a lot more than java's. What I'm talking about here is features such as macros, implicits and type inference. Those features aren't free and compilation time and memory will take hits here. (basically because the compiler has to all sorts of crazy stuff for type inference; run through the code multiple times for macros; etc, etc, etc)
Obviously, like any other language, your compilation time is determined by a lot of things: what features you use, what patterns you use, what dependencies you have to name just a few.
Anyway, point by point:
I honestly don't know how to answer this one. I am currently working on a smallish java project (think a dozen dependencies, a few thousand lines of code, some spring but not much else going on) and I am certainly not getting millisecond compilation times in Intellij. If this really is causing problems, then my only suggestion is to use the command line.
I don't really know anything at all about eclipse, but if you are using Intellij 13 you can set the incremental compiler like this. The sbt plugin was changed quite a bit in Intellij 14 so I don't think this is an option anymore (ie it does whatever it does)
I think my answer above covers this. The point is you don't have to use sbt package, and I don't typically. Instead type sbt and when it loads type ~test-quick and only run sbt package when you need to
That's probably just how intellij works. Like 3, I don't think you can change this anymore
I don't know what these are, but you should probably Google the error and if you can't find anything create a new Stack Overflow question with some examples.
Other things to consider
Setup can be important. Most hardcore Scala developers I know code in sublime text or emacs using a plugin called ensime and sbt on the command line to test/compile it (along with a repl or two). Otherwise (at the time of writing this anyway), most other people (including me) use Intellij to write their code and then either use sbt on the command line or Intellij itself to test/compile. You need to play about and find what's best for you.
Another thing you want to think about is the version of sbt you're on. I don't have benchmarks for this, but I remember older versions being slower and it would be painful to change the way you work just because you're running an old version of something.
Also make sure you have the java 8 sdk installed - I know the girls and guys at Typesafe are spending more and more time optimising sbt for java 8.
Points 2 and 4 just work for me (and have for a long time), no special setup required.
Also, if I import a project in intellij and use sbt command line at the same time, the intellij build and the command line build don't play well with each other and I start to observe full compilations in intellij and weired compilation errors
This should be fixed in the new Scala plugin version.

Scala ClassNotFoundException Random Constant

I have quite a few mixed scala and java projects. They will run just fine up to some seemingly random time when they don't.
Compile goes fine, with zero errors or warnings, yet on run I get the infamous
ClassNotFoundException
In this current project I have only Scala files, and I deleted all of them and left it at a simple Test object with main method.
YET IT STILL SAYS ClassNotFoundException
I have scoured the net, and tried everything I have found.
Has anyone had this experience, and moreover, are there any suggestions?
Are you trying to run a run configuration for a class that have been deleted? If you're using Intellij and pressing Shift-F10 it will happily try to run a configuration even though the class does not exist anymore.
My other related question actually provides more insight and provides a solution to this problem.
Running Objects in Play 2.0 Framework
Hope it helps when everyone is using Scala in a few years...

Scala # IntelliJ 12 - Can't fetch docs / some functions are not shown in dropdown. [BUGS]

I've been searching for a long time now to find a decent IDE for Scala.
Eclipse is not satisfying at all, as auto completion doesn't work, somehow. Also, I can't modify the libraries at all in the build path e.g. set the docs/source.
IntelliJ seems better, however, there are some major bugs:
When the drop down menu appears that lists all the function, some of them are sometimes just not shown! I think it's because of scala-library / scala-compiler, it mixes them up or something...
The docs cannot be fetched, I get this message over and over again (here the functions are listed, strange enough...):
This is the library window:
The dependencies and the compiler are set right, I strongly suppose.
Does anyone know what the problem is? And how to fix it? Or is it a known bug?
Is there an IDE that can handle Scala?
Regards!
Typically, searching for names (values/classes/functions/etc.) for auto-complete can be quite slow compared to Java in IntelliJ simply because the automatically imported namespace can be quite large, and it also searches through all implicit conversions. However, I've never seen it refuse to show functions like your screenshot, what version of IntelliJ and Scala plugin are you using?
I've never gotten the pop-up docs to work via attaching JavaDocs (I haven't tried remote fetching), so I'd recommend just ctrl+click to go through to the source to read the documentation there. (However, the function/type definition does popup correctly when sources are attached). I'd suggest posting an issue at http://youtrack.jetbrains.com/issues/SCL if you have the opportunity.
To your final question, the answer is unfortunately no. The tooling for Scala is nowhere near that of Java, but both Scala IDE for Eclipse and IntelliJ are making great progress in changing that, and both are strong in some areas while weak in others. In the end, you'll get limited support either way, so choose whichever fits you best.
I have found the solution to the problem:
The bug was caused by Java Decompiler Plugin. It tried to decompile the binaries of Scala's class-files, which obviously failed. This led to that error message and further led to the Scala plugin not working as it should, thus not displaying some of the functions.
This took me a long time to figure out, I installed 2 other versions of IntelliJ (which apparently imported the JD plugin), NetBeans and downloaded several builds of the Scala Plugin, only to discover this cause. Deactivating Java Decompiler solved it.
Regards.

How to become more productive using Scala? (Tools, IDEs)

What Tools do you people use to work with Scala? For my learning phase, I used the Scala REPL and hacked some code with TextMate and compiled it with the scalac CLI. But as the projects grow in size, much more sophisticated tools are required.
I am aware of the Scala plugins for Elipse, IntelliJ and Netbeans and I tried them all. The best one is IMHO IntelliJ, but still far away from being perfect.
The major issue I have is the lack of auto completion. As a not-so-advanced Scala coder, I still dont know the whole standard API and have to switch between the Scaladoc and IDE regularly. This feels like "killing productivity". But they all fail to auto-complete method arguments. (I heard that method arguments are not included in compiled scala code, but what about attaching source to do auto completion?)
Another very annoying issue is the build process. I am using Maven to build my Scala projects and manage their dependencies. But nevertheless, I have to do a full rebuild to test my changes. Maybe I am spoiled by Eclipses incremental rebuild available in the Java world, but it feels like a big issue to me.
I like Scala very much and I feel way more productive while coding, but the lack of sophisticed tools let me feel less productive. And both seem to cancel out themselves.
So, whats my question? I doubt every single Scala programmer uses good ol' vim or emacs along with scalac to do their work. So what tools do you use? What workflows have you developed to bring speed into developing with the Scala language?
Edit
Clarification what I ment with auto-completion of method arguments.
val myList = "foo" :: "all your base" :: Nil
myList.partition(_.length > 3)
For the code above, IntelliJ fail to provide me with the information that partition requires that I have to pass a () => Boolean function. In fact, IntelliJ does not check for this contraint. I can pass a String and IntelliJ will not indicate my error until I do a compile.
scalac
Get familiar with command line options to Scalac.
-deprecation
-Xprint:all: watch your code progress through compiler phases, very useful to see what implicits are applied.
-help / -X' /-Y` list all options.
The latest nightly builds of scalac include a bash completion file that makes these easier to use.
IntelliJ IDEA
Method completion with Javadoc (CTRL-Space, CTRL-Q/Apple-J) Screenshot
Parameter Info for the example in the question (CTRL-P) Screenshot
Method Argument Completion (CTRL-SHIFT-Space). Screenshot
You need to have the source or javadocs linked into the dependencies in IntelliJ to see the Javadoc.
It doesn't currently highlight type errors on the fly, as there are still too many false-positives in complex code. This is coming, though.
Simple Build Tool
SBT keeps the compiler resident, and analyzes dependencies between classes to allow incremental recompilation. It can also monitor for changes to source files and automatically trigger recompilation and/or test execution.
Continous Compilation: >~compile
Continuous Compilation + Test: ~test-quick
I have SBT and IntelliJ project configured in http://github.com/scalaz/scalaz, you could use this as a reference.
I've been using Scala daily for the last six months. I'm still using vim (and ctags to find stuff), and Maven for builds. I've gotten some good mileage out of JRebel when working on Lift web apps -- it will reload changes on the fly without server restarts.
I spent some time looking into IDEs, but it got depressing really fast. I really missed a lot of Eclipse features at first, but after a period of adjustment I don't think I'm significantly less productive now.
I've heard some rumblings that NetBeans is the current champ for Scala IDEs, but I haven't tried it first hand.
One simple way is to use fsc, an offline compiler. It maintains caches of information, and the standard compiler will talk to fsc (running as a daemon) and use its cached information during compilation, thus speeding up your compilation cycle.
Here's my answer on a similar thread. After giving about an hour to Ensime. I just can't help but get the word out. I must say it's very, very well written for an Emacs package.
I am afraid you have to wait for Scala to become rock-solid.
I had exactly the same issue with Java ten years ago. It was even worse.
I also tried them all. For Scala 2.7.7 IntelliJ is the winner, but for Scala 2.8.0-SNAPSHOT Eclipse is not that bad.
Wait half of a year after 2.8.0 is released and check again. It should become bearable.
With Scala 2.8, please see this for getting better performance out of Maven.
You should also give Netbeans 6.8 a try with the nightly Scala build. I am very satisfied programming Scala with this IDE. For building, I sometimes also use Ant. The best thing about NB Scala plugin is that it is fast and code-completion works flawlessly.
For the example you gave: NB gives me this error
code-completion:
The Eclipse Scala IDE is quite mature now (as of 3.0).