use of com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException 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));
BuildInvoker localInvoker = buildSystem.getBuildInvoker(project, context);
ShardedTargetsResult shardedTargetsResult = BlazeBuildTargetSharder.expandAndShardTargets(project, context, workspaceRoot, viewSet, projectState.getWorkspacePathResolver(), targets, localInvoker, buildSystem.getSyncStrategy(project));
if (shardedTargetsResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
throw new SyncFailedException();
}
ShardedTargetList shardedTargets = shardedTargetsResult.shardedTargets;
boolean parallel;
SyncStrategy strategy = buildSystem.getSyncStrategy(project);
switch(strategy) {
case PARALLEL:
parallel = true;
break;
case DECIDE_AUTOMATICALLY:
parallel = shardedTargets.shardCount() > 1;
break;
case SERIAL:
parallel = false;
break;
default:
throw new IllegalStateException("Invalid sync strategy: " + strategy);
}
if (!shardedTargetsResult.shardedTargets.shardStats().shardingApproach().equals(ShardingApproach.PARTITION_WITHOUT_EXPANDING)) {
int targetCount = shardedTargets.shardStats().actualTargetSizePerShard().stream().mapToInt(Integer::intValue).sum();
printShardingSummary(context, targetCount, shardedTargets.shardCount(), parallel);
}
BuildInvoker syncBuildInvoker = null;
if (parallel) {
syncBuildInvoker = buildSystem.getParallelBuildInvoker(project, context).orElse(null);
}
if (syncBuildInvoker == null) {
syncBuildInvoker = localInvoker;
}
resultBuilder.setBlazeInfo(syncBuildInvoker.getBlazeInfo());
buildStats.setSyncSharded(shardedTargets.shardCount() > 1).setShardCount(shardedTargets.shardCount()).setShardStats(shardedTargets.shardStats()).setParallelBuilds(syncBuildInvoker.supportsParallelism());
BlazeBuildOutputs blazeBuildResult = getBlazeBuildResult(context, viewSet, shardedTargets, syncBuildInvoker);
resultBuilder.setBuildResult(blazeBuildResult);
buildStats.setBuildResult(blazeBuildResult.buildResult).setBuildIds(blazeBuildResult.buildIds).setBuildBinaryType(syncBuildInvoker.getType()).setBepBytesConsumed(blazeBuildResult.bepBytesConsumed);
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));
}
use of com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException in project intellij by bazelbuild.
the class BlazeBuildService method buildProject.
public void buildProject() {
if (!Blaze.isBlazeProject(project)) {
return;
}
ProjectViewSet projectView = ProjectViewManager.getInstance(project).getProjectViewSet();
BlazeProjectData projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (projectView == null || projectData == null) {
return;
}
ScopedFunction<List<TargetExpression>> targets = context -> {
try {
return SyncProjectTargetsHelper.getProjectTargets(project, context, projectView, projectData.getWorkspacePathResolver(), projectData.getWorkspaceLanguageSettings()).getTargetsToSync();
} catch (SyncCanceledException e) {
context.setCancelled();
return null;
} catch (SyncFailedException e) {
context.setHasError();
return null;
}
};
buildTargetExpressions(project, projectView, projectData, targets, new NotificationScope(project, "Make", "Make project", "Make project completed successfully", "Make project failed"), "Make project", buildSystem);
// In case the user touched a file, but didn't change its content. The user will get a false
// positive for class file out of date. We need a way for the user to suppress the false
// message. Clicking the "build project" link should at least make the message go away.
project.putUserData(PROJECT_LAST_BUILD_TIMESTAMP_KEY, System.currentTimeMillis());
}
use of com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException 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(blazeInfo, 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, blazeInfo, projectState.getBlazeVersionData(), projectState.getWorkspacePathResolver(), artifactLocationDecoder, projectState.getLanguageSettings(), syncStateBuilder.build());
FileCaches.onSync(project, context, projectState.getProjectViewSet(), newProjectData, oldProjectData, syncMode);
ListenableFuture<PrefetchStats> prefetch = PrefetchService.getInstance().prefetchProjectFiles(project, projectState.getProjectViewSet(), newProjectData);
FutureResult<PrefetchStats> result = FutureUtil.waitForFuture(context, prefetch).withProgressMessage("Prefetching files...").timed("PrefetchFiles", EventType.Prefetching).onError("Prefetch failed").run();
if (result.success()) {
long prefetched = result.result().bytesPrefetched();
if (prefetched > 0) {
context.output(new NetworkTrafficUsedOutput(prefetched, "prefetch"));
}
}
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();
}
}
use of com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException 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.getBuildSystemName(project) == BuildSystemName.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;
}
use of com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException 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.getBuildSystemName(project) == BuildSystemName.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();
}
Aggregations