New dependencies added to build.sbt are not seen by Intellij IDEA

I'm working on v2016.2.4 on a Scala project, based on SBT.

Whenever I add a new dependency to the project, IDEA fails to see it, and fails to compile the code.

For instance, if I add scalatest:

libraryDependencies ++= {
Seq(
"org.scalatest" %% "scalatest" % "3.0.0" % "test"
)
}

IDEA is still unable to compile this file:

import org.scalatest._

class MySpec extends FlatSpec {

}

indeed "scalatest" and "FlatSpec" are marked in red. This is the build output:

/project/src/test/scala/MessageCountSpec.scala
Error:(1, 12) object scalatest is not a member of package org
import org.scalatest._
Error:(3, 32) not found: type FlatSpec
class MySpec extends FlatSpec {

 

Sometimes recreating the entire project from scratch (e.g. deleting .idea folder and importing the source code) seems to work, but it's quite time consuming. I've tried to enable auto-import, and Cache Invalidate + Restart, tried "sbt clean" and deleting all the target/*.class/*.jar etc. No luck yet.

Is there any thring to tell IDEA to reindex the dependencies and set things straight?

Note: "sbt run" works just fine, it's just the IDE which won't compile

6
11 comments

Hi, thanks for the report.

Do you have any idea under which conditions the dependency resolution fails? Do you have an example project where this is the case?

0

It rather looks like specific case. Cause scalatest code with following dependency: 

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0"

is valid.

However, this one

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0" % "test"

gives the error.

2

I eventually got used to click the "SBT refresh" button whenever I change the dependencies in build.sbt. This has been working well and I'm suggesting the same to colleagues when they hit the same problem. Perhaps the IDE auto-import needs some work to automate this step.

3

One reason this might happen if the repository where the dependency is contained is not properly indexed. Refreshing the import will cause sbt to download it, and let IDEA index it in the local cache. There are other cases where the repositories aren't properly added to the IDEA project structure (I'm working on a fix).

To check what repos are available and refresh them, go into "Project Structure" Dialog / Project Settings / Modules / <your project>-build / SBT tab at the bottom.

0

Thank you for your work @Justin Kaeser. But This error keeps happening for every new dependency added. In fact for every new added dependency I need to refresh cache and project, and this might be annoying. Can anyone reproduce this or is it just me?

0

Yes, this is unfortunately still an issue we need to find a better solution for.

3

How do you "refresh cache and project?"

0

You can refresh cache by [ File -> Invalidate Caches / Restart  ] menu item.

4

Just for future reference for others with a similar problem...

I was experiencing something similar, also with scalacheck/scalatest specifically. In my case, another issue was my Project Structure. In addition to <projname> and <projname>-build in my list of entries in "Project Structure", I also had the "main" and "test" folders within my project listed separately. Removing those entries solved the issue.

2

This is fucking annoying. I've been using Eclipse and never have this issue. Recently, my company has bought the license and hence I switched to IntelliJ. I invalidate the cache and restart and the dependencies appear in the "External Libraries" in the project structure. However, it still doesn't recognize the import. Fail!

0

I was having the same problem.

The problem was with the where I was creating the .scala file.
The scope of the dependency was "test" but I was writing the .scala file in main/scala folder instead of test/scala folder

Either remove the scope from dependency or write the .scala file in test/scala folder

0

Please sign in to leave a comment.