diff --git a/platform/build-scripts/bazel/src/org/jetbrains/intellij/build/bazel/lib.kt b/platform/build-scripts/bazel/src/org/jetbrains/intellij/build/bazel/lib.kt index f8be49107b9d..623ebed187a8 100644 --- a/platform/build-scripts/bazel/src/org/jetbrains/intellij/build/bazel/lib.kt +++ b/platform/build-scripts/bazel/src/org/jetbrains/intellij/build/bazel/lib.kt @@ -96,9 +96,9 @@ internal fun BuildFile.generateMavenLib( target("jvm_import") { option("name", targetName) - option("jar", "@${fileToHttpRuleFile(jar.path)}") + option("jar", "@${fileToHttpRuleFile(lib.mavenCoordinates, jar.path)}") if (sourceJar != null) { - option("source_jar", "@${fileToHttpRuleFile(sourceJar.path)}") + option("source_jar", "@${fileToHttpRuleFile(lib.mavenCoordinates + ":sources", jar.path)}") } if (targetName == "kotlinx-serialization-core") { option("exported_compiler_plugins", listOf("@lib//:kotlin-serialization-plugin")) @@ -113,14 +113,14 @@ internal fun BuildFile.generateMavenLib( load("@rules_java//java:defs.bzl", "java_library") target("java_library") { option("name", targetName) - option("exports", lib.jars.map { ":${fileToHttpRuleRepoName(it.path)}_import" }) + option("exports", lib.jars.map { ":${mavenCoordinatesToHttpRuleRepoName(lib.mavenCoordinates, it.path)}_import" }) libVisibility?.let { visibility(arrayOf(it)) } } for (jar in lib.jars) { - val bazelLabel = fileToHttpRuleRepoName(jar.path) + val bazelLabel = mavenCoordinatesToHttpRuleRepoName(lib.mavenCoordinates, jar.path) val label = "${bazelLabel}_import" if (!labelTracker.add(label)) { continue @@ -131,7 +131,7 @@ internal fun BuildFile.generateMavenLib( option("name", label) option("jar", "@$bazelLabel//file") if (sourceJar != null) { - option("source_jar", "@${fileToHttpRuleFile(sourceJar.path)}") + option("source_jar", "@${fileToHttpRuleFile(lib.mavenCoordinates + ":sources", jar.path)}") } } } @@ -209,7 +209,7 @@ internal fun generateBazelModuleSectionsForLibs( buildFile(bazelFileUpdater, owner.sectionName) { for (lib in list) { for (jar in lib.jars) { - val label = fileToHttpRuleRepoName(jar.path) + val label = mavenCoordinatesToHttpRuleRepoName(lib.mavenCoordinates, jar.path) if (!labelTracker.add(label)) { continue } @@ -228,7 +228,7 @@ internal fun generateBazelModuleSectionsForLibs( } for (jar in lib.sourceJars) { - val label = fileToHttpRuleRepoName(jar.path) + val label = mavenCoordinatesToHttpRuleRepoName(lib.mavenCoordinates, jar.path) if (!labelTracker.add(label)) { continue } @@ -251,9 +251,46 @@ internal fun generateBazelModuleSectionsForLibs( } } -private fun fileToHttpRuleRepoName(jar: Path): String = bazelLabelBadCharsPattern.replace(jar.nameWithoutExtension, "_") + "_http" +/** + * We need to use this format to avoid clashes: + * ``` + * [groupId]-[artifactId]-[version]-[jarFileName] + * ``` + * + * We can't use only the filename, as there can be non-unique filenames in different GAV coordinates; we can't only use the coordinates, as there are transitive dependencies in + * many JPS Maven library entries. + * + * To reduce noise, we can remove duplication of the GAV coordinate parts from the jar filename, and then make sure there are no consecutive dashes left over. + */ +private fun mavenCoordinatesToHttpRuleRepoName(mavenCoordinates: String, jarPath: Path): String { + val parts = mavenCoordinates.split(":") + require(parts.size >= 3) { "Maven coordinates must have at least groupId:artifactId:version format: $mavenCoordinates" } + val name = buildString { + append(parts[0]) + append('-') + append(parts[1]) + append('-') + append(parts[2]) + append('-') -private fun fileToHttpRuleFile(jar: Path): String = fileToHttpRuleRepoName(jar) + "//file" + val normalizedFilename = jarPath.nameWithoutExtension.trim() + .replace(parts[0], "") + .replace(parts[1], "") + .replace(parts[2], "") + .replace(parts[2].replace(".", "_"), "") + + if (normalizedFilename.isNotEmpty()) { + append(normalizedFilename) + } + } + .replace("-+".toRegex(), "-") + .removeSuffix("-") + + val sanitizedName = bazelLabelBadCharsPattern.replace(name, "_") + return sanitizedName + "_http" +} + +private fun fileToHttpRuleFile(coordinates: String, jarPath: Path): String = mavenCoordinatesToHttpRuleRepoName(coordinates, jarPath) + "//file" internal fun generateLocalLibs(libs: Collection, isLibraryProvided: (Library) -> Boolean, fileToUpdater: MutableMap) { for ((dir, libs) in libs.sortedBy { it.target.targetName }.groupBy { it.bazelBuildFileDir }) { @@ -283,4 +320,4 @@ internal fun generateLocalLibs(libs: Collection, isLibraryProvided } } } -} \ No newline at end of file +}