Skip to content

Commit

Permalink
[fix] remove build_flags from non-build calls | #BAZEL-647 Done
Browse files Browse the repository at this point in the history
Merge-request: BAZEL-MR-573
Merged-by: Blazej Kardys <Blazej.Kardys@jetbrains.com>
  • Loading branch information
sellophane authored and Space Team committed Oct 20, 2023
1 parent 9b9985c commit 242995b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Expand Up @@ -14,7 +14,9 @@ class BazelInfoResolver(
}

private fun bazelInfoFromBazel(cancelChecker: CancelChecker): BazelInfo {
val processResult = bazelRunner.commandBuilder().info().executeBazelCommand().waitAndGetResult(cancelChecker,true)
val processResult = bazelRunner.commandBuilder()
.info().executeBazelCommand(useBuildFlags = false)
.waitAndGetResult(cancelChecker,true)
return parseBazelInfo(processResult).also { storage.store(it) }
}

Expand All @@ -25,10 +27,12 @@ class BazelInfoResolver(
InfoLinePattern.matchEntire(line)?.let { it.groupValues[1] to it.groupValues[2] }
}.toMap()

fun BazelProcessResult.meaningfulOutput() = if (isNotSuccess) stderr else stdout

fun extract(name: String): String =
outputMap[name]
?: error("Failed to resolve $name from bazel info in ${bazelRunner.workspaceRoot}. " +
"Bazel Info output: '${bazelProcessResult.stdout.escapeNewLines()}'")
"Bazel Info output: '${bazelProcessResult.meaningfulOutput().escapeNewLines()}'")

return BasicBazelInfo(
execRoot = extract("execution_root"),
Expand Down
Expand Up @@ -50,10 +50,12 @@ class BazelRunner private constructor(
arguments: List<String>,
originId: String?,
parseProcessOutput: Boolean,
useBuildFlags: Boolean = true,
): BazelProcess {
val workspaceContext = workspaceContextProvider.currentWorkspaceContext()
val usedBuildFlags = if (useBuildFlags) buildFlags(workspaceContext) else emptyList()
val processArgs =
listOf(bazel(workspaceContext), command) + buildFlags(workspaceContext) + flags + arguments
listOf(bazel(workspaceContext), command) + usedBuildFlags + flags + arguments
logInvocation(processArgs, originId)
val processBuilder = ProcessBuilder(processArgs)
val outputLogger = bspClientLogger.takeIf { parseProcessOutput }
Expand Down
Expand Up @@ -83,8 +83,12 @@ open class BazelRunnerBuilder internal constructor(
return this
}

fun executeBazelCommand(originId: String? = null, parseProcessOutput: Boolean = true): BazelProcess {
return bazelRunner.runBazelCommand(bazelCommand, flags, arguments, originId, parseProcessOutput)
fun executeBazelCommand(
originId: String? = null,
parseProcessOutput: Boolean = true,
useBuildFlags: Boolean = true,
): BazelProcess {
return bazelRunner.runBazelCommand(bazelCommand, flags, arguments, originId, parseProcessOutput, useBuildFlags)
}

fun executeBazelBesCommand(originId: String? = null, buildEventFile: Path): BazelProcess {
Expand Down
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.bsp.bazel.server.bsp.managers

import org.eclipse.lsp4j.jsonrpc.CancelChecker
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner
import org.jetbrains.bsp.bazel.commons.escapeNewLines
import org.w3c.dom.Document
import org.w3c.dom.NodeList
import org.xml.sax.InputSource
Expand All @@ -20,12 +21,13 @@ class BazelExternalRulesQueryImpl(private val bazelRunner: BazelRunner) : BazelE
bazelRunner.commandBuilder().query()
.withArgument("//external:*")
.withFlags(listOf("--output=xml", "--order_output=no"))
.executeBazelCommand(parseProcessOutput = false)
.waitAndGetResult(cancelChecker, ensureAllOutputRead = true)
.stdout
.readXML()
?.calculateEligibleRules()
?: listOf()
.executeBazelCommand(parseProcessOutput = false, useBuildFlags = false)
.waitAndGetResult(cancelChecker, ensureAllOutputRead = true).let { result ->
if (result.isNotSuccess)
error("Bazel query failed with output: '${result.stderr.escapeNewLines()}'")
else
result.stdout.readXML()?.calculateEligibleRules()
} ?: listOf()

private fun String.readXML(): Document? =
DocumentBuilderFactory
Expand Down

0 comments on commit 242995b

Please sign in to comment.