Scala, Cats, Intellij IDEA: fix syntax & compile errors? - scala

Using the Cats library for Scala in IntelliJ IDEA leads to errors even if terminal SBT works. Can we fix this? Or some workaround that isn't switching editor?

Cats seems to work fine on my IntelliJ 2018.1.4. If it is working in the terminal but not in IntelliJ, it could be because of using in-built IntelliJ compiler. Have you tried enabling Use sbt shell for build and import, which delegates building to SBT:
Open sbt projects tool window: View | Tool Windows | sbt
Click sbt settings icon
Select Use sbt shell for build and import (required sbt 0.13.5+)
Use sbt shell might resolve compilation issues, however note that in-editor error highlighting might still give false positives because it uses custom-built typechecker.
Metals is an alternative that uses Scala proper for both compilation and in-editor error diagnostics.

Related

Run Scala Dotty project using Intellij IDE

I created a basic Scala Dotty project using Dotty template and import the project to IntelliJ IDE.
Everything works fine when I use the sbt command line.
When I try to build or run it inside IntelliJ IDE, I got following errors:
Error:scalac: Multiple 'scala-library*.jar' files (scala-library-0.9.0-RC1.jar, scala-library-2.12.6.jar) in Scala compiler classpath in Scala SDK sbt: ch.epfl.lamp:dotty-library_0.9:0.9.0-RC1:jar
Any ideas how to solve this?
IDE support for Dotty
Currently, the only IDE we officially support is Visual Studio Code.
Anyway when you import a project to IntelliJ IDEA check "use sbt shell". At least for me after that a test project compiles and runs with Ctrl+Shift+F10.
It's possible that not everything will work. For example Dotty macros don't but if I compile and run manually then they do.

IntelliJ: what difference between "SBT Console" and "SBT Shell"?

In the IntelliJ I see two tool windows: SBT Console and SBT Shell. What is the difference between these tools?
You may have installed both scala and sbt plugins. The newer version of scala plugin has sbt as part of it.
sbt console is the sbt plugin -- click start you will enter into sbt interactive mode.
sbt shell is part of the scala plugin, which is similar to the above mentioned sbt console and can autocomplete commands.
Here is a description: https://blog.jetbrains.com/scala/2017/03/23/scala-plugin-for-intellij-idea-2017-1-cleaner-ui-sbt-shell-repl-worksheet-akka-support-and-more/
SBT Console plugin is deprecated in favour of IntelliJ Scala Plugin. Deprecation notice states:
JetBrains have recently added support for an SBT console to the
IntellIJ Scala Plugin. This is now the recommended way to use SBT
within IntellIJ. No new releases of this plugin are planned.

How to work with Intellij Idea and Scala

I work on an SBT multi-module scala project.
I use Idea to edit code and an external terminal with sbt ~compile for compiling.
I'm not very happy with this workflow.
Is there a better way ?
There are options to configure at some places, the doc is not very clear, so how to configure that :
in Build > Compiler > Scala compiler
in Languages > Scala
in Languages > Scala Compile Server
Do you configure some options on the scala compiler in idea ?
When and how do you compile (e.g. do you use make automatically in compiler settings) ?
Do you use idea or SBT for incrementality type
You can import multi-project SBT projects into scala via
File -> Import Project...
SBT project
When importing, make sure you select Use auto-import. This will keep your IntelliJ environment in sync as you change your SBT build files.
Under 13, at least, you may have to go into
File -> Project Structure
and delete some extraneous projects it ends up creating (it seems to create a project and project-build for me).
Once set up, building in IntelliJ will build via SBT, i.e. it's the same behavior as building via sbt compile.

Intellij unable to resolve references to a specific jar file

In a given project that is driven by sbt there is some kind of corruption in the project libraries specifically for a MavenLocal repository used for kafka-spark-9.7.2.jar in which:
references to the classes provided by that jar are marked as "symbol not found" by the editor parser
however the editor (strangely) does offer to import the classes
but after accepting the import, the symbols are still marked in red as unresolved.
The following attempts to "clean things up" have already been performed:
Build | Make Project
Build | Rebuild Project
In addition I have verified that the project does build from
sbt package
on the command line
UPDATE After re-running sbt gen-idea the librraries are still not found by the Parser. yet the libraries exist -even IJ knows about them as shown in the following screenshot. Why is it that IJ can find the library
C:\Users\S80035683\.ivy2\cache\org.apache.kafka\kafka\jars\kafka-0.7.2-spark.jar!\kafka\api\FetchRequest.class
However IJ is unable to resolve any classes from that library in the Parser?
You have to build the project for Intellij, try to do this in your project root:
sbt idea with-sources=yes
This should build the project structure from scratch and add the right dependencies, usually I refresh the project after adding a dependency or a jar.
Edit:
To use the command you need this plugin, otherwise you can use gen-idea but I used it only a few times and I'm not sure how it will work out.
Edit2:
There was some confusion, first, for the IDEA SBT console you don't need to prepend the sbt command since you already are inside sbt:
If you have the sbt plugin for idea you can use gen-idea with-source=yes (without prepending sbt)
From the terminal, either you go to your project root and type sbt to enter the sbt console and use gen-idea or idea with-sources=yes (without prepending sbt)
or directly sbt gen-idea or if you have the plugin sbt idea with-sources=yes (prepending sbt)
To reach the sbt console inside idea you need to install the sbt plugin on preferences -> plugin and search for sbt and then View -> Tool Windows -> SBT Console:
To start the console click on the play button, to kill the console on the skull.
I had the same problem. I fixed it by directly writing the CLASSES and SOURCES of the problematic library. This can be found in .idea/libraries/SBT__<problematic library>_jar.xml

Scala Compiler doesn't compile in ScalaIDE

I'm trying to develop on the Scala compiler project with the help of ScalaIDE. I followed this guide to set up the development environment. When I now try to build the mentioned projects, the reflect project won't get built. Instead, I get the following error via the console output:
uncaught exception during compilation: scala.reflect.internal.MissingRequirementError reflect Unknown Scala Problem
Having tried the provided ANT script of the project via the console, everything seems to work fine.
Does anyone know if I'm missing a hidden compiler flag, dependency or something like this?
Thanks!
With the IDE for Scala 2.10 you can only build the 2.10.x branch of the Scala compiler. If you want to work on master, you need to install a 2.11-based version of the IDE. We don't publicise IDE for 2.11 nightlies yet, but they are available at:
http://download.scala-ide.org/nightly/scala-ide-master-2.11.0-SNAPSHOT/