Search code examples
scalaintellij-ideasbtintellij-13

What is the issue with this sbt file?


When I import a SBT project into intelliJ, the build.sbt file is showing lot of errors as shown in the following screenshot. Wondering what might be the issue

IDEA Version 13.1.4

I also see the following

The following source roots are outside of the corresponding base directories:
C:\Users\p0c\Downloads\recfun\src\main\resources
C:\Users\p0c\Downloads\recfun\src\test\java
C:\Users\p0c\Downloads\recfun\src\test\scala
C:\Users\p0c\Downloads\recfun\src\test\resources
These source roots cannot be included in the IDEA project model. Please consider using shared SBT projects instead of shared source roots.

enter image description here


Solution

  • I think the question perhaps does not provide all the required information to answer conclusively, but I'll give it a spin anyways -

    Since sbt runs correctly when invoked from the shell, we know the sbt file is fine. I use Idea for my Scala and sbt projects and I am sure the Idea sbt support works very well, but! Idea is far more restrictive than sbt when it comes to project structure. It is really easy to create a valid sbt project structure that Idea can't support very well.

    Given that the source roots error indicates that the recfun/src folder is not in the project folder, it seems clear that this error is not emitted during the processing of recfun/build.sbt. The screenshot shows you have at least three different sbt files, progfun-recfun, submission and scala-recfun. Since Idea will also create projects like submission-build, and you have an assignment-build project there, something is probably broken in the project structure, not from the sbt viewpoint - there you're fine, you can build - but from the Idea viewpoint, which is more restrictive.

    My suggestion to resolve this would be to change your project structure as follows. First, have a top level project with a build.sbt. Then create a sub-project for each src folder you want. Do not put a src folder in your top level project. Each sub-project needs a build.sbt as well.

    Second, make sure the sub-projects build correctly with sbt when run from the shell. Arrange the sub-project build.sbt files with the proper dependencies, using this syntax:

    lazy val a = ProjectRef(file("../a"), "a")
    lazy val b = ProjectRef(file("../b"), "b")
    lazy val root = Project(id = "c", base = file(".")) dependsOn (a, b)
    

    (This example has three sister projects a, b and c, where c depends on a and b. The three projects are placed in directories with the same name, within the root directory. The code snippet is from the build file for c.)

    Third, arrange your top level build.sbt to aggregate the sub-projects, using this syntax in the top level build.sbt:

    lazy val a = ProjectRef(file("a"), "a")
    lazy val b = ProjectRef(file("b"), "b")
    lazy val c = ProjectRef(file("c"), "c")
    
    lazy val root = (project in file(".")).
      aggregate(a, b, c)
    

    Building this top level project will build each of the sub-projects a, b and c, and the dependencies established in the sub-project build files will ensure they are built in the right order.

    Fourth, import the top level project into Idea, and all should be well.

    You can get fancy with the file structure if you want, because the project references use relative paths, but it's usually nice to at least start simple.

    I had a lot of frustration with sbt and Idea at the start, I hope this helps :)