10

I'm having trouble getting IntelliJ to recognize Ammonite imports in my Scala script. This is a new feature, reported here, where there are also instructions. I followed the instructions and I started a new sbt project in IntelliJ to try it out, but it's not working.

I see the Ammonite dependencies in the project's "External Libraries", which I specified in the build.sbt: libraryDependencies += "com.lihaoyi" %% "ammonite-ops" % "1.0.3" libraryDependencies += "com.lihaoyi" % "ammonite" % "1.0.3" cross CrossVersion.full

The (quite simple) project seems to successfully build. I also specified that Scala worksheet files should be treated as "Always Ammonite".

All this, yet the IDE shows the script like: enter image description here

What more do I need to do to get this to work?

Versions:

  • Java 1.8.0
  • Scala 2.12.4
  • sbt 1.0.3
  • IntelliJ CE 2017.3.2
  • IntelliJ scala plugin 2017.3.11
einnocent
  • 3,567
  • 4
  • 32
  • 42
  • One thing I noticed is that transitive dependencies are not pulled into scope unfortunately. For example if I pull in `circe` library and have IntelliJ download that, I'm still not able to resolve import for `cats` library (which `circe` depends on) – Jacob Wang Dec 29 '17 at 09:16

4 Answers4

11

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. Staring code

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. Import $ivy dependencies

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... Create library from jar...

This makes Intellij recognize the library, imports and it can start giving you hints. enter image description here

The library is added to the Project's dependencies (Ctrl+Alt+Shift+s). You can see also dependencies coming from other files below. Project structure with added dependency

Bartosz Bierkowski
  • 2,782
  • 1
  • 19
  • 18
  • You forgot to mention changing form 0.6.0 to 0.6.7 :) That version was missing from the Maven repo. Also you need to run the script first to get `amm` to download the `pom`s before it will create the lib from jar. Seems like this is not the way the plugin is meant to work, but hey, it's working! – einnocent Dec 29 '17 at 05:51
  • Thanks @einnocent, I updated the answer with your feedback for future generations. – Bartosz Bierkowski Dec 29 '17 at 10:33
2

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:

enter image description here

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

enter image description here

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"
arberg
  • 4,148
  • 4
  • 31
  • 39
2

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:

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

  2. Running ivy -refresh from the directory with the created ivy.xml.

  3. Then we need is to "Create library from jar..." from the red import's context actions in IntelliJ IDEA.

That's all!

arz.freezy
  • 648
  • 1
  • 6
  • 12
  • This worked for me when I opened my worksheet directory alone, not within my Mill project. – Lasf May 24 '20 at 08:49
-1

you need to associate .sc files with ammonite.

in you settings, it should look like this enter image description here (solution taken from here)

I would also recommend reading the following blog post (scroll down to Ammonite scripts support) to make sure you have all needed requirements

lev
  • 3,986
  • 4
  • 33
  • 46
  • Thanks but I already did that: 'I also specified that Scala worksheet files should be treated as "Always Ammonite".' Also, in the question, I linked to the blog post you mentioned. Also, in the solution you link to, I commented there a few days ago :) – einnocent Dec 28 '17 at 20:46