Skip to content

Commit ec2a3ca

Browse files
vasilmkdbuilduser
authored andcommitted
[build, sbt import] 253 backport: Add a workaround for manually attaching the community/project directory as a source directory to the scalaUltimate-build build module when the Ultimate and Community build definitions are unified under one sbt build instance #SCL-24902
#noport - This change is an IDEA 2025.3 backport of https://code.jetbrains.team/p/scl/repositories/intellij-scala/revision/05e468ddbf91c3c3fb855d6dd8b42afecabebb0c. - The commit cannot be cleanly applied because it depends on other changes present in 261. - Additionally, the code had to be adapted for 253, because it uses both APIs which don't exist in the platform and APIs which we have introduced for replacing deprecated functions in 261. (cherry picked from commit 8ffad66)
1 parent 88dd1bd commit ec2a3ca

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

sbt/sbt-impl/src/org/jetbrains/sbt/project/SbtProjectResolver.scala

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import org.jetbrains.plugins.scala.*
2121
import org.jetbrains.plugins.scala.build.*
2222
import org.jetbrains.plugins.scala.compiler.data.CompileOrder
2323
import org.jetbrains.plugins.scala.extensions.PathExt
24-
import org.jetbrains.plugins.scala.project.{ReplClasspath, Version}
2524
import org.jetbrains.plugins.scala.project.external.{JdkByHome, JdkByName, ScalaSdkUtils, SdkReference}
25+
import org.jetbrains.plugins.scala.project.{ReplClasspath, Version}
2626
import org.jetbrains.plugins.scala.util.ScalaNotificationGroups
2727
import org.jetbrains.sbt.SbtUtil.*
2828
import org.jetbrains.sbt.project.SbtProjectResolver.*
@@ -39,6 +39,7 @@ import org.jetbrains.sbt.{RichBoolean, Sbt, SbtBundle, SbtUtil, SbtVersion, usin
3939

4040
import java.io.FileNotFoundException
4141
import java.net.URI
42+
import java.nio.charset.StandardCharsets
4243
import java.nio.file.{Files, Path}
4344
import java.util.{Collections, Locale, UUID}
4445
import scala.collection.{MapView, mutable}
@@ -557,9 +558,8 @@ class SbtProjectResolver extends ExternalSystemProjectResolver[SbtExecutionSetti
557558
* }}}<br>
558559
* See also https://youtrack.jetbrains.com/issue/SCL-13573/Apply-shared-external-source-directory-logic-for-sbt-build-modules
559560
*/
560-
private def configureBuildModuleDependencies(buildModules: Seq[BuildModuleNodeWithBuildBaseDir]): Unit = {
561-
if (buildModules.size == 2) {
562-
val Seq(module1, module2) = buildModules
561+
private def configureBuildModuleDependencies(buildModules: Seq[BuildModuleNodeWithBuildBaseDir]): Unit = buildModules match {
562+
case Seq(module1, module2) =>
563563
if (isChild(module1.buildBaseDir, module2.buildBaseDir)) {
564564
addModuleDependencyNode(module2.moduleNode, module1.moduleNode, DependencyScope.COMPILE)
565565
}
@@ -569,7 +569,27 @@ class SbtProjectResolver extends ExternalSystemProjectResolver[SbtExecutionSetti
569569
else {
570570
//modules are not hierarchical? Not sure if such case possible but will leave the empty branch here
571571
}
572-
}
572+
573+
// See https://youtrack.jetbrains.com/issue/SCL-24902/Unify-the-Scala-Ultimate-and-Scala-Community-sbt-builds.
574+
// This is an extremely specific workaround for the Scala Plugin for IntelliJ IDEA Ultimate repository.
575+
// We set the <root>/community/project directory as a source directory for the "scalaUltimate-build" build module.
576+
case Seq(buildModule) if buildModule.moduleNode.getModuleName == "scalaUltimate-build" =>
577+
val buildBaseDir = buildModule.buildBaseDir
578+
val communityProjectDir = buildBaseDir / "community" / Sbt.ProjectDirectory
579+
if (communityProjectDir.exists) { // The community directory exists
580+
val buildSbtSourcesPatchFile = buildBaseDir / Sbt.ProjectDirectory / "build.sbt"
581+
if (buildSbtSourcesPatchFile.exists) { // <root>/project/build.sbt exists
582+
// The suggested optimized eel function does exist in 253, only 261 and later.
583+
//noinspection UseOptimizedEelFunctions
584+
val contents = Files.readString(buildSbtSourcesPatchFile, StandardCharsets.UTF_8)
585+
val containsPatch = contents.contains("""Compile / unmanagedSourceDirectories += baseDirectory.value.getParentFile / "community" / "project"""")
586+
if (containsPatch) {
587+
buildModule.moduleNode.add(createBuildContentRootForScalaPluginUltimateWorkaround(communityProjectDir))
588+
}
589+
}
590+
}
591+
592+
case _ =>
573593
}
574594

575595
private def isChild(child: Path, parentPath: Path): Boolean = {
@@ -1257,6 +1277,13 @@ class SbtProjectResolver extends ExternalSystemProjectResolver[SbtExecutionSetti
12571277
result
12581278
}
12591279

1280+
private def createBuildContentRootForScalaPluginUltimateWorkaround(communityProjectDir: Path): ContentRootNode = {
1281+
val result = new ContentRootNode(communityProjectDir.toCanonicalPath.toString)
1282+
val sourceDirs = Seq(communityProjectDir.toCanonicalPath.toString)
1283+
result.storePaths(esProjectData.ExternalSystemSourceType.SOURCE, sourceDirs)
1284+
result
1285+
}
1286+
12601287
private def createSbtBuildModuleData(build: sbtStructure.BuildData, projects: Seq[ProjectData], localCachePath: Option[String]): SbtBuildModuleNode = {
12611288
val buildProjects = projects.filter(p => p.buildURI == build.uri)
12621289
val imports = build.imports.flatMap(_.trim.substring(7).split(", "))

0 commit comments

Comments
 (0)