private async resolveTargets()

in src/test-explorer/resolver.ts [157:248]


  private async resolveTargets(
    parentTest: vscode.TestItem,
    cancellationToken?: vscode.CancellationToken
  ) {
    // Request available targets from the build server.
    updateDescription(
      parentTest,
      'Loading: waiting for build server connection'
    )
    parentTest.error = undefined
    const conn = await this.buildServer.getConnection()

    updateDescription(parentTest, 'Loading: fetching available targets')
    let result: bsp.WorkspaceBuildTargetsResult
    try {
      result = await conn.sendRequest(
        bsp.WorkspaceBuildTargets.type,
        cancellationToken
      )
    } catch (e) {
      if (e.code === CANCEL_ERROR_CODE) {
        updateDescription(
          parentTest,
          'Refresh Canceled: Contents may be outdated.'
        )
        return
      }
      updateDescription(parentTest, 'Error: unable to fetch targets')
      throw e
    }

    updateDescription(parentTest, 'Loading: processing target results')

    // Process the returned targets, create new test items, and store their metadata.
    const directories = new Map<string, vscode.TestItem>()
    const buildFileName = getExtensionSetting(SettingName.BUILD_FILE_NAME)
    parentTest.children.replace([])
    this.store.clearTargetIdentifiers()
    this.store.knownFiles.clear()

    result.targets.forEach(target => {
      if (!target.capabilities.canTest) return

      let relevantParent = parentTest
      if (target.baseDirectory) {
        const pathSegmentTestItems =
          this.testItemFactory.createPathSegmentTestItems(
            directories,
            target.baseDirectory
          )
        parentTest.children.add(pathSegmentTestItems.rootTestItem)
        relevantParent = pathSegmentTestItems.baseTestItem
      }

      const buildFileUri =
        target.baseDirectory && buildFileName
          ? vscode.Uri.parse(path.join(target.baseDirectory, buildFileName))
          : undefined
      // UI will group runs that have the same ID.
      const newTest = this.testItemFactory.createBuildTargetTestItem(
        target,
        buildFileUri
      )
      relevantParent.children.add(newTest)
      this.store.setTargetIdentifier(target.id, newTest)
    })

    // If no test items were added, show the getting started message as a test message overlaid on the .bazelproject file.
    // This provides an opportunity for the user to make adjustments and re-sync.
    if (parentTest.children.size === 0) {
      // Dummy test run that overlays the getting started message on the .bazelproject file.
      const run = this.store.testController.createTestRun(
        new vscode.TestRunRequest()
      )
      run.errored(parentTest, new vscode.TestMessage(gettingStartedMessage))
      run.end()

      // Link in the test explorer tree to set up the project.
      const encodedFileUri = encodeURIComponent(JSON.stringify(parentTest.uri))
      const syncFailMessage = new vscode.MarkdownString(
        `No test targets found. [Setup your project](command:vscode.open?${encodedFileUri})`
      )
      syncFailMessage.isTrusted = true
      parentTest.error = syncFailMessage
    }

    // Replace all children with the newly returned test cases.
    this.condenseTestItems(parentTest)
    updateDescription(parentTest)

    await this.resolveOpenSourceFiles()
  }