IntelliJ IDEA: Cannot import SBT project - scala

I'm completely new to development using Play or IntelliJ for that matter. I've created a simple HelloWorld application using Activator, and this is an sbt project.
I've been trying to import this to IntelliJ and this is the screen I'm stuck at:
https://www.dropbox.com/s/we1a4a3184sojvb/Screenshot%202014-07-24%2016.57.11.png
In almost all tutorials I've been through online, I've seen people using an sbt option on the import screen. I've installed the SBT plugin as well, but that hasn't helped. I've restarted IntelliJ several times to no avail.
Where am I going wrong?
I'm running 13.1.4 with the SBT plugin installed.

I had this error when importing a new module from existing sources.
On the right-hand side of your IntelliJ window, you will see a list of vertical tabs, open the SBT tab.
Select the module that's causing trouble, right click on it, select "Detach external project"
Try to import the module again.

Move to /your-project-folder
Run ./activator
Type idea . It will generate IDEA project.
Open (not import) File->Open.. project in IDEA.

I had a similar problem which came from a different place than the other solutions here so adding it in case it occurs for others.
In my case the problem was caused by accidentally opening the IdeaProjects directory. When you do this, it creates the .Idea folder in that directory, and populates it as a project. This apparently also confuses the set import.
The solution is to remove the .Idea folder and open (not import) the directory again.

I encountered the same error when I was trying to import a new scala project A to an existing IntelliJ project B as a module. I solved the problem by following step below:
Go to your folder of project B. Open file .idea/sbt.xml.
Deleting the entry which includes the path of project A.

After a series of struggles, I must say something weird solved this problem – moving to IntelliJ IDEA Ultimate. That has built in Play application support and can run/debug/test the application out of the box, which is very handy.
All I can say after this experience was that JetBrains wants you to upgrade to Ultimate if you want things to work right out of the box.
I'm sure there are ways to get this working with CE also, but I wasn't successful with that.

Solved for
IntelliJ IDEA 2022.2.1 (Ultimate Edition)
scala plugin: 2022.2.12
sbt version: 1.7.1
doing these steps:
Unlink sbt Project(sbt toolbar), then link it again.
Delete modules (ctrl + alt + shift + s).
Remove .bsp/, and .idea/ config files.
Invalidate caches..(ctrl + f).
Restart IDEA.
The number of steps to make it work may vary.

Uninstall everything
Reinstall IntelliJ
Install the JetBrains official Scala plugin
Import the project
Make sure that the project has a build.sbt file in the right place, so that IntelliJ can find it as an SBT project.
This is about IntelliJ IDEA version 13.1.4.

This issue is solved in the latest updates of the SBT and Scala plugins.

Make sure that your java path is correct (IntelliJ can use other path than system defined)
Preferences -> Build, Execution, Deployment -> sbt -> JVM - choose a
correct JRE

Related

How to open SBT Tool Window in Intellij?

This question may sound silly for you, but I have been searching the Internet for a while and couldn't find the answer. How can I open SBT Tool Window in Intellij (scala and SBT plugins obviously installed)?
In Menu: View -> Tool Windows there should be two entries:
SBT
SBT Console
The first you can use to refresh IDEA project structure from SBT and view dependencies.
The second gives you a console where you can run sbt commands.
Update:
As Justin already aded:
As of IntelliJ IDEA 2017.1 Build #IC-171.3780.52 there is a new and (imho) better way:
(If you are working with an SBT project, that is)
When you open the SBT Tool Window, you will find a new scala terminal icon:
This will open an sbt console session with your sbt project import settings.
One major benefit is that you will actually (finally) get links to your code lines for compiler warnings and errors.
To be able to open SBT Console from View -> Tool Windows you first need to install the plugin required for it.
You can find the SBT plugin by searching for it under Settings -> Plugins -> Browse repositories
Plugin homepage: https://github.com/orfjackal/idea-sbt-plugin
In IntelliJ 2017.1, an sbt shell is integrated with the Scala plugin. You can open it from an icon on the sbt project view toolwindow, or from the "Tools" menu
In Intellij 16 SBT Console menu item no longer exists and in Debian 7 (XFCE) Alt-F12 doesn't work.
To run SBT console:
select View -> Tool Windows -> Terminal (for Alt-F12)
type sbt and console works
You can either use above or simply use Alt+F12
I had an issue where the sbt shell launcher was not on the bottom of the IDE and the sbt was not listed as an option under Tools. Discovered that the issue was after expanding a zip file of a scala project to my workspace, the build.sbt file was not in the root folder of the project structure, but nested one subfolder down. When importing this project into intellij it was not properly configuring it as an sbt project. After removing the project, exploding the zip in such a way that the build.sbt was in the root of the project and reimporting the issue was resolved.
I had a similar, but slightly different issue. First, I didn't have the Scala plugin installed on my version of IntelliJ. Once done, I restarted the app. Then created a new project (even though I was trying to open an existing one). When creating the project I made sure to put the root directory of the project as the root folder of the project I wanted to import (the one that has the sbt file). IntelliJ synched my new project with the existing one and I was good to go.

IntelliJ source code editor shows false compilation errors

I have a problem with the following odd behaviour of the IntelliJ IDEA development environment (version 14) with scala:
After I reset the (global) scala SDK from a 2.11 version to version 2.10.4 the source code editor shows many compilation errors: list, map, print, (1 to n) etc. are all red.
The SDK is added to global libraries and the jars are all found.
However the code compiles and runs correctly (I rebuilt the project to force recompilation). Has anyone ever experienced something like this?
How can this be fixed?
I am using tha java sdk 1.7.0_55.
In file menu, Invalidate Caches / Restart
also set Incremental Type to SBT in Settings -> Build, Execution, Deployment-> Compiler -> Scala Compiler
There are high chances that you didn't load your project properly, and that's why the errors are appearing. I have written a complete blog post about how you can fix it. And following are the two methods from the write up which can correct your issue.
These two steps should be able to help you, for rest of them please check the link.
1. Importing Project as an SBT Project:
First of all, close your project and import it again after selecting the directory and choose SBT as an External Module. Select Import Project, then go to the project directory, select your project, choose Import Project from external model, opt for SBT and click Next and then Finish. IntelliJ IDEA will ask you to overwrite the .idea file, say Yes to it. After IDEA is done indexing and loading your project, all the issues will be sorted out. The editor will work fine, and you will have your missing features restored.
2. Fixing missing project files with SBT-IDEA:
Sometimes IntelliJ IDEA misses out on the most important project files and shows editor errors along with a broken code completion, however, during the code compilation, everything works as per your expectation. This solution works with a plugin called as SBT-IDEA. Now to enable this plugin, open your terminal and follow the instruction:
a. Create a file plugins.sbt in the directory ~/.sbt/0.13/plugins
b. Now, write the following content to it:
resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.7.0-SNAPSHOT")
Note: Please check for the latest version at the plugin’s GitHub page.
Save and quit, and execute sudo sbt gen-idea in your project root directory.
I hope it helps!
Thank you.
In my case I changed build.sbt file (added empty line and saved file). Then I Idea re-checked dependencies and indexed. Then everything worked fine.
To remove all the false errors sometimes you need to disable type-aware highlighting
by pressing Ctrl + alt + shift + e for example:
https://blog.jetbrains.com/scala/2011/03/02/type-aware-highlighting/
The errors seem to be caused because Scala plugin re-implements compiler logic and that is imperfect.
More information here in the comments:
https://www.reddit.com/r/scala/comments/5pcwfv/question_what_causes_intellij_idea_highlighter/
This occurs when IntelliJ tries to build your code by itself and fails miserably.
If you know your build is successful, all you need to do is edit the Run Configuration and remove the Build part inside the Before Launch part.
This solution solved it for me!
Try invalidating your caches and restarting IDEA.
(File > Invalidate Cache)
Wait for IDEA to re-index your project (Status indicator on lower right status bar). The caches will sometimes become corrupted. This is most likely to occur upon reorganizing a project as you mentioned you did.

sbt build failed in Intellij but I want to open the project anyways

I am trying to import an existing sbt project. As part of the import process IJ attempts to build it. The sbt build fails - and it is due to proxy issues not the culprit of IJ. I want to open the project with existing sources anyways..but am unable to figure out how to do so . When hitting "Finish" in the "import Project" dialog, IJ will do the build.Is there a "skip build" option somewhere?
As a workaround, you may try to generate intellij files from sbt using
sbt gen-idea
or
sbt idea
(the exact command depends on the sbt configuration)
It's always worked for me. I'm using the cmomunity edition.
I have found that the most reliable way to handle sbt projects in IJ is to actually open the file directly from the Finder (mac) / Explorer (windoze) / nautilus (linux/gnome).

Attaching sources in IntelliJ IDEA for scala project

I have Playframework 2 project with Scala (very small one). It uses Scala Anorm library.
I have the code like this:
package models
..
import anorm.SqlParser._
...
val rowParser = scalar[Long]
So, I would like to see source of scalar method. Trying to attache the sources I have in my plaframework source folder, but IDEA just swallows my request and does nothing back.
I use last version of IDEA and Scala plugin. Is it bug, probably?
First download source code :
1.) Click on this link : http://www.scala-lang.org/download/all.html
2.) Choose any scala version.
3.) Now in the last section 'Other Resources' you can see 'Sources' link. Click on it to download. In my case (https://codeload.github.com/scala/scala/tar.gz/v2.11.7)
Now point to this Source Code from IntelliJ.
1.) Open Project Structure in IntelliJ. Shorcut (Cmd + DownArrow)
2.) Select Global Libraries from the left section.
3.) Then on right side, Under 'Scala Library' section. Click '+' and point to source directory. See the screenshot.
Aha.. fixed.
So: I used to use 'attach source' in the top of the window editor - it does not work (I guess it is a bug - maybe related to the scala plugin, because usually it works).
But if try to attache sources to particular library - in "Project Structure -> Library -> +Attach File or Directories -> Sources" then it works.
Thanks.
To fix it we need to do some changes into IntelliJ IDEA platform. Most probably it will be fixed in IntelliJ IDEA 13.1.
I have the same problem. I installed "Scala Imports Organizer", and problem was solved.
for "automatically attaching source jars" when using Bloop and BSP you can add one line:
bloopExportJarClassifiers in Global := Some(Set("sources"))
to your build.sbt, also sbt updateClassifiers, also Reload all BSP Projects to refresh, also Invalidate Caches/Restart to trigger indexing (if not done automatically)
reference

How to have Eclipse recognize dependencies from SBT

I am trying to figure out how to make Eclipse recognize dependencies that are retrieved using SBT? SBT download the correct dependencies and puts them in my ~/.ivy directory but eclipse doesn't see them. Is there a way to do this?
thanks
If you are using sbteclipse plugin it's achievable in a simple way. In sbt type:
reload
eclipse with-source=true
Then in eclipse, hit F5 on a project folder to refresh it. Or right-click and choose "Refresh". Just works.
This is probably not the answer you are looking for and I admit it is not elegant but it currently works for me, meaning that I think it takes less time for me to periodically do the following instead of researching and finding a more elegant solution.
I assume you are using the sbt-eclipse plugin ( https://github.com/typesafehub/sbteclipse ). When I add new dependencies to my project ( which is actually pretty rare ) I simply regenerate my eclipse project files from the plugin. The downside of this is that I have a multiple module project and after I refresh the projects in eclipse I need to re-add the inter-project dependencies in the eclipse build path editor.
Like I mentioned it is pretty hacky but all in all I really don't loose that much time doing it. It's not pretty but it works.
Best of luck,
Andy
I use the Apache IvyDE plugin for Eclipse, and I've had more luck with this approach. It's only described in the old sbt docs, but works with sbt 0.11
First, install the IvyDE plugin in Eclipse and restart.
Run the sbt command deliver-local - this will create an XML ivy file of your dependencies.
In Eclipse, under your Project/Properties - Java Build Path - Libraries, click "Add Library" and choose "IvyDE Managed Dependencies" then select the file target/scala-2.9.1/ivy-projectversion.xml
When you add a new dependency to build.sbt, run the sbt commands reload and deliver-local again. Then right-click the Ivy library for your project in the Package Explorer - it will be called "target/scala-2.9.1/ivy-projectversion.xml [compile,test]", and click the second "Refresh" menu item (between "Refresh" and "Reload Settings" - not "F5 Refresh" ).
In command prompt go to the project folder and type
sbt eclipse
This should generate the appropriate .classpath entries in eclipse project.
Go back to eclipse, select the project and press f5, this will reload the referenced libs.
All you need is execute from your project home:
sbt "eclipse with-source=true"
or enter sbt console and write:
eclipse with-source=true
BTW: I don't know if from Jan '12 something has changed but now it seems much more simple.
The following works for me:
1) close project in Eclipse
2) in my file explorer, browse to my Eclipse project, make sure hidden files are visible
3) open .classpath in a simple text editor
4) copy the bottom entry. For example, in my current project, it is <classpathentry path="/home/natalie/.ivy2/cache/org.scalatest/scalatest_2.9.1/jars/scalatest_2.9.1-1.8.jar" kind="lib"></classpathentry>
5) navigate to my .ivy folder, cache, then down to the library I have added via sbt
6) right click on jar file, select properties, copy the path and jar file name and replace it in the entry I copied in step 4
7) save .classpath
8) open my project on Eclipse
New dependency is now available in Eclipse.