portable-scala/sbt-crossproject

Shared resource directory is ignored

rtimush opened this issue ยท 5 comments

The resource directory in the "shared" folder is not added to project resourceDirectories. This is a problem especially for CrossType.Pure as all resource directories for this layout are in the "hidden" .<platform> folder.

Ideally, resource directories should be treated in the same way as source directories are.

build.sbt:

import sbtcrossproject.{crossProject, CrossType}

lazy val foo =
  crossProject(JSPlatform, JVMPlatform)
    .crossType(CrossType.Pure)

lazy val `foo-jvm` = foo.jvm
lazy val `foo-js`  = foo.js

SBT shell:

sbt:tmp> show fooJVM/sourceDirectories
[info] * /private/tmp/foo/.jvm/src/main/scala-2.12
[info] * /private/tmp/foo/.jvm/src/main/scala
[info] * /private/tmp/foo/.jvm/src/main/java
[info] * /private/tmp/foo/src/main/scala-2.12
[info] * /private/tmp/foo/src/main/scala
[info] * /private/tmp/foo/.jvm/target/scala-2.12/src_managed/main
sbt:tmp> show fooJVM/resourceDirectories
[info] * /private/tmp/foo/.jvm/src/main/resources
[info] * /private/tmp/foo/.jvm/target/scala-2.12/resource_managed/main
sjrd commented

Good point. You can work around this with

.settings(
  unmanagedResourceDirectories += baseDirectory.value / "src/main/resources"
)

I used the following code as a workaround:

.settings(
  Seq(Compile, Test).flatMap(inConfig(_) {
    unmanagedResourceDirectories ++= {
      unmanagedSourceDirectories.value
        .map(src => (src / ".." / "resources").getCanonicalFile)
        .filterNot(unmanagedResourceDirectories.value.contains)
        .distinct
    }
  })
)

It is a bit another issue, but there exists a similar problem for generated code.
For example, I want to use ScalaPB to generate code that will be shared between different platforms, and right now there are a copy of source file for each platform.

In addition to that, IDEA does not correctly resolve dependencies from code in shared to generated code (seems like a problem with IDEA project model though).

I am also seeing this issue with IntelliJ not resolving references, and it is a real problem with our project. Is this an IntelliJ problem or an sbt-crossproject problem? Any idea how we could work around it?

FYI, an alternative CrossType that keeps the project directory stable for the JVM target and only puts it in a subdirectory for the JS target works fine as a not-too-messy workaround, ala:

object PureIntelliJ extends CrossType {
  @deprecated("use projectDir(crossBase: File, platform: Platform): File", "0.1.0")
  def projectDir(crossBase: File, projectType: String): File =
    if (projectType == "jvm") crossBase
    else crossBase / ("." + projectType)

  def projectDir(crossBase: File, platform: Platform): File =
    if (platform.identifier == "jvm") crossBase
    else crossBase / ("." + platform.identifier)

  def sharedSrcDir(projectBase: File, conf: String): Option[File] =
    Some(projectBase.getParentFile / "src" / conf / "scala")
}