suspend fun runBuildTargetTask()

in plugin-bazel/src/main/kotlin/org/jetbrains/bazel/server/tasks/BuildTargetTask.kt [34:81]


suspend fun runBuildTargetTask(
  targetIds: List<Label>,
  project: Project,
  isDebug: Boolean,
  buildTargetTask: BuildTargetTask,
): BazelStatus {
  saveAllFiles()
  return withBackgroundProgress(project, BazelPluginBundle.message("background.progress.building.targets")) {
    // some languages require running `bazel build` with additional flags before debugging. e.g., python, c++
    // when this happens, isDebug should be set to true, and flags from "debug_flags" section of the project view file will be added
    val debugFlag = if (isDebug) project.connection.runWithServer { it.workspaceContext().debugFlags } else listOf()
    project.connection.runWithServer { server ->
      coroutineScope {
        val originId = "build-" + UUID.randomUUID().toString()
        val bspBuildConsole = ConsoleService.getInstance(project).buildConsole

        val taskListener = BazelBuildTaskListener(bspBuildConsole, originId)

        BazelTaskEventsService.getInstance(project).saveListener(originId, taskListener)

        val startBuildMessage = when (targetIds.size) {
          0 -> BazelPluginBundle.message("console.task.build.no.targets")
          1 -> BazelPluginBundle.message("console.task.build.in.progress.one", targetIds.first().toShortString(project))
          else -> BazelPluginBundle.message("console.task.build.in.progress.many", targetIds.size)
        }

        bspBuildConsole.startTask(
          taskId = originId,
          title = BazelPluginBundle.message("console.task.build.title"),
          message = startBuildMessage,
          cancelAction = { this@coroutineScope.cancel() },
          redoAction = {
            BazelCoroutineService.getInstance(project).start { runBuildTargetTask(targetIds, project, isDebug, buildTargetTask) }
          },
        )

        try {
          val buildDeferred = async { buildTargetTask.build(server, targetIds, bspBuildConsole, originId, debugFlag) }
          return@coroutineScope BspTaskStatusLogger(buildDeferred, bspBuildConsole, originId).getStatus()
        } finally {
          BazelTaskEventsService.getInstance(project).removeListener(originId)
        }
      }
    }
  }.also {
    VirtualFileManager.getInstance().asyncRefresh()
  }
}