private static void handleSyncResult(
    @NotNull String externalProjectPath,
    @NotNull ImportSpec importSpec,
    @NotNull ExternalSystemResolveProjectTask resolveProjectTask,
    @NotNull BuildEventDispatcher eventDispatcher,
    @NotNull Ref<Supplier<? extends FinishBuildEvent>> finishSyncEventSupplier
  ) {
    var project = importSpec.getProject();
    var taskId = resolveProjectTask.getId();
    var externalSystemId = taskId.getProjectSystemId();
    var isPreviewMode = importSpec.isPreviewMode();
    var callback = importSpec.getCallback();

    if (project.isDisposed()) return;

    try {
      var error = resolveProjectTask.getError();
      if (error == null) {
        if (callback != null) {
          var externalProjectData = ProjectDataManagerImpl.getInstance()
            .getExternalProjectData(project, externalSystemId, externalProjectPath);
          if (externalProjectData != null) {
            var externalProject = externalProjectData.getExternalProjectStructure();
            if (externalProject != null && importSpec.shouldCreateDirectoriesForEmptyContentRoots()) {
              externalProject.putUserData(ContentRootDataService.CREATE_EMPTY_DIRECTORIES, Boolean.TRUE);
            }
            callback.onSuccess(taskId, externalProject);
          }
        }
        if (!isPreviewMode) {
          var externalSystemTaskActivator = ExternalProjectsManagerImpl.getInstance(project).getTaskActivator();
          externalSystemTaskActivator.runTasks(externalProjectPath, ExternalSystemTaskActivator.Phase.AFTER_SYNC);
        }
        return;
      }
      if (error instanceof ImportCanceledException) {
        // stop refresh task
        return;
      }

      if (callback != null) {
        var message = ExternalSystemApiUtil.buildErrorMessage(error);
        if (StringUtil.isEmpty(message)) {
          var systemName = externalSystemId.getReadableName();
          message = String.format("Can't resolve %s project at '%s'. Reason: %s", systemName, externalProjectPath, message);
        }
        callback.onFailure(taskId, message, extractDetails(error));
      }
    }
    catch (Throwable t) {
      finishSyncEventSupplier.set(() -> {
        var eventTime = System.currentTimeMillis();
        var eventMessage = BuildBundle.message("build.status.failed");
        var systemName = externalSystemId.getReadableName();
        var projectName = resolveProjectTask.getProjectName();
        var title = ExternalSystemBundle.message("notification.project.refresh.fail.title", systemName, projectName);
        var eventResult = createFailureResult(title, t, externalSystemId, project, externalProjectPath, DataContext.EMPTY_CONTEXT);
        return new FinishBuildEventImpl(taskId, null, eventTime, eventMessage, eventResult);
      });
    }
    finally {
      if (!isPreviewMode) {
        if (isNewProject(project)) {
          var virtualFile = VfsUtil.findFileByIoFile(new File(externalProjectPath), false);
          if (virtualFile != null) {
            VfsUtil.markDirtyAndRefresh(true, false, true, virtualFile);
          }
        }
        project.putUserData(ExternalSystemDataKeys.NEWLY_CREATED_PROJECT, null);
        project.putUserData(ExternalSystemDataKeys.NEWLY_IMPORTED_PROJECT, null);
        eventDispatcher.onEvent(taskId, getSyncFinishEvent(taskId, finishSyncEventSupplier));
      }
    }
  }
private static void handleSyncResult(
    @NotNull String externalProjectPath,
    @NotNull ImportSpec importSpec,
    @NotNull ExternalSystemResolveProjectTask resolveProjectTask,
    @NotNull BuildEventDispatcher eventDispatcher,
    @NotNull Ref<Supplier<? extends FinishBuildEvent>> finishSyncEventSupplier
  )