How to fully clean, re-resolve and rebuild a Scala sbt-managed project in IDEA?
ScalaIntellij IdeaSbtScala Problem Overview
TL;DR: How can I fully reconcile all dependencies in IntelliJ when SBT file changes
I have a SBT project setup in IntelliJ. For the most part if works like a charm, but it is quite a pain for me, to change the version of the dependencies and compilers.
What I hope to do, is to update my SBT file, and click something like clean project in IntelliJ, so it gets up to date.
The best menu item I have found is Rebuild project. It reads new dependencies, but keeps the old ones around, so External Libraries contains multiple versions of the Scala runtime and 3rd party libraries.
To get around this I can close IntelliJ and delete all files in .idea/libraries
. When I restart IntelliJ it will re-resolve the dependencies. It works, but manually deleting metadata files indicates that I am doing the wrong thing.
Scala Solutions
Solution 1 - Scala
To refresh IntelliJ after changes made in *.sbt files:
- Open the SBT Project pane (Menu View / Tool WIndows / SBT)
- Right click on your SBT project
- Select Refresh external project
Solution 2 - Scala
When you save the SBT file, IntelliJ IDEA normally refreshes the project. But if it doesn't, you can do it manually.
There is a Refresh All SBT Projects button in the SBT Panel:
On the occasion that IDEA is still confused, not finding packages you just added:
From the File menu:
It's a pretty big hammer, requiring a restart of the app, and multi-minute rebuild of the IntelliSense index. But it's occasionally the only thing that will get IDEA to recognize your new SBT packages.
Solution 3 - Scala
Another heavy hammer but seems to work.
- Close IntelliJ
- Delete
.idea
folder - Import app from existing sources
Solution 4 - Scala
Has happened to me before, and yes, is a PITA.
What I usually do is to manually remove the dependencies from the project, all of them, then let sbt re-import what is actually needed. Don't do it on the UI though, since even having multiple selection, will ask for confirmation for EVERY dependency! (you will be clicking "Yes, I'm sure" for an hour), instead, go and delete directly from the .iml
project file on your .idea/modules
directory.
Solution 5 - Scala
try following steps:
- close IntelliJ
- backup whole project folder
- delete .idea folder
- delete target folder
- delete project/target folder
- reopen IntelliJ and import as a sbt project
Note this is only for SBT projects
Solution 6 - Scala
I was using different versions of sbt and scala across different proejcts and IntelJi got confused somehow. I am only able to resolve the issue by removing the sbt
and ivy2
cache folders:
rm -rf ~/.ivy2
rm -rf ~/.sbt
Solution 7 - Scala
Agree, that's a PITA. Nothing helped me (I tried "Invalidate/Restart", removing files from ".idea/libraries" and restarting).
The only way to overcome the problem for me was to manually remove project-related folders from the IntelliJIdea cache. I removed these folders from the following places (I use OSX and IntelliJ-Idea 14):
- /Users/{UserName}/Library/Caches/IntelliJIdea14/compiler/{ProjectName}
- /Users/{UserName}/Library/Caches/IntelliJIdea14/compile-server/{ProjectName}
This helped me, hope it will save time for someone.
Solution 8 - Scala
IDEA 2016.2.4
None of these solutions worked for me.
**** BEFORE YOU TRY THE ANY OF ABOVE. MAKE A BACKUP OF THE SBT / IDEA PROJECT IN QUESTION *****
If I updated the 'build.sbt' then the external library dependencies on the project view did not update. Moreover, the Project Structure still showed the old dependencies. No matter what I tried IDEA would not refresh.
The only way I could get a partial restore was to copy the '.idea' folder from another machine. SBT plugin implementation is ****ed! I removed the '.idea/libraries' folder beforehand and then IDEA did not restore this folder. I tried many times importing the project again.
Very bad :(
ADDENDUM: I managed to get my project working. I think SBT with IDEA is no longer at fault entirely. It could be just possible that my Apache Ivy 2 cache might have been corrupted. So I executed the following commands:
$ rm -rf ~/.ivy2/cache/org.scalatest/scalatest_2.11/*
$ rm -rf ~/.ivy2/cache/org.scalactic/scalactic_2.11/*
Restarted IDEA with invalidate cache under the file drop down menu item. This seemed to trigger some internal action in IDEA and SBT. I also tried manually adding add JARs throught the IDE, but it got massively confused. Moral of the story is to work on two different laptops machines daily and keep them updated.
Also double check SBT from the command line works. I was using 0.13.12
$ sbt about
$ sbt clean
$ sbt test
Also recheck with the SBT dependencies plugin too.
Solution 9 - Scala
What worked for me was to:
-
Close IntelliJ
-
delete
.idea/libraries
internals -
Start IntelliJ
-
right-clicked
pom.xml Maven > Reimport
After that there were no old versions and duplicate libraries in the External Libraries.
Solution 10 - Scala
If you are using the activator through a terminal/shell/command prompt use the following command:
activator clean
Or just "clean" on the sbt shell through intellij
It should help reset things and next time you run or compile, it should go through it from scratch.