Scala's build.sbt not recognised by IntelliJ

Viewed 1598

I'm having an issue where my build.sbt file is not being picked up properly using IntelliJ (Ultimate 2020.2.3). I create a new Scala sbt project using the IntelliJ wizard (file > new > project > scala / sbt) and add a new dependancy to the buid.sbt file but when I try to import it into one of my class files, IntelliJ will not compile my project due to a dependancy issue.

The sbt tool window picks up the external dependancies but my project "external dependancies" doesn't contain the library at all, even after I refresh the project. Also, my build.sbt file has a bunch of errors in it like it's not being recognised properly by IntelliJ but I've installed the Scala plugin so I don't know what else I could do?

Here you can see that I've added the org.slf4j dependancy into my build.sbt; It's picked up by the sbt tool window (on the right) but not by my project (on the left)

enter image description here

When I hover over the libraryDependencies in the build.sbt file it doesn't seem to recognise the sbt syntax:

enter image description here

I cannot import the library into my class file and the project wont build:

enter image description here

I'm new to Scala and I'm simply trying to create a basic hello world project but I can't seem to get past this frustrating issue.

2 Answers

You can try first to create an SBT project manually in some directory. build.sbt alone would be enough.

Then run sbt in that directory to see that SBT shell works properly. Try some commands like sbt compile, sbt run with some helloworld app. When you create helloworld app mind the directory structure where you put it, i.e.: src/main/scala/HelloWorld.scala.

If all that works go to IntelliJ and import/open project from there - select build.sbt.

When you create or import SBT project from IntelliJ it takes a while to bootstrap everything and download compiler, etc. Just leave it until it's done. Same goes for first time compilation when the compiler needs to be compiled :).

To setup a sample project you can issue these shell commands:

mkdir untitled
cd untitled/

echo 'name := "untitled"

version := "0.1"

scalaVersion := "2.13.3"

libraryDependencies ++= Seq(
  "org.slf4j" % "slf4j-simple" % "1.7.26",
)
' > build.sbt

mkdir -p src/main/scala/

echo 'object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello World")
  }
}
' > src/main/scala/HelloWorld.scala

sbt run

If there are not issues with these steps then import build.sbt into IntelliJ.

I was finally able to get past this issue. I'm not 100% sure what solved it in the end unfortunately but disabling a bunch of plugins I wasn't using seemed to fix the issue for me.

I suspect it may have been the Gradle plugin since I saw some errors in the idea.log file but after re-enabling the Gradle plugin to test everything is still working so not sure.

Related