Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ repeatedly forgets that target src_managed is a source directory

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.

like image 261
Knows Not Much Avatar asked Sep 14 '25 14:09

Knows Not Much


2 Answers

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.

like image 100
Choppy The Lumberjack Avatar answered Sep 16 '25 08:09

Choppy The Lumberjack


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"
like image 42
anderha Avatar answered Sep 16 '25 07:09

anderha