diff --git a/deps/src/MakeTree.scala b/deps/src/MakeTree.scala index bdb2824..c4261ea 100644 --- a/deps/src/MakeTree.scala +++ b/deps/src/MakeTree.scala @@ -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) } diff --git a/deps/src/Resolve.scala b/deps/src/Resolve.scala index 96eac4e..b922c4a 100644 --- a/deps/src/Resolve.scala +++ b/deps/src/Resolve.scala @@ -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] = @@ -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, diff --git a/deps/src/Target.scala b/deps/src/Target.scala index f614f80..de4bee2 100644 --- a/deps/src/Target.scala +++ b/deps/src/Target.scala @@ -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( @@ -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 ") @@ -43,7 +51,7 @@ final case class Target( | $deps0 | ],""".stripMargin s"""\n${lang.asString}_import( - | name = "${coordinates.cleanName}", + | name = "$adjustedName", | jars = [ | "$jar" | ],$runtime_deps