IntelliJ repeatedly forgets that target src_managed is a source directory

Viewed 921

I am using IntelliJ 2017.1 Ultimate edition. I am working on a scala project where I generate some code using SBT.

The code is copied into target/scala-2.11/src_managed folder.

Time and again, my compilation fails and I see that IntelliJ has forgotten that src_managed is a source dir

enter image description here

If i right click on the src_managed folder and say mark directory as source root then compilation succeeds. But its very irritating that IntelliJ forgets time and again, that this is a source directory.

2 Answers

Stumbled upon this question as I had the same problem. Though this question is old, I'll answer with my solution for others that may have the same problem.

Add the directory as managed source directory in your build.sbt:

Compile / managedSourceDirectories += baseDirectory.value / "target/scala-2.13/src_managed"

// or if you have your scala version in a value/variable
lazy val scalaVersion = "2.13.3"
Compile / managedSourceDirectories += baseDirectory.value / s"target/scala-${"""[\d]*[\.][\d]*""".r.findFirstIn(scalaVersion).get}/src_managed"

For some reason when you add "main" to the source path and it creates the sources in src_managed/main then Intellj picks it up:

.settings(
  (sourceGenerators in Compile) += (codeGen in Compile),
  (codeGen in Compile) := {
    val r = (runner in Compile).value
    val s = streams.value.log
    // This is [basedir]/target/scala-2.11/src_managed
    val sourcePath = sourceManaged.value
    val classPath = (fullClasspath in Test in `generator`).value.map(_.data)

    // This is [basedir]/target/scala-2.11/src_managed/main
    val fileDir = new File(sourcePath, "main").getAbsoluteFile
    r.run(
     "com.github.integration.CodeGeneratorRunner",
      classPath, Seq(fileDir.getAbsolutePath), s
    )
  )
)

... and then intellj picks it up!

Intellij Picks It Up

I don't know how, I don't know why but it does. SBT works either way. Maybe there's some SBT convention we don't know about, maybe there's a bug with Intellij.

Related