Skip to content

Commit

Permalink
dependencies duplication fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-skrodzka committed Nov 4, 2024
1 parent 05c422d commit 1b365d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 12 additions & 1 deletion deps/src/MakeTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ object MakeTree:
// a package and call the extensions
writeToFile(vars.depsBuildFile, "")

// create a set of coordinates with the same cleanName and groupId
// to prevent duplicate import names in the generated output
val duplicateCoordinates: Set[Coordinates] = targets
.groupBy(target => (target.coordinates.cleanName, target.coordinates.groupId))
.collect {
case ((cleanName, groupId), groupedTargets) if groupedTargets.size > 1 =>
groupedTargets.map(_.coordinates)
}
.flatten
.toSet

// make tree of BUILD files
targets
.groupBy(_.coordinates.groupId)
.foreach { (group, targets) =>
val file = new File(vars.targetsTreeFile, group.toPath + File.separator + vars.targetsFileName)
val content = cfg.targetsHeader + targets.map(_.toBzl()).mkString
val content = cfg.targetsHeader + targets.map(_.toBzl(duplicateCoordinates)).mkString
writeToFile(file, content)
}
4 changes: 2 additions & 2 deletions deps/src/Resolve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sbt.internal.util.ConsoleLogger
import sbt.librarymanagement.syntax.*
import sbt.librarymanagement.{Configurations, ScalaModuleInfo, UnresolvedWarningConfiguration, UpdateConfiguration}
import sbt.internal.librarymanagement.cross.CrossVersionUtil
import scala.collection.mutable.HashMap
import scala.collection.mutable

object Resolve:
def apply()(using vars: Vars, cfg: DepsCfg): Vector[Target] =
Expand Down Expand Up @@ -44,7 +44,7 @@ object Resolve:
.contains(moduleReport.module.toUvCoordinates.withCleanName)
}
val uvCoordinates_modules = modules.map { m => m.module.toUvCoordinates -> m.module }.toMap
val modules_deps: HashMap[Coordinates, Vector[Coordinates]] = HashMap.empty
val modules_deps: mutable.HashMap[Coordinates, Vector[Coordinates]] = mutable.HashMap.empty
modules.foreach { moduleReport =>
moduleReport.callers.foreach { caller =>
// dependency can be resolved with a different version,
Expand Down
16 changes: 12 additions & 4 deletions deps/src/Target.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ final case class Target(
url: String,
deps: Vector[Coordinates]
):
def toBzl()(using vars: Vars): String =
def toBzl(duplicateCoordinates: Set[Coordinates] = Set.empty)(using vars: Vars): String =

def getAdjustedName(coordinates: Coordinates): String =
if duplicateCoordinates.contains(coordinates)
then coordinates.artifactId.replaceAll("[-.]", "_")
else coordinates.cleanName

replacement_label match
case Some(replacement_label) =>
s"""\n${lang.asString}_import(
Expand All @@ -27,12 +33,14 @@ final case class Target(
| ]
|)\n""".stripMargin
case None =>
val adjustedName = getAdjustedName(coordinates)
val runtime_deps =
val deps0 = deps
.map { c =>
val depName = getAdjustedName(c)
if coordinates.groupId == c.groupId
then s"\":${c.cleanName}\""
else s"\"${vars.targetsTreeBazelPath}/${c.groupId.toUnixPath}:${c.cleanName}\""
then s"\":$depName\""
else s"\"${vars.targetsTreeBazelPath}/${c.groupId.toUnixPath}:$depName\""
}
.sorted
.mkString(",\n ")
Expand All @@ -43,7 +51,7 @@ final case class Target(
| $deps0
| ],""".stripMargin
s"""\n${lang.asString}_import(
| name = "${coordinates.cleanName}",
| name = "$adjustedName",
| jars = [
| "$jar"
| ],$runtime_deps
Expand Down

0 comments on commit 1b365d7

Please sign in to comment.