Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty please support sub-projects with different Scala versions. #53

Closed
steinybot opened this issue Mar 4, 2021 · 5 comments
Closed

Comments

@steinybot
Copy link
Contributor

My use case for needing sub-projects with different Scala versions is when there are 2 sub-projects, one that is a Scala library that is cross built for Scala 2.12 and 2.13 and another sub-project which is an sbt plugin that can only be built with Scala 2.12.

@steinybot
Copy link
Contributor Author

steinybot commented Mar 4, 2021

I may have found a workaround:

lazy val sbtProject = (project in file("sbt"))
  .dependsOn(core)
  .enablePlugins(SbtPlugin)
  .settings(commonProjectSettings)
  .settings(
    skip := {
      CrossVersion.partialVersion((core / scalaVersion).value) match {
        case Some((2, n)) if n == 13 => true
        case _ => false
      }
    },
    crossScalaVersions := Nil,
  )

@steinybot
Copy link
Contributor Author

steinybot commented Mar 4, 2021

Well that didn't work.

Latest iteration is:

import explicitdeps.ExplicitDepsPlugin
import explicitdeps.ExplicitDepsPlugin.autoImport._
import sbt.Keys._
import sbt._
import sbt.plugins.SbtPlugin

// This is all the crazy hacks to get cross compiling working with an sub-project that is an sbt plugin.
object SbtSubProjectPluginPlugin extends AutoPlugin {

  override def trigger: PluginTrigger = allRequirements
  override def requires: Plugins      = ExplicitDepsPlugin && SbtPlugin

  private val sspppIsScala213 = settingKey[Boolean]("Checks if the current Scala version is 2.13")

  override def projectSettings: Seq[Def.Setting[_]] =
    List(
      crossScalaVersions := Nil,
      libraryDependencies := libraryDependenciesSetting.value,
      projectDependencies := projectDependenciesTask.value,
      sspppIsScala213 := {
        if (isScala213(scalaVersion.value))
          throw new IllegalStateException("sbt project must not use Scala 2.13. Did you force the version with '+'?")
        isScala213Setting.value
      },
      // We can't skip this as it has to run at least once or sbt complains.
      update / skip := false,
      // Skip everything else otherwise it will just fail.
      skip := sspppIsScala213.value,
      undeclaredCompileDependenciesFilter -= moduleFilter()
    )

  private def isScala213Setting = Def.setting {
    val versions =
      scalaVersion.all(ScopeFilter(inDependencies(ThisProject, transitive = true, includeRoot = false))).value
    versions.exists(isScala213)
  }

  private def isScala213(version: String) =
    CrossVersion.partialVersion(version) match {
      case Some((2, n)) if n == 13 => true
      case _                       => false
    }

  private def projectDependenciesTask = Def.task {
    // Remove all project dependencies for Scala 2.13 as they will not resolve when cross building.
    if (sspppIsScala213.value) {
      Seq.empty
    } else {
      projectDependencies.value
    }
  }

  private def libraryDependenciesSetting = Def.setting {
    // Remove all library dependencies for Scala 2.13 as they will not resolve when cross building.
    if (sspppIsScala213.value) {
      Seq.empty
    } else {
      libraryDependencies.value
    }
  }
}

@kubukoz
Copy link
Contributor

kubukoz commented Mar 7, 2021

@steinybot just out of curiosity, what would happen if you set crossScalaVersions in ThisBuild to the whole set of versions, but with specific overrides in each project?

@steinybot
Copy link
Contributor Author

That doesn't seem to make a difference. For the record the problem I have actually has nothing to do with sbt-github-actions instead it is sbt/sbt#5586.

@djspiewak
Copy link
Collaborator

Yeah this is a serious limitation of sbt right now. There are a lot of crazy hacks to get around it but none of them work well. It's also something that you somewhat-forcibly hit whenever you have Scala Native in your build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants