Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
[fix] Use Android flags for run and test to avoid cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
gottagofaster236 committed Apr 2, 2024
1 parent a7925e7 commit ade3e68
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.jetbrains.bsp.bazel.bazelrunner

import java.nio.file.Path
import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.logger.BspClientLogger
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContext
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContextProvider
import org.jetbrains.bsp.bazel.workspacecontext.extraFlags
import java.nio.file.Path

class BazelRunner private constructor(
private val workspaceContextProvider: WorkspaceContextProvider,
Expand Down Expand Up @@ -87,5 +88,6 @@ class BazelRunner private constructor(
}

private fun bazel(workspaceContext: WorkspaceContext): String = workspaceContext.bazelBinary.value.toString()
private fun buildFlags(workspaceContext: WorkspaceContext): List<String> = workspaceContext.buildFlags.values
private fun buildFlags(workspaceContext: WorkspaceContext): List<String> =
workspaceContext.buildFlags.values + workspaceContext.extraFlags
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kt_jvm_library(
"//executioncontext/api",
],
deps = [
"//bazelrunner:params",
"//executioncontext/api",
"//executioncontext/projectview:generator",
"//executioncontext/projectview:parser",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.bsp.bazel.workspacecontext

import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag
import org.jetbrains.bsp.bazel.executioncontext.api.ExecutionContext
import org.jetbrains.bsp.bazel.executioncontext.api.ExecutionContextConstructor
import org.jetbrains.bsp.bazel.projectview.model.ProjectView
Expand Down Expand Up @@ -103,3 +104,13 @@ class WorkspaceContextConstructor(workspaceRoot: Path) : ExecutionContextConstru

val WorkspaceContext.isAndroidEnabled: Boolean
get() = "rules_android" in enabledRules.values

val WorkspaceContext.extraFlags: List<String>
get() = if (isAndroidEnabled) {
listOf(
BazelFlag.experimentalGoogleLegacyApi(),
BazelFlag.experimentalEnableAndroidMigrationApis(),
)
} else {
emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.aspect
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.buildManualTests
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.color
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.curses
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.experimentalEnableAndroidMigrationApis
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.experimentalGoogleLegacyApi
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.keepGoing
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.outputGroups
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.repositoryOverride
Expand Down Expand Up @@ -48,7 +46,6 @@ class BazelBspAspectsManager(
targetSpecs: TargetsSpec,
aspect: String,
outputGroups: List<String>,
isAndroidEnabled: Boolean,
shouldBuildManualFlags: Boolean,
): BazelBspAspectsManagerResult {
if (targetSpecs.values.isEmpty()) return BazelBspAspectsManagerResult(BepOutput(), isFailure = false)
Expand All @@ -60,10 +57,9 @@ class BazelBspAspectsManager(
color(true),
curses(false),
)
val androidFlags = if (isAndroidEnabled) listOf(experimentalGoogleLegacyApi(), experimentalEnableAndroidMigrationApis()) else emptyList()
val buildManualTargetsFlags = if (shouldBuildManualFlags) listOf(buildManualTests()) else emptyList()

val flagsToUse = defaultFlags + androidFlags + buildManualTargetsFlags
val flagsToUse = defaultFlags + buildManualTargetsFlags

return bazelBspCompilationManager
.buildTargetsWithBep(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import ch.epfl.scala.bsp4j.BuildClient
import ch.epfl.scala.bsp4j.TextDocumentIdentifier
import org.eclipse.lsp4j.jsonrpc.CancelChecker
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.experimentalEnableAndroidMigrationApis
import org.jetbrains.bsp.bazel.bazelrunner.params.BazelFlag.experimentalGoogleLegacyApi
import org.jetbrains.bsp.bazel.server.bep.BepServer
import org.jetbrains.bsp.bazel.server.diagnostics.DiagnosticsService
import org.jetbrains.bsp.bazel.server.paths.BazelPathsResolver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jetbrains.bsp.bazel.server.sync

import ch.epfl.scala.bsp4j.BuildClient
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import ch.epfl.scala.bsp4j.CleanCacheParams
import ch.epfl.scala.bsp4j.CleanCacheResult
Expand Down Expand Up @@ -29,7 +28,6 @@ import org.jetbrains.bsp.bazel.server.bsp.managers.BazelBspCompilationManager
import org.jetbrains.bsp.bazel.server.bsp.managers.BepReader
import org.jetbrains.bsp.bazel.server.diagnostics.DiagnosticsService
import org.jetbrains.bsp.bazel.server.paths.BazelPathsResolver
import org.jetbrains.bsp.bazel.server.sync.BspMappings
import org.jetbrains.bsp.bazel.server.sync.BspMappings.toBspId
import org.jetbrains.bsp.bazel.server.sync.languages.android.AdditionalAndroidBuildTargetsProvider
import org.jetbrains.bsp.bazel.server.sync.model.Module
Expand Down Expand Up @@ -165,21 +163,13 @@ class ExecuteService(
private fun build(cancelChecker: CancelChecker, bspIds: List<BuildTargetIdentifier>, originId: String): BazelProcessResult {
val targets = bspIds + getAdditionalBuildTargets(cancelChecker, bspIds)
val targetsSpec = TargetsSpec(targets, emptyList())
val isAndroidEnabled = workspaceContextProvider.currentWorkspaceContext().isAndroidEnabled

val androidFlags = listOf(
BazelFlag.experimentalGoogleLegacyApi(),
BazelFlag.experimentalEnableAndroidMigrationApis()
)
val flagsToUse = if (isAndroidEnabled) androidFlags else emptyList()
// TODO: what if there's more than one target?
// (it was like this in now-deleted BazelBspCompilationManager.buildTargetsWithBep)
return withBepServer(originId, bspIds.firstOrNull()) { bepReader ->
bazelRunner
.commandBuilder()
.build()
.withTargets(targetsSpec)
.withFlags(flagsToUse)
.executeBazelBesCommand(originId, bepReader.eventFile.toPath().toAbsolutePath())
.waitAndGetResult(cancelChecker, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.jetbrains.bsp.bazel.server.bsp.managers.BazelExternalRulesQueryImpl
import org.jetbrains.bsp.bazel.server.sync.model.Project
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContext
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContextProvider
import org.jetbrains.bsp.bazel.workspacecontext.isAndroidEnabled

/** Responsible for querying bazel and constructing Project instance */
class ProjectResolver(
Expand Down Expand Up @@ -90,7 +89,6 @@ class ProjectResolver(
targetSpecs = workspaceContext.targets,
aspect = ASPECT_NAME,
outputGroups = outputGroups,
isAndroidEnabled = workspaceContext.isAndroidEnabled,
shouldBuildManualFlags = workspaceContext.shouldAddBuildAffectingFlags(keepDefaultOutputGroups)
)
}
Expand Down

0 comments on commit ade3e68

Please sign in to comment.