Skip to content

Commit

Permalink
BuildConfig: Fix validation for module platform compatibility
Browse files Browse the repository at this point in the history
The validation triggered a false positive for modules with custom
build targets.

The compatibility must be checked for all platform-specific modules
separately since these may depend on additional modules which are
not included on the base module.
  • Loading branch information
tindzk committed Nov 3, 2019
1 parent 33490d7 commit 977c3bf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
36 changes: 24 additions & 12 deletions src/main/scala/seed/config/BuildConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,27 @@ object BuildConfig {
hasCycle(name, Set())
}

val incompatibleModuleDepPlatform =
module.moduleDeps
.flatMap { name =>
build.get(name).map(m => name -> m.module)
}
.map { case (n, m) => (n, module.targets.diff(m.targets)) }
.find(_._2.nonEmpty)
// Check whether platforms are missing on the given module's dependencies
val missingModuleDepPlatform: Option[(String, Platform)] = {

/** @return None if module has custom build targets */
def resolveModule(name: String) =
build.get(name).map(m => name -> m.module).filter(_._2.target.isEmpty)

def platform(p: Platform) =
platformModule(module, p).flatMap(
_.moduleDeps.flatMap(resolveModule).collectFirst {
case (name, module) if !module.targets.contains(p) => (name, p)
}
)

// Compatibility must be checked for all platform-specific modules
// separately since these may depend on additional modules which are not
// included on the base module
platform(JVM)
.orElse(platform(JavaScript))
.orElse(platform(Native))
}

val moduleName = Ansi.italic(name)

Expand Down Expand Up @@ -389,12 +403,10 @@ object BuildConfig {
error(s"Module $moduleName cannot depend on itself")
else if (cyclicModuleDep2)
error(s"Cycle detected in dependencies of module $moduleName")
else if (incompatibleModuleDepPlatform.isDefined)
else if (missingModuleDepPlatform.isDefined)
error(
s"Module ${Ansi.italic(incompatibleModuleDepPlatform.get._1)} has missing target platform(s) (${incompatibleModuleDepPlatform.get._2
.map(_.id)
.map(Ansi.italic)
.mkString(", ")}) required by $moduleName"
s"Module ${Ansi.italic(missingModuleDepPlatform.get._1)} has missing target platform ${Ansi
.italic(missingModuleDepPlatform.get._2.id)} required by $moduleName"
)
else true
}
Expand Down
53 changes: 51 additions & 2 deletions src/test/scala/seed/config/BuildConfigSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,62 @@ object BuildConfigSpec extends SimpleTestSuite {
assert(
messages.exists(
_.contains(
s"Module ${Ansi.italic("foo")} has missing target platform(s) (${Ansi
.italic("native")}) required by ${Ansi.italic("bar")}"
s"Module ${Ansi.italic("foo")} has missing target platform ${Ansi
.italic("native")} required by ${Ansi.italic("bar")}"
)
)
)
}

test("Platform compatibility when inheriting (2)") {
val buildToml = """
|[project]
|scalaJsVersion = "0.6.26"
|scalaNativeVersion = "0.3.7"
|
|[module.foo.js]
|scalaVersion = "2.11.11"
|sources = ["foo/"]
|
|[module.bar.native]
|scalaVersion = "2.11.11"
|moduleDeps = ["foo"]
|sources = ["bar/"]
""".stripMargin

val messages = ListBuffer[String]()
val log = new Log(messages += _, identity, LogLevel.Error, false)
parseBuild(buildToml, log, fail = true)(_ => "")
assert(
messages.exists(
_.contains(
s"Module ${Ansi.italic("foo")} has missing target platform ${Ansi
.italic("native")} required by ${Ansi.italic("bar")}"
)
)
)
}

test("Custom build targets do not need to set any platforms") {
val buildToml = """
|[project]
|scalaVersion = "2.11.11"
|
|[module.template.target.scss]
|root = "scss"
|command = "yarn install && yarn run gulp"
|
|[module.app.jvm]
|moduleDeps = ["template"]
|sources = ["src/"]
""".stripMargin

val messages = ListBuffer[String]()
val log = new Log(messages += _, identity, LogLevel.Error, false)
val build = parseBuild(buildToml, log)(_ => "")
assertEquals(build("app").module.targets, List(JVM))
}

test("Scala version compatibility") {
val buildToml = """
|[module.foo.jvm]
Expand Down

0 comments on commit 977c3bf

Please sign in to comment.