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

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.

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.

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

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.

Scala IDE 4.0.0 thinks there's errors in an out-of-the-box Play Framework 2.3.7 program

I've created a Play Framework program via Typesafe Activator (so it follows the template exactly).
I used sbteclipse-plugin version 3.0.0 to create an Eclipse project and imported that into Scala IDE 4.0.0. These are all the latest versions at the time of writing.
The Scala IDE definitely seems to support the Play Framework. It has syntax highlighting for the custom formats, including the routing file and templates. Yet, it doesn't seem to be able to find the views from the controllers. In particular, the call to views.html.index triggers an error: "object index is not a member of package views.html".
I tried enabling refreshing using native hooks or pooling as detailed here, but it had no affect.
I should note that while the code has been compiled in the command line (with activator ~run), it hasn't been compiled in Scala IDE, since I don't know how to (it doesn't seem to be documented anywhere).
What can I do to get rid of these false errors?
EDIT: After running activator clean ~run, I have another error: The project cannot be built until build path errors are resolved. There's no further details on what these build path errors are.
Update: Just upgrade to sbteclipse version 5.1.0 and everything should work out of the box. Also make sure you follow the Play documentation on how to set up Eclipse/ScalaIDE.
This is a known bug in sbteclipse, which probably will be fixed soon.
For now, you can add the following line to your build.sbt:
EclipseKeys.createSrc := EclipseCreateSrc.All
Kill the SBT console and run sbt eclipse again. That should add the following line to the .classpath file within your project folder as a workaround:
<classpathentry kind="src" path="target/scala-2.11/twirl/main"/>
Refresh your Eclipse project to pick up the change.
I had the same issue, also with Scala IDE 4.0.0 . I followed mkurz instuctions and they worked like a charm. But instead of changing the .classpath file in the project folder manually I used Eclipse interface:
In the top menu of the main window, click on Project and then on Properties.
In the Properties window, click on Java Build Path option (options list is on the left)
In the Source tab, click on Add Folder... button.
In the Source Folder Selection window, choose the target/scala-2.11/twirl/main folder, so it is included in the compilation path. Click Ok button.
Click Ok in the Properties window.
Now the project should compile just fine :) . With that I was able to finish the play setup example in Scala IDE website
I tried #mkurz solution first, but also ran into the same error as #matt. I became frustrated that I could not generate the eclipse project without having to go to the Eclipse project properties to manually fix the build errors. After some investigation, I discovered the solution that removed all errors entirely. Add this your build.sbt:
unmanagedSourceDirectories in Compile <+= twirlCompileTemplates.target
Or if that does not work for you, you could also use:
unmanagedSourceDirectories in Compile <+= target.zipWith(scalaBinaryVersion) { (b,v) => b / s"scala-$v/twirl/main" }
Good bye, build errors!
I got the same error message.
Are you using java8 as jre in eclipse?
After switching back from java8 to java7, everything worked fine again.
If, after following Mkurz' instructions (adding EclipseKeys.CreateSrc... ), your problems are not solved, click on Project -> Properties -> Java Build Path. Look at the source folders tab.
You may find a duplicate file folder named .../src_managed/main (Thanks Matt). If so, close the project. Remove ONE of the two ../src_managed/main entries from the .classpath file (located in the base of the activator/SBT project directory). Reopen and clean the project and you should be good to go.
For me, it turned out that installed JRE in the Scala IDE was openjdk, changed it to Oracle Java 8 and it worked.

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.

Starting a new project with sbt-idea plugin

I am new to sbt and the sbt-idea plugin. I created a new project with the plugin and when opening the generated .idea file inside IntelliJ and compiling I am getting that "please specify compiler in Scala facet". When looking on the scala compiler facet all I see is "buildScala" in red.
Since I saw many here are using the plugin, can you explain the steps you took to correct this?
I have this problem when I use the sbt-idea processor, and then import the module into an existing IDEA project. However, when I open the project created by sbt-idea I do not have the problem.
I have not been able to fix the red buildScala problem with imported modules. I suspect it would require tomfoolery in the project files, as I can't find config options to correct it via the GUI.
Personally, I always install sbt-idea as a processor in every new SBT install I make. (See "Usage as processor" in the previous link). Then the correct way to generate project files is simply sbt idea.
Can you clarify exactly the steps you followed that led to the error?