How to fully clean, re-resolve and rebuild a Scala sbt-managed project in IDEA? - scala

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.

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

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.

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

Another heavy hammer but seems to work.
Close IntelliJ
Delete .idea folder
Import app from existing sources

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.

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

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.

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.

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.

What worked for me was to:
1) Close IntelliJ
2) delete .idea/libraries internals
3) Start IntelliJ
4) right-clicked pom.xml Maven > Reimport
After that there were no old versions and duplicate libraries in the External Libraries.

Related

IDEA says a dependency is missing, SBT disagrees

my issue seems similar to Getting IntelliJ IDEA understand SBT dependencies
except I'm using sbt 1.4.4 ,and the plugin suggested in that question no longer appears to work (it requires sbt version 0.13.x, and last commit to that plugin was 7 years ago)
is there anything similar that might work with more recent versions of sbt, or some other way to get intelliJ to work?
It only seems to affect 2 dependencies, all other dependencies works fine, thing is, i really need those 2 dependencies.
I'm on Linux, should it matter, a colleague opened the same code on Mac, and had no issues, however I've tried clearing every cache i could think of, reinstalled IDEA, rebooted twice, changed idea settings to use sbt shell for everything, nothing and everything in between. I've tried to rebuild with sbt version override enabled and/or disabled.
I dont get any errors from intelliJ rebuilding the project, but the editor still insists that some dependencies does not exist!
I'm out of ideas, how can I fix IDEA?
First identify the path to problematic dependencies by executing
sbt 'show dependencyClasspath'
which prints out something like
Attributed(/home/picard/.cache/coursier/v1/https/repo1.maven.org/maven2/org/typelevel/mouse_2.13/1.0.0/mouse_2.13-1.0.0.jar)
Next in the following order
sbt clean the project
invalidate caches
Exit IntelliJ
nuke your project's .idea directory.
Now say mouse was the problematic dependency, then remove it with
rm -fr /home/picard/.cache/coursier/v1/https/repo1.maven.org/maven2/org/typelevel/mouse_2.13
Finally reimport the sbt project which, if you have the shortcut setup, from project's root directory
idea .
and let sbt refetch the missing dependencies and let IntelliJ reindex them.

Fixing my Scala Intellij project when loading it from an existing project in sbt and dependencies download fails

Oftentimes I have troubles with sbt downloading its dependencies when behind a corporate firewall when trying to "Load a project from existing sources" -> sbt. It eventually times out and leaves my project in the following state:
there is some problem when
I can at this point download all my dependencies through the sbt-terminal tool (or the command-line) but Intellij will still keep this "view", instead of correctly showing me the diverse folders the project contains (like src/, test/, etc).
Is there a way to fix this?
Thanks
I also have had this problem multiple times. I usually close IntelliJ, clean its repository rm -rf .idea/, and then open the project folder (instead of importing it). It's always done the job for me.

Why does IDEA report errors for build.sbt in a new sbt project?

I'm running IntelliJ 13.1.5 community edition. I've got JDK, Scala, SBT installed. The JAVA_HOME, JDK_HOME, SCALA_HOME and SBT_HOME variables are set. If I create a new SBT project, after initial sbt refresh, I get this:
The thing compiles and runs fine, even if I add dependencies, but the file itself shows tonnes of errors. F4 settings show empty SBT module stuff:
I then installed https://github.com/mpeltonen/sbt-idea and ran sbt gen-idea in the project folder. This created a bunch of files, and reloading the project, the reds in the sbt file went away.
This is on my laptop. However, on my work machine, simply creating a new project works fine, and the sbt file isn't filled with red. Can't seem to figure out how that machine's different. Any pointers on how to resolve this annoying issue?
In case IntelliJ IDEA struggles a bit with caching, try the first and if still not working the second should definitely do the trick:
File -> Invalidate cache and restart
Close IDEA, delete .idea, re-import your project
I was also getting highlighted errors in all of the build.sbt file.
What fixed it was to select to Download "Sources for SBT and plugins" in the Import Project from SBT window.
Please, check if you added the “scala-library*.jar” to Scala compiler library
This link might be helpful:
http://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/
If you are using windows or linux, please also see:
http://confluence.jetbrains.com/display/SCA/Setting+up+Scala+plugin+project+in+IntelliJ+IDEA
Update on 2020, Apr
This works for scala 2.13 and Intellij 2020.1 when:
sbt files has error
project structure is wrong
Reason is 'External libraries' from sbt is not loading for IDE.
To do so, similar to old version: https://www.lagomframework.com/documentation/1.6.x/java/IntellijSbtJava.html
1. rm -rf .ida
2. Click Menu item: Open... to pick the project directory
If not working, I assume you need to setup Global SDK for intellij by
cmd+; and pick the path of scala.
In case of mac,
brew install scala normally echo out the path /usr/local/opt/scala/idea
Let ide browse to this path by holding cmd+shift+G to open path.
IDE should find the path like /usr/local/Cellar/scala/2.13.1/idea/lib/scala-library.jar and scala-compiler.jar and scala-reflect.jar
There might be better way to do the global scala setting though.

IntelliJ IDEA 13: new Scala SBT project hasn't src directory structure generated

I followed the getting start video on Jetbrains website to setup IntelliJ IDEA 13.1 Community Edition to work with Scala. Scala plugin v0.36.431 had been installed. While I created a new Scala SBT project with wizard, there was no src/ directory structure generated in the project. Only two sbt files were generated:
scala-course/
├── build.sbt
└── project
└── plugins.sbt
From the video and other document I know that there should be a src/ directory structure, including src/main/scala, src/test/scala, etc. sbt uses the same directory structure as Maven for source files by default.
I can create those folders manually and mark it as source root. However it is trivial. So my question is: Why IntelliJ IDEA new project wizard doesn't generate the directory structure as said in document? Was I doing something wrong? I checked the preferences and couldn't find anything that seems related.
Normally it should create these folders automatically. It may take a while though - it takes couple of seconds in my case.
When creating project make sure you have selected Scala -> SBT, then proceed with the wizard.
Once the Finish is clicked, the project will be loaded. This part takes couple of seconds, and I can see no src/main/scala nor src/test/scala generated until it's done. Observe the bottom of the screen to see when it's done.
Once the process is finished, you'll see the folders.
If that's not the case, check the settings. You should have the Create directories for empty content roots automatically checked. You may want to check Use auto-import to automatically propagate changes in the build.sbt.
After changing the settings (if the change is required) you may need to refresh the project, as seen in picture below.
This can also happen if you do not have a JDK selected. For some reason you no longer get the option to select an SDK so you must make sure you have configured this before hand. To fix this do the following:
From the welcome screen, go to
Configure -> Project defaults -> Project structure and add the jdk.
Source:
What's the reason for "Error:Cannot determine Java VM executable in selected JDK"?
Thanks to lpiepiora, with his hint I find out the reason.
Because my sbt is newly installed, there is nothing in ~/.ivy2/cache/ and ~/.sbt/boot/. sbt needed to download required dependencies from repositories on network. It happened that my proxy to internet had something wrong, download stuck.
And also need to notice that, if quit IntelliJ IDEA when sbt is running in background, the next time you'll get error of waiting for some lock file. Have to remove the lock file on filesystem and restart IntelliJ IDEA again.
After fixed the network problem, everything work as promised. It requires several minutes, depends on network speed, to download required jar files. After finished, the src/ directory structure is created.
I followed the instructions in this thread but I had a java crash in the final phase in sbt with the configuration bellow and I think this info maybe useful:
The problem happened with IDEA 2016.2, sbt 0.13.8 (I tried later to import using 0.13.12 but the crash was the same), scala 2.11.8 and ubuntu 16.04.
The only way I could make it work was to use java 8 instead of 9.
error: error while loading package, Missing dependency 'object java.lang.Object in compiler mirror', required by /home/jbamaral/.sbt/boot at xsbt.boot.Boot.main(Boot.scala)
...
stack log here
...
[error] scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.

IntelliJ IDEA Scala plugin's syntax highlighting displays Scala packages in red

This is a bit weird, I've got IntelliJ IDEA 10, a fairly recent Scala plugin on it and sbt 0.7.7. I've been using the same setup on the same project for a while and syntax highlighting worked perfect.
After a few clean/compiles from sbt (I'm not really sure about the action), everything from Scala._ is red. For instance, List() is red but when I import scala.collection._ it gets fixed. The project compiles without any problems in both IDEA and SBT. What may be the problem?
(new/other projects are OK on the same setup so I guess I've messed up something but I have no clue where to look for it)
Thanks
I've never found a single solution to this, so i generally try all the things.
sbt gen-idea (to recreate IDEA project meta-data)
File > Invalidate Caches ... > Invalidate and Restart
After invalidating caches, upon restart IDEA will re-index all of your class files.
Usually I'll try
sbt> clean
sbt> gen-idea
Then if it didn't work go for invalidating caches. If that still doesn't work (and it almost always does) try deleting the following directories and then perform #1/#2 from above again:
target
project/target
.idea
.idea_modules
at project Settings, add the jars to its dependencies of IDE, then you will get the red away.
SBT is one thing, IDEA is another, they use their own way to compile scala. some sbt plugin for idea is also something that just hook sbt command line to idea's panel.
This sometimes happens to me when the SBT and IDEA projects get out of sync. Simply run the SBT Idea processor again - this regenerates your Idea project files.