private static TargetMapAndInterfaceState updateTargetMap()

in base/src/com/google/idea/blaze/base/sync/aspects/BlazeIdeInterfaceAspectsImpl.java [170:285]


  private static TargetMapAndInterfaceState updateTargetMap(
      Project project,
      BlazeContext context,
      WorkspaceRoot workspaceRoot,
      SyncProjectState projectState,
      BlazeBuildOutputs buildResult,
      boolean mergeWithOldState,
      @Nullable BlazeProjectData oldProjectData) {
    // If there was a partial error, make a best-effort attempt to sync. Retain
    // any old state that we have in an attempt not to lose too much code.
    if (buildResult.buildResult.status == BuildResult.Status.BUILD_ERROR) {
      mergeWithOldState = true;
    }

    TargetMap oldTargetMap = oldProjectData != null ? oldProjectData.getTargetMap() : null;
    BlazeIdeInterfaceState prevState =
        oldProjectData != null ? oldProjectData.getTargetData().ideInterfaceState : null;

    Predicate<String> ideInfoPredicate = AspectStrategy.ASPECT_OUTPUT_FILE_PREDICATE;
    Collection<OutputArtifact> files =
        buildResult
            .getOutputGroupArtifacts(group -> group.startsWith(OutputGroup.INFO.prefix))
            .stream()
            .filter(f -> ideInfoPredicate.test(f.getKey()))
            .distinct()
            .collect(toImmutableList());

    ArtifactsDiff diff;
    try {
      diff =
          ArtifactsDiff.diffArtifacts(prevState != null ? prevState.ideInfoFileState : null, files);
    } catch (InterruptedException e) {
      throw new ProcessCanceledException(e);
    } catch (ExecutionException e) {
      IssueOutput.error("Failed to diff aspect output files: " + e).submit(context);
      return null;
    }

    // if we're merging with the old state, no files are removed
    int targetCount = files.size() + (mergeWithOldState ? diff.getRemovedOutputs().size() : 0);
    int removedCount = mergeWithOldState ? 0 : diff.getRemovedOutputs().size();

    context.output(
        PrintOutput.log(
            String.format(
                "Total rules: %d, new/changed: %d, removed: %d",
                targetCount, diff.getUpdatedOutputs().size(), removedCount)));

    ListenableFuture<?> downloadArtifactsFuture =
        RemoteArtifactPrefetcher.getInstance()
            .downloadArtifacts(
                /* projectName= */ project.getName(),
                /* outputArtifacts= */ BlazeArtifact.getRemoteArtifacts(diff.getUpdatedOutputs()));
    ListenableFuture<?> loadFilesInJvmFuture =
        RemoteArtifactPrefetcher.getInstance()
            .loadFilesInJvm(
                /* outputArtifacts= */ BlazeArtifact.getRemoteArtifacts(diff.getUpdatedOutputs()));

    if (!FutureUtil.waitForFuture(
            context, Futures.allAsList(downloadArtifactsFuture, loadFilesInJvmFuture))
        .timed("PrefetchRemoteAspectOutput", EventType.Prefetching)
        .withProgressMessage("Reading IDE info result...")
        .run()
        .success()) {
      return null;
    }

    ListenableFuture<?> fetchLocalFilesFuture =
        PrefetchService.getInstance()
            .prefetchFiles(
                /* files= */ BlazeArtifact.getLocalFiles(diff.getUpdatedOutputs()),
                /* refetchCachedFiles= */ true,
                /* fetchFileTypes= */ false);
    if (!FutureUtil.waitForFuture(context, fetchLocalFilesFuture)
        .timed("FetchAspectOutput", EventType.Prefetching)
        .withProgressMessage("Reading IDE info result...")
        .run()
        .success()) {
      return null;
    }

    ImportRoots importRoots =
        ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project))
            .add(projectState.getProjectViewSet())
            .build();

    BlazeConfigurationHandler configHandler =
        new BlazeConfigurationHandler(projectState.getBlazeInfo());
    TargetMapAndInterfaceState state =
        updateState(
            project,
            context,
            prevState,
            diff,
            configHandler,
            projectState.getBlazeVersionData(),
            projectState.getLanguageSettings(),
            importRoots,
            mergeWithOldState,
            oldTargetMap);
    if (state == null) {
      return null;
    }
    // prefetch ide-resolve genfiles
    Scope.push(
        context,
        childContext -> {
          childContext.push(new TimingScope("GenfilesPrefetchBuildArtifacts", EventType.Other));
          ImmutableList<OutputArtifact> resolveOutputs =
              ImmutableList.copyOf(
                  buildResult.getOutputGroupArtifacts(
                      group -> group.startsWith(OutputGroup.RESOLVE.prefix)));
          prefetchGenfiles(context, resolveOutputs);
        });
    return state;
  }