Scala: Intellij shows compile errors on Import statements - scala

I'm trying to teach myself scala and I'm using IntelliJ IDEA 2016.1.3. The editor does not recognize import commands. Errors appear. I have downloaded libraries, but the problem persists. Am I doing something wrong?

Try this,
1.) I created a scala project:
In your console:
$ activator new my-first-app play-scala
Please bear in mind that I just automate the creation of scala project which follow the conventional project structure. I am using the Play framework and activator command, fyi, but this is out of topic.
2.) I open the project folder in IntelliJ by
a. Go to File -> New -> Project from existing source
b. find the "my-first-app" folder
c. double click the "build.sbt"
d. a prompt appear and check the "Use auto-import" checkbox
e. make sure the project sdk is a JDK 1.8
f. Click OK.
g. Select the modules data as root. As is.
h. Go to Settings (Ctrl + Alt + s)
I. Under "Languages & Frameworks", go to "Play2"
j. In the Play2 section, check the "Use SBT watcher" and "Use Play 2 compiler for this project"
h. Click "apply" then click "ok"
g. if still not work, refresh the project by going to SBT child window of intelliJ and click the refresh button.
Good luck! :D

Related

How to start with Scala, intelliJ IDEA and SBT?

I downloaded IntelliJ IDEA 14.0.3, SBT, Scala and SBT plugins, set environment variables correctly, i guess (if i can run Scala repl is it correct?)
When i start IntelliJ IDEA and choose new SBT-based project i get some problems:
1) no /src/ folder in project structure. How i can fix that?
2) can't compile manually created Scala file: IDEA suggest me edit configuration with Alt+Shift+F10 then i get window with various settings, so what directly should i edit?
Sorry for possibly stupid questions, but i just want to run miserable
"hello world" in Scala...
You don't need any extra plugins besides the Scala plugin. Assuming you have that installed, then
Step One: Fire up IntelliJ and Click on "Create New Project"
Step Two: Choose Scala from the left hand side panel and choose SBT on the right hand side panel (i.e SBT-based Scala project) and click Next.
Step Three: Fill in your project details and click Finish
Step Four: Wait for Intellij to download all SBT dependencies and to construct the folder structure and to index.
Step Five: Begin hacking!

IntelliJ IDEA: Cannot import SBT project

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

Can't debug a Scala application in IntelliJ + sbt-idea-plugin

I'm developing a Scala application using IntelliJ Idea 12, sbt and sbt-idea plugin. I generated all necessary files by saying "gen-idea" and everything goes well -- I was able to compile the sources in command line in sbt as well in IntelliJ Idea 12 itself using menu Build->Make Project.
Here is my main class (Application.scala)
object Application extends App {
val a = 12345
println("application entry point 1235")
}
I have a breakpoint at val a = 12345.
1) There were other errors before, but now says "Module is not specified". How do I solve this?
I'll be posting updates since there would be other errors as my gut tells me.
To debug just like to run, you need to create a run configuration (menu Run -> Edit Configurations). If you haven't done yet, you need to add an Application entry with the + button.
Not only do you need to specify the main class, but also which "module" that class belongs to. By default, "Use classpath of module" will be empty. Here in the popup menu, you need to select the main module (not the one ending in "-build"). After you choose that and close with "Ok", it should work.
Although not necessary, I also recommend to use sbt for building instead of "Make". In the configuration in the "Before launch" part, select "Make" and click on "-", then click on "+" and choose sbt -> test:products.
Edit: Here is the reference for the SBT plugin for IntelliJ.

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.