Skip to content

Commit

Permalink
[feature] Add a flag that makes the algorithm more likely to interpre…
Browse files Browse the repository at this point in the history
…t target as a library than a module

Merge-request: BAZEL-MR-1039
Merged-by: Xuan Son Trinh <xuanson.trinh@jetbrains.com>
  • Loading branch information
tpasternak authored and Space Team committed Apr 17, 2024
1 parent ac69b36 commit 8d13b46
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 9 deletions.
@@ -1,6 +1,7 @@
package org.jetbrains.bsp.bazel.projectview.model

import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.projectview.model.sections.ExperimentalUseLibOverModSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelBinarySection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildFlagsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildManualTargetsSection
Expand Down Expand Up @@ -38,6 +39,8 @@ data class ProjectView(
val enabledRules: ProjectViewEnabledRulesSection?,
/** local java home path to override to use with IDE, e.g. IntelliJ IDEA */
val ideJavaHomeOverride: ProjectViewIdeJavaHomeOverrideSection?,
/** use a new predicate that is more likely to interpret targets as libraries than as modules */
val useLibOverModSection: ExperimentalUseLibOverModSection? = null,
) {

data class Builder(
Expand All @@ -51,6 +54,8 @@ data class ProjectView(
private val importDepth: ProjectViewImportDepthSection? = null,
private val enabledRules: ProjectViewEnabledRulesSection? = null,
private val ideJavaHomeOverride: ProjectViewIdeJavaHomeOverrideSection? = null,
private val useLibOverModSection: ExperimentalUseLibOverModSection? = null,

) {

fun build(): ProjectView {
Expand All @@ -69,6 +74,8 @@ data class ProjectView(
val importDepth = combineImportDepthSection(importedProjectViews)
val enabledRules = combineEnabledRulesSection(importedProjectViews)
val ideJavaHomeOverride = combineIdeJavaHomeOverrideSection(importedProjectViews)
val useLibOverModSection = combineUseLibOverModSection(importedProjectViews)

log.debug(
"Building project view with combined"
+ " targets: {},"
Expand All @@ -79,7 +86,8 @@ data class ProjectView(
+ " deriveTargetsFlag: {}."
+ " import depth: {},"
+ " enabled rules: {},"
+ " ideJavaHomeOverride: {},",
+ " ideJavaHomeOverride: {},"
+ " useLibOverModSection: {},",
targets,
bazelBinary,
buildFlags,
Expand All @@ -89,6 +97,7 @@ data class ProjectView(
importDepth,
enabledRules,
ideJavaHomeOverride,
useLibOverModSection
)
return ProjectView(
targets,
Expand All @@ -100,6 +109,14 @@ data class ProjectView(
importDepth,
enabledRules,
ideJavaHomeOverride,
useLibOverModSection,
)
}

private fun combineUseLibOverModSection(importedProjectViews: List<ProjectView>): ExperimentalUseLibOverModSection? {
return useLibOverModSection ?: getLastImportedSingletonValue(
importedProjectViews,
ProjectView::useLibOverModSection
)
}

Expand Down
Expand Up @@ -40,3 +40,10 @@ data class ProjectViewImportDepthSection(override val value: Int) :
const val SECTION_NAME = "import_depth"
}
}

data class ExperimentalUseLibOverModSection(override val value: Boolean) :
ProjectViewSingletonSection<Boolean>(SECTION_NAME) {
companion object {
const val SECTION_NAME = "experimental_use_lib_over_mod"
}
}
Expand Up @@ -3,7 +3,7 @@ package org.jetbrains.bsp.bazel.projectview.parser
import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.commons.escapeNewLines
import org.jetbrains.bsp.bazel.projectview.model.ProjectView
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewIdeJavaHomeOverrideSection
import org.jetbrains.bsp.bazel.projectview.parser.sections.ExperimentalUseLibOverModSectionParser;
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBazelBinarySectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBuildFlagsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBuildManualTargetsSectionParser
Expand Down Expand Up @@ -43,6 +43,7 @@ open class DefaultProjectViewParser : ProjectViewParser {
importDepth = ProjectViewImportDepthSectionParser.parse(rawSections),
enabledRules = ProjectViewEnabledRulesSectionParser.parse(rawSections),
ideJavaHomeOverride = ProjectViewIdeJavaHomeOverrideSectionParser.parse(rawSections),
useLibOverModSection = ExperimentalUseLibOverModSectionParser.parse(rawSections)
).build()
}

Expand Down
@@ -1,6 +1,7 @@
package org.jetbrains.bsp.bazel.projectview.parser.sections

import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.projectview.model.sections.ExperimentalUseLibOverModSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelBinarySection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildManualTargetsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDeriveTargetsFromDirectoriesSection
Expand Down Expand Up @@ -87,3 +88,12 @@ object ProjectViewIdeJavaHomeOverrideSectionParser :

override fun createInstance(value: Path): ProjectViewIdeJavaHomeOverrideSection = ProjectViewIdeJavaHomeOverrideSection(value)
}

object ExperimentalUseLibOverModSectionParser :
ProjectViewSingletonSectionParser<Boolean, ExperimentalUseLibOverModSection>(ExperimentalUseLibOverModSection.SECTION_NAME) {

override fun mapRawValue(rawValue: String): Boolean = rawValue.toBoolean()

override fun createInstance(value: Boolean): ExperimentalUseLibOverModSection =
ExperimentalUseLibOverModSection(value)
}
@@ -0,0 +1,14 @@
package org.jetbrains.bsp.bazel.workspacecontext

import org.jetbrains.bsp.bazel.executioncontext.api.ExecutionContextEntityExtractor
import org.jetbrains.bsp.bazel.executioncontext.api.ExecutionContextSingletonEntity
import org.jetbrains.bsp.bazel.projectview.model.ProjectView

data class ExperimentalUseLibOverModSpec(
override val value: Boolean,
) : ExecutionContextSingletonEntity<Boolean>()

internal object ExperimentalUseLibOverModSpecExtractor : ExecutionContextEntityExtractor<ExperimentalUseLibOverModSpec> {
override fun fromProjectView(projectView: ProjectView): ExperimentalUseLibOverModSpec =
ExperimentalUseLibOverModSpec(projectView.useLibOverModSection?.value ?: false)
}
Expand Up @@ -76,6 +76,8 @@ data class WorkspaceContext(
* Obtained from `ProjectView` simply by mapping `ide_java_home_override` section.
*/
val ideJavaHomeOverrideSpec: IdeJavaHomeOverrideSpec,

val experimentalUseLibOverModSection: ExperimentalUseLibOverModSpec
) : ExecutionContext()

class WorkspaceContextConstructor(workspaceRoot: Path) : ExecutionContextConstructor<WorkspaceContext> {
Expand All @@ -98,6 +100,7 @@ class WorkspaceContextConstructor(workspaceRoot: Path) : ExecutionContextConstru
importDepth = ImportDepthSpecExtractor.fromProjectView(projectView),
enabledRules = EnabledRulesSpecExtractor.fromProjectView(projectView),
ideJavaHomeOverrideSpec = IdeJavaHomeOverrideSpecExtractor.fromProjectView(projectView),
experimentalUseLibOverModSection = ExperimentalUseLibOverModSpecExtractor.fromProjectView(projectView),
)
}
}
Expand Down
Expand Up @@ -36,6 +36,7 @@ object ProjectViewCLiOptionsProvider {
buildManualTargets = toBuildManualTargetsSection(projectViewCliOptions),
enabledRules = toEnabledRulesSection(projectViewCliOptions),
ideJavaHomeOverride = toIdeJavaHomeOverrideSection(projectViewCliOptions),
useLibOverModSection = null, // Experimental flag, no need to be configurable via CLI
)

private fun toBazelBinarySection(projectViewCliOptions: ProjectViewCliOptions?): ProjectViewBazelBinarySection? =
Expand Down
Expand Up @@ -8,7 +8,6 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.jetbrains.bsp.bazel.bazelrunner.BazelInfo
import org.jetbrains.bsp.bazel.info.BspTargetInfo
import org.jetbrains.bsp.bazel.info.BspTargetInfo.FileLocation
import org.jetbrains.bsp.bazel.info.BspTargetInfo.TargetInfo
import org.jetbrains.bsp.bazel.logger.BspClientLogger
Expand Down Expand Up @@ -108,7 +107,7 @@ class BazelProjectMapper(
(removeDotBazelBspTarget(allTargetNames) - targetsToImport.map(TargetInfo::getId).toSet()).map { Label(it) }
}
val rustExternalTargetsToImport = measure("Select external Rust targets") {
selectRustExternalTargetsToImport(rootTargets, dependencyGraph)
selectRustExternalTargetsToImport(rootTargets, dependencyGraph, workspaceContext)
}
val rustExternalModules = measure("Create Rust external modules") {
createRustExternalModules(rustExternalTargetsToImport, dependencyGraph, librariesFromDeps)
Expand Down Expand Up @@ -386,15 +385,15 @@ class BazelProjectMapper(
.toSet()

private fun selectRustExternalTargetsToImport(
rootTargets: Set<String>, graph: DependencyGraph
rootTargets: Set<String>, graph: DependencyGraph, workspaceContext: WorkspaceContext
): Sequence<TargetInfo> =
graph.allTargetsAtDepth(-1, rootTargets).asSequence().filter { !isWorkspaceTarget(it) && isRustTarget(it) }
graph.allTargetsAtDepth(-1, rootTargets).asSequence().filter { !isWorkspaceTarget(it, workspaceContext) && isRustTarget(it) }

private fun selectTargetsToImport(
workspaceContext: WorkspaceContext, rootTargets: Set<String>, graph: DependencyGraph
): Sequence<TargetInfo> = graph.allTargetsAtDepth(
workspaceContext.importDepth.value, rootTargets
).asSequence().filter(::isWorkspaceTarget)
).asSequence().filter { isWorkspaceTarget(it, workspaceContext) }

private fun hasKnownSources(targetInfo: TargetInfo) =
targetInfo.sourcesList.any {
Expand All @@ -406,9 +405,9 @@ class BazelProjectMapper(
it.relativePath.endsWith(".rs")
}

private fun isWorkspaceTarget(target: TargetInfo): Boolean =
private fun isWorkspaceTarget(target: TargetInfo, workspaceContext: WorkspaceContext): Boolean =
bazelInfo.release.isRelativeWorkspacePath(target.id) &&
(hasKnownSources(target) ||
(hasKnownSources(target) || !workspaceContext.experimentalUseLibOverModSection.value &&
target.kind in setOf(
"java_library",
"java_binary",
Expand Down

0 comments on commit 8d13b46

Please sign in to comment.