Search in sources :

Example 1 with SyncFailedException

use of com.google.idea.blaze.base.sync.SyncScope.SyncFailedException in project intellij by bazelbuild.

the class BuildPhaseSyncTask method doRun.

private void doRun(BlazeContext context) throws SyncFailedException, SyncCanceledException {
    List<TargetExpression> targets = Lists.newArrayList();
    ProjectViewSet viewSet = projectState.getProjectViewSet();
    if (syncParams.addWorkingSet() && projectState.getWorkingSet() != null) {
        Collection<TargetExpression> workingSetTargets = getWorkingSetTargets(context);
        if (!workingSetTargets.isEmpty()) {
            targets.addAll(workingSetTargets);
            printTargets(context, "working set", workingSetTargets);
        }
    }
    if (syncParams.addProjectViewTargets()) {
        ProjectTargets projectTargets = SyncProjectTargetsHelper.getProjectTargets(project, context, viewSet, projectState.getWorkspacePathResolver(), projectState.getLanguageSettings());
        if (!projectTargets.derivedTargets.isEmpty()) {
            buildStats.setTargetsDerivedFromDirectories(true);
            printTargets(context, "project view directories", projectTargets.derivedTargets);
        }
        if (!projectTargets.explicitTargets.isEmpty()) {
            printTargets(context, "project view targets", projectTargets.explicitTargets);
        }
        targets.addAll(projectTargets.getTargetsToSync());
    }
    if (!syncParams.sourceFilesToSync().isEmpty()) {
        Collection<TargetExpression> targetsFromSources = findTargetsBuildingSourceFiles(syncParams.sourceFilesToSync(), context);
        if (!targetsFromSources.isEmpty()) {
            targets.addAll(targetsFromSources);
            printTargets(context, syncParams.title() + " (targets derived from query)", targetsFromSources);
        }
    }
    if (!syncParams.targetExpressions().isEmpty()) {
        targets.addAll(syncParams.targetExpressions());
        printTargets(context, syncParams.title(), syncParams.targetExpressions());
    }
    buildStats.setTargets(targets);
    notifyBuildStarted(context, syncParams.addProjectViewTargets(), ImmutableList.copyOf(targets));
    BlazeBuildParams buildParams = BlazeBuildParams.fromProject(project);
    ShardedTargetsResult shardedTargetsResult = BlazeBuildTargetSharder.expandAndShardTargets(project, context, workspaceRoot, buildParams, viewSet, projectState.getWorkspacePathResolver(), targets);
    if (shardedTargetsResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
        throw new SyncFailedException();
    }
    ShardedTargetList shardedTargets = shardedTargetsResult.shardedTargets;
    buildStats.setSyncSharded(shardedTargets.shardCount() > 1).setShardCount(shardedTargets.shardCount()).setShardStats(shardedTargets.shardStats()).setParallelBuilds(buildParams.parallelizeBuilds());
    BlazeBuildOutputs blazeBuildResult = getBlazeBuildResult(context, viewSet, shardedTargets);
    resultBuilder.setBuildResult(blazeBuildResult);
    buildStats.setBuildResult(blazeBuildResult.buildResult).setBuildIds(blazeBuildResult.buildIds);
    if (context.isCancelled()) {
        throw new SyncCanceledException();
    }
    String invocationResultMsg = "Build invocation result: " + blazeBuildResult.buildResult.status;
    if (blazeBuildResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
        context.setHasError();
        if (blazeBuildResult.buildResult.outOfMemory()) {
            SuggestBuildShardingNotification.syncOutOfMemoryError(project, context);
        }
        context.output(PrintOutput.error(invocationResultMsg));
        throw new SyncFailedException();
    }
    context.output(PrintOutput.log(invocationResultMsg));
}
Also used : ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) ShardedTargetsResult(com.google.idea.blaze.base.sync.sharding.BlazeBuildTargetSharder.ShardedTargetsResult) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) ProjectTargets(com.google.idea.blaze.base.sync.SyncProjectTargetsHelper.ProjectTargets) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) ShardedTargetList(com.google.idea.blaze.base.sync.sharding.ShardedTargetList) BlazeBuildOutputs(com.google.idea.blaze.base.sync.aspects.BlazeBuildOutputs) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException)

Example 2 with SyncFailedException

use of com.google.idea.blaze.base.sync.SyncScope.SyncFailedException in project intellij by bazelbuild.

the class SyncPhaseCoordinator method updateProjectAndFinishSync.

private void updateProjectAndFinishSync(UpdatePhaseTask updateTask, BlazeContext context) {
    SyncStats.Builder stats = SyncStats.builder();
    SyncResult syncResult = updateTask.syncResult();
    try {
        fillInBuildStats(stats, updateTask.projectState(), updateTask.buildResult());
        if (!syncResult.successful() || !updateTask.buildResult().isValid()) {
            return;
        }
        List<TimedEvent> timedEvents = SyncScope.runWithTiming(context, childContext -> {
            ProjectTargetData targetData = updateTargetData(updateTask, childContext);
            if (targetData == null) {
                childContext.setHasError();
                throw new SyncFailedException();
            }
            ProjectUpdateSyncTask.runProjectUpdatePhase(project, updateTask.syncParams().syncMode(), updateTask.projectState(), targetData, childContext);
        }, new TimingScope("Project update phase", EventType.Other));
        stats.addTimedEvents(timedEvents);
        if (!context.shouldContinue()) {
            syncResult = context.isCancelled() ? SyncResult.CANCELLED : SyncResult.FAILURE;
        }
    } catch (Throwable e) {
        logSyncError(context, e);
        syncResult = SyncResult.FAILURE;
    } finally {
        SyncProjectState projectState = updateTask.projectState();
        finishSync(updateTask.syncParams(), updateTask.startTime(), context, projectState != null ? projectState.getProjectViewSet() : null, updateTask.buildIds(), syncResult, stats);
    }
}
Also used : TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) SyncStats(com.google.idea.blaze.base.logging.utils.SyncStats) ProjectTargetData(com.google.idea.blaze.base.model.ProjectTargetData) TimedEvent(com.google.idea.blaze.base.scope.scopes.TimingScopeListener.TimedEvent) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException)

Example 3 with SyncFailedException

use of com.google.idea.blaze.base.sync.SyncScope.SyncFailedException in project intellij by bazelbuild.

the class SyncProjectTargetsHelper method deriveTargetsFromDirectories.

private static ImmutableList<TargetExpression> deriveTargetsFromDirectories(Project project, BlazeContext context, ProjectViewSet projectViewSet, WorkspacePathResolver pathResolver, WorkspaceLanguageSettings languageSettings) throws SyncFailedException, SyncCanceledException {
    String fileBugSuggestion = Blaze.getBuildSystem(project) == BuildSystem.Bazel ? "" : " Please run 'Blaze > File a Bug'";
    if (!DirectoryToTargetProvider.hasProvider()) {
        IssueOutput.error("Can't derive targets from project directories: no query provider available." + fileBugSuggestion).submit(context);
        throw new SyncFailedException();
    }
    ImportRoots importRoots = ImportRoots.builder(project).add(projectViewSet).build();
    if (importRoots.rootDirectories().isEmpty()) {
        return ImmutableList.of();
    }
    List<TargetInfo> targets = Scope.push(context, childContext -> {
        childContext.push(new TimingScope("QueryDirectoryTargets", EventType.BlazeInvocation));
        childContext.output(new StatusOutput("Querying targets in project directories..."));
        // We don't want blaze build errors to fail the whole sync
        childContext.setPropagatesErrors(false);
        return DirectoryToTargetProvider.expandDirectoryTargets(project, importRoots, pathResolver, childContext);
    });
    if (context.isCancelled()) {
        throw new SyncCanceledException();
    }
    if (targets == null) {
        IssueOutput.error("Deriving targets from project directories failed." + fileBugSuggestion).submit(context);
        throw new SyncFailedException();
    }
    ImmutableList<TargetExpression> retained = SourceToTargetFilteringStrategy.filterTargets(targets).stream().filter(t -> t.getKind() != null && t.getKind().getLanguageClasses().stream().anyMatch(languageSettings::isLanguageActive)).map(t -> t.label).collect(toImmutableList());
    context.output(PrintOutput.log(String.format("%d targets found under project directories; syncing %d of them.", targets.size(), retained.size())));
    return retained;
}
Also used : BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) AutomaticallyDeriveTargetsSection(com.google.idea.blaze.base.projectview.section.sections.AutomaticallyDeriveTargetsSection) StatusOutput(com.google.idea.blaze.base.scope.output.StatusOutput) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) ImmutableList(com.google.common.collect.ImmutableList) Scope(com.google.idea.blaze.base.scope.Scope) IssueOutput(com.google.idea.blaze.base.scope.output.IssueOutput) DirectoryToTargetProvider(com.google.idea.blaze.base.dependencies.DirectoryToTargetProvider) Project(com.intellij.openapi.project.Project) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) WorkspacePathResolver(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver) BuildSystem(com.google.idea.blaze.base.settings.BuildSystem) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) Blaze(com.google.idea.blaze.base.settings.Blaze) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SourceToTargetFilteringStrategy(com.google.idea.blaze.base.dependencies.SourceToTargetFilteringStrategy) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) PrintOutput(com.google.idea.blaze.base.scope.output.PrintOutput) TargetSection(com.google.idea.blaze.base.projectview.section.sections.TargetSection) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException) List(java.util.List) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) StatusOutput(com.google.idea.blaze.base.scope.output.StatusOutput) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException)

Example 4 with SyncFailedException

use of com.google.idea.blaze.base.sync.SyncScope.SyncFailedException in project intellij by bazelbuild.

the class ProjectUpdateSyncTask method run.

private void run(BlazeContext context) throws SyncCanceledException, SyncFailedException {
    TargetMap targetMap = targetData.targetMap;
    RemoteOutputArtifacts oldRemoteState = RemoteOutputArtifacts.fromProjectData(oldProjectData);
    RemoteOutputArtifacts newRemoteState = targetData.remoteOutputs;
    ArtifactLocationDecoder artifactLocationDecoder = new ArtifactLocationDecoderImpl(projectState.getBlazeInfo(), projectState.getWorkspacePathResolver(), newRemoteState);
    Scope.push(context, childContext -> {
        childContext.push(new TimingScope("UpdateRemoteOutputsCache", EventType.Prefetching));
        RemoteOutputsCache.getInstance(project).updateCache(context, targetMap, projectState.getLanguageSettings(), newRemoteState, oldRemoteState, /* clearCache= */
        syncMode == SyncMode.FULL);
    });
    SyncState.Builder syncStateBuilder = new SyncState.Builder();
    Scope.push(context, childContext -> {
        childContext.push(new TimingScope("UpdateSyncState", EventType.Other));
        for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
            syncPlugin.updateSyncState(project, childContext, workspaceRoot, projectState.getProjectViewSet(), projectState.getLanguageSettings(), projectState.getBlazeVersionData(), projectState.getWorkingSet(), artifactLocationDecoder, targetMap, syncStateBuilder, oldProjectData != null ? oldProjectData.getSyncState() : null, syncMode);
        }
    });
    if (context.isCancelled()) {
        throw new SyncCanceledException();
    }
    if (context.hasErrors()) {
        throw new SyncFailedException();
    }
    BlazeProjectData newProjectData = new BlazeProjectData(targetData, projectState.getBlazeInfo(), projectState.getBlazeVersionData(), projectState.getWorkspacePathResolver(), artifactLocationDecoder, projectState.getLanguageSettings(), syncStateBuilder.build());
    FileCaches.onSync(project, context, projectState.getProjectViewSet(), newProjectData, oldProjectData, syncMode);
    ListenableFuture<?> prefetch = PrefetchService.getInstance().prefetchProjectFiles(project, projectState.getProjectViewSet(), newProjectData);
    FutureUtil.waitForFuture(context, prefetch).withProgressMessage("Prefetching files...").timed("PrefetchFiles", EventType.Prefetching).onError("Prefetch failed").run();
    ListenableFuture<DirectoryStructure> directoryStructureFuture = DirectoryStructure.getRootDirectoryStructure(project, workspaceRoot, projectState.getProjectViewSet());
    refreshVirtualFileSystem(context, project, newProjectData);
    DirectoryStructure directoryStructure = FutureUtil.waitForFuture(context, directoryStructureFuture).withProgressMessage("Computing directory structure...").timed("DirectoryStructure", EventType.Other).onError("Directory structure computation failed").run().result();
    if (directoryStructure == null) {
        throw new SyncFailedException();
    }
    boolean success = updateProject(context, projectState.getProjectViewSet(), projectState.getBlazeVersionData(), directoryStructure, oldProjectData, newProjectData);
    if (!success) {
        throw new SyncFailedException();
    }
}
Also used : RemoteOutputArtifacts(com.google.idea.blaze.base.model.RemoteOutputArtifacts) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) SyncState(com.google.idea.blaze.base.model.SyncState) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) ArtifactLocationDecoderImpl(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoderImpl) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) DirectoryStructure(com.google.idea.blaze.base.sync.projectstructure.DirectoryStructure)

Example 5 with SyncFailedException

use of com.google.idea.blaze.base.sync.SyncScope.SyncFailedException in project intellij by bazelbuild.

the class BuildPhaseSyncTask method findTargetsBuildingSourceFiles.

/**
 * Finds the list of targets to sync for the given source files. Ignores directories, and sources
 * not covered by the .bazelproject directories.
 */
private ImmutableList<TargetExpression> findTargetsBuildingSourceFiles(Collection<WorkspacePath> sources, BlazeContext context) throws SyncCanceledException, SyncFailedException {
    ImportRoots importRoots = getImportRoots();
    ImmutableList.Builder<TargetExpression> targets = ImmutableList.builder();
    ImmutableList.Builder<WorkspacePath> pathsToQuery = ImmutableList.builder();
    for (WorkspacePath source : sources) {
        File file = projectState.getWorkspacePathResolver().resolveToFile(source);
        if (FileOperationProvider.getInstance().isDirectory(file)) {
            continue;
        }
        if (!importRoots.containsWorkspacePath(source)) {
            continue;
        }
        if (Blaze.getBuildSystemProvider(project).isBuildFile(file.getName())) {
            targets.add(TargetExpression.allFromPackageNonRecursive(source.getParent()));
            continue;
        }
        pathsToQuery.add(source);
    }
    List<TargetInfo> result = Scope.push(context, childContext -> {
        childContext.push(new TimingScope("QuerySourceTargets", EventType.BlazeInvocation));
        childContext.output(new StatusOutput("Querying targets building source files..."));
        // We don't want blaze build errors to fail the whole sync
        childContext.setPropagatesErrors(false);
        return BlazeQuerySourceToTargetProvider.getTargetsBuildingSourceFiles(project, pathsToQuery.build(), childContext, ContextType.Sync);
    });
    if (context.isCancelled()) {
        throw new SyncCanceledException();
    }
    if (result == null) {
        String fileBugSuggestion = Blaze.getBuildSystem(project) == BuildSystem.Bazel ? "" : " Please run 'Blaze > File a Bug'";
        IssueOutput.error("Querying blaze targets building project source files failed." + fileBugSuggestion).submit(context);
        throw new SyncFailedException();
    }
    targets.addAll(result.stream().map(t -> t.label).collect(toImmutableList()));
    return targets.build();
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatusOutput(com.google.idea.blaze.base.scope.output.StatusOutput) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) SyncFailedException(com.google.idea.blaze.base.sync.SyncScope.SyncFailedException) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) SyncCanceledException(com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException) File(java.io.File)

Aggregations

SyncFailedException (com.google.idea.blaze.base.sync.SyncScope.SyncFailedException)7 SyncCanceledException (com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException)6 TimingScope (com.google.idea.blaze.base.scope.scopes.TimingScope)5 TargetExpression (com.google.idea.blaze.base.model.primitives.TargetExpression)4 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)4 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 TargetInfo (com.google.idea.blaze.base.dependencies.TargetInfo)2 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)2 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)2 StatusOutput (com.google.idea.blaze.base.scope.output.StatusOutput)2 EventType (com.google.idea.blaze.base.scope.scopes.TimingScope.EventType)2 Blaze (com.google.idea.blaze.base.settings.Blaze)2 ImportRoots (com.google.idea.blaze.base.sync.projectview.ImportRoots)2 WorkspaceLanguageSettings (com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings)2 WorkspacePathResolver (com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver)2 Project (com.intellij.openapi.project.Project)2 List (java.util.List)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableSet (com.google.common.collect.ImmutableSet)1