How to get IntelliJ to recognize imports in Scala script?
I think IntelliJ IDEA won't find a dependency in a local cache if you're working mostly with Scala 2.13 and SBT 1.3+ with the Coursier library management. We need to help IntelliJ IDEA by downloading dependencies to the Ivy cache.
I solved this issue by:
Creating the
ivy.xml
somewhere with required dependencies, e.g:<ivy-module version="1.0"> <info organisation="test" module="download-deps"/> <dependencies> <dependency org="com.softwaremill.sttp.client" name="core_2.12" rev="2.1.1"/> <dependency org="com.softwaremill.sttp.client" name="okhttp-backend_2.12" rev="2.1.1"/> <dependency org="com.github.tototoshi" name="scala-csv_2.12" rev="1.3.6"/> </dependencies> </ivy-module>
Note, I specified Scala 2.12 here.
Running
ivy -refresh
from the directory with the createdivy.xml
.- Then we need is to "Create library from jar..." from the red import's context actions in IntelliJ IDEA.
That's all!
I had similar issues with IntelliJ and seem to be figthing bugs in IntelliJ.
My versions:
- IntelliJ Ultimate 2018.2.4
- IntelliJ Scala plugin 2018.2.11
- OS: Windows
- Scala: 2.12.6 (at the moment Ammonite current release 1.2.1 is not released for Scala 2.12.7, see #879 and below)
Without IntelliJ Project
If I just open the .sc file without open project then I get prompted with
- Project JDK is not defined
- No Scala SDK in module
- the notification: 'Add all Ammonite standard dependencies to the project?'
The notification quickly dissapears. If I click 'Add' button before fixing the JDK, the imports does not work, and the add button is gone. fix the jdk and scala, and then find the 'Add all Ammonite...' in the eventlog and click 'add' there, then it works.
However all changes in settings are lost next time I reopen IntelliJ.
With IntelliJ Project
- Create new project Scala sbt project, default directory based project.
- I place my sc-files in there somewhere
Now IntelliJ remembers settings for sc-files. I can even open sc files directly from explorer/total commander in IntelliJ after configuring 'open with...' to intellij, though I prefer.
Do NOT allow IntelliJ to auto-import sbt project settings via notification, it seems to ruin the library imports, and I need to recreate project to recover.
Scala Version issues
The library auto-import 'add' button will only work for Scala versions for which Ammonite is released. See Maven Central Ammonite releases. At the time of writing Ammonite 1.2.1 is not released for scala-2.12.7, though the current unstable version of ammonite is. See Ammonite bug-report #879. The add button just fails silently, which sucks, esp. for a newbie like me.
The auto-generated sbt build file looks like this
name := "Ammonite"
version := "0.1"
scalaVersion := "2.12.6"
This is how it looks in project settings after libraries have successfully loaded:
and here are the dependencies that make it compile. Note that the 'SBT : ' dependencies are those auto-generated via SBT-file (it seems), and the others are libraries added by scala plugin ammonite notifications feature
The above library list is empty when IntelliJ fails to load the ammonite libraries.
IntelliJ with SBT (because IntelliJ loses ammonite imports, and for $ivy)
IntelliJ seems to frequently for lose its ammonite imports. If I have SBT setup to give the imports, then it does not matter so much. Also IntelliJ $ivy support for ammonite only works when files are already cached locally in ivy-cache, and running Ammonite script on my windows caches files in different location, which means IntelliJ $ivy imports effectively does not work. Hence I use SBT to setup the sum of the imports I use in my scripts.
My build.sbt
scalaVersion := "2.12.7"
// https://mvnrepository.com/artifact/com.lihaoyi/ammonite
libraryDependencies += "com.lihaoyi" % ("ammonite_" + scalaVersion) % "1.2.1-16-48ce533"
// Extra example dependency
libraryDependencies += "org.jsoup" % "jsoup" % "1.11.3"
I had similar issues with IntelliJ, so let me share how I made it work.
My versions:
- IntelliJ Ultimate 2017.3.2 (so basically the same version as yours)
- IntelliJ Scala plugin 2017.3.11
- Kubuntu, so keyboard shortcuts below may have to be adjusted
Now, I started from the same code as you have, with updated version of dependency 0.6.7
.
Already now pressing the green arrow to next to 1st line runs Ammonite. The dependencies are downloaded and link is displayed. That is mandatory step for IntelliJ to recognize that new jar is available.
Also at this stage Intellij shows me a small popup with information that I can import the jar. I did not use it.
To make IntelliJ recognize the imports, I press Alt+Enter Show Intention Actions on the broken com.lihaoyi::scalatags:0.6.7
dependency and use Create library from jar...
This makes Intellij recognize the library, imports and it can start giving you hints.
The library is added to the Project's dependencies (Ctrl+Alt+Shift+s). You can see also dependencies coming from other files below.