-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor throw -> error fixup! Typo fix fixup! Typo fix Typo fix Tiny cleanup Fix unit tests [fix] Support bazel 5 in inverse sources query WIP Inverse sources with query Co-authored-by: Xuan Son Trinh <xuanson.trinh@jetbrains.com> Merge-request: BAZEL-MR-771 Merged-by: Tomasz Pasternak <Tomasz.Pasternak@jetbrains.com>
- Loading branch information
1 parent
a2f623b
commit 26ef246
Showing
7 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
server/src/main/kotlin/org/jetbrains/bsp/bazel/server/sync/InverseSourcesQuery.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.jetbrains.bsp.bazel.server.sync | ||
|
||
import ch.epfl.scala.bsp4j.BuildTargetIdentifier | ||
import ch.epfl.scala.bsp4j.InverseSourcesResult | ||
import ch.epfl.scala.bsp4j.StatusCode | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker | ||
import org.jetbrains.bsp.bazel.bazelrunner.BazelRelease | ||
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner | ||
import java.nio.file.Path | ||
|
||
object InverseSourcesQuery { | ||
fun inverseSourcesQuery( | ||
documentRelativePath: Path, | ||
bazelRunner: BazelRunner, | ||
bazelInfo: BazelRelease, | ||
cancelChecker: CancelChecker | ||
): InverseSourcesResult { | ||
val fileLabel = fileLabel(documentRelativePath, bazelRunner, cancelChecker) | ||
?: return InverseSourcesResult( | ||
emptyList() | ||
) | ||
val listOfLabels = targetLabels(fileLabel, bazelRunner, bazelInfo, cancelChecker) | ||
return InverseSourcesResult(listOfLabels.map { BuildTargetIdentifier(it) }) | ||
} | ||
|
||
/** | ||
* @return list of targets that contain `fileLabel` in their `srcs` attribute | ||
*/ | ||
private fun targetLabels( | ||
fileLabel: String, | ||
bazelRunner: BazelRunner, | ||
bazelRelease: BazelRelease, | ||
cancelChecker: CancelChecker | ||
): List<String> { | ||
val packageLabel = fileLabel.replace(":.*".toRegex(), ":*") | ||
val consistentLabelsArg = listOfNotNull(if (bazelRelease.major >= 6) "--consistent_labels" else null) // #bazel5 | ||
val targetLabelsQuery = | ||
bazelRunner.commandBuilder().query() | ||
.withArgument("attr('srcs', ${fileLabel}, ${packageLabel})") | ||
.withArguments(consistentLabelsArg) | ||
.executeBazelCommand(null).waitAndGetResult(cancelChecker) | ||
if (targetLabelsQuery.statusCode == StatusCode.OK) { | ||
return targetLabelsQuery.stdoutLines | ||
} else { | ||
error("Could not retrieve inverse sources") | ||
} | ||
} | ||
|
||
/** | ||
* @return Bazel label corresponding to file path | ||
* | ||
* For example when you pass a relative path like "foo/Lib.kt", you will receive "//foo:Lib.kt". | ||
* Null is returned in case the file does not exist or does not belong to any target's sources list | ||
*/ | ||
private fun fileLabel( | ||
relativePath: Path, | ||
bazelRunner: BazelRunner, | ||
cancelChecker: CancelChecker | ||
): String? { | ||
val fileLabelResult = bazelRunner.commandBuilder().query().withTargets(listOf(relativePath.toString())) | ||
.executeBazelCommand(null).waitAndGetResult(cancelChecker) | ||
return if (fileLabelResult.statusCode == StatusCode.OK && fileLabelResult.stdoutLines.size == 1) { | ||
fileLabelResult.stdoutLines.first() | ||
} else if (fileLabelResult.stderrLines.first().startsWith("ERROR: no such target '")) { | ||
null | ||
} else { | ||
throw RuntimeException("Could not find file. Bazel query failed:\n ${fileLabelResult.stderrLines.joinToString { "\n" }}\n") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters