Search in sources :

Example 26 with TargetExpression

use of com.google.idea.blaze.base.model.primitives.TargetExpression in project intellij by bazelbuild.

the class WildcardTargetPatternTest method testRecursiveWildcardPatternAlternativeFormat.

@Test
public void testRecursiveWildcardPatternAlternativeFormat() {
    TargetExpression target = TargetExpression.fromStringSafe("//java/com/google/...:all");
    WildcardTargetPattern wildcardPattern = WildcardTargetPattern.fromExpression(target);
    assertThat(wildcardPattern).isNotNull();
    assertThat(wildcardPattern.coversPackage(new WorkspacePath("java/com/google"))).isTrue();
    assertThat(wildcardPattern.coversPackage(new WorkspacePath("java/com/google/foo"))).isTrue();
    assertThat(wildcardPattern.isRecursive()).isTrue();
    assertThat(wildcardPattern.getBasePackage()).isEqualTo(new WorkspacePath("java/com/google"));
    assertThat(wildcardPattern.rulesOnly()).isTrue();
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) WildcardTargetPattern(com.google.idea.blaze.base.model.primitives.WildcardTargetPattern) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) Test(org.junit.Test)

Example 27 with TargetExpression

use of com.google.idea.blaze.base.model.primitives.TargetExpression in project intellij by bazelbuild.

the class BlazeSyncTask method doSyncProject.

/**
 * @return true if sync successfully completed
 */
private SyncResult doSyncProject(BlazeContext context, @Nullable BlazeProjectData oldBlazeProjectData) {
    long syncStartTime = System.currentTimeMillis();
    if (!FileOperationProvider.getInstance().exists(workspaceRoot.directory())) {
        IssueOutput.error(String.format("Workspace '%s' doesn't exist.", workspaceRoot.directory())).submit(context);
        return SyncResult.FAILURE;
    }
    BlazeVcsHandler vcsHandler = BlazeVcsHandler.vcsHandlerForProject(project);
    if (vcsHandler == null) {
        IssueOutput.error("Could not find a VCS handler").submit(context);
        return SyncResult.FAILURE;
    }
    ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();
    WorkspacePathResolverAndProjectView workspacePathResolverAndProjectView = computeWorkspacePathResolverAndProjectView(context, vcsHandler, executor);
    if (workspacePathResolverAndProjectView == null) {
        return SyncResult.FAILURE;
    }
    ProjectViewSet projectViewSet = workspacePathResolverAndProjectView.projectViewSet;
    List<String> syncFlags = BlazeFlags.blazeFlags(project, projectViewSet, BlazeCommandName.INFO, BlazeInvocationContext.Sync);
    syncStats.setSyncFlags(syncFlags);
    ListenableFuture<BlazeInfo> blazeInfoFuture = BlazeInfoRunner.getInstance().runBlazeInfo(context, importSettings.getBuildSystem(), Blaze.getBuildSystemProvider(project).getSyncBinaryPath(), workspaceRoot, syncFlags);
    ListenableFuture<WorkingSet> workingSetFuture = vcsHandler.getWorkingSet(project, context, workspaceRoot, executor);
    BlazeInfo blazeInfo = FutureUtil.waitForFuture(context, blazeInfoFuture).timed(Blaze.buildSystemName(project) + "Info", EventType.BlazeInvocation).withProgressMessage(String.format("Running %s info...", Blaze.buildSystemName(project))).onError(String.format("Could not run %s info", Blaze.buildSystemName(project))).run().result();
    if (blazeInfo == null) {
        return SyncResult.FAILURE;
    }
    BlazeVersionData blazeVersionData = BlazeVersionData.build(importSettings.getBuildSystem(), workspaceRoot, blazeInfo);
    if (!BuildSystemVersionChecker.verifyVersionSupported(context, blazeVersionData)) {
        return SyncResult.FAILURE;
    }
    WorkspacePathResolver workspacePathResolver = workspacePathResolverAndProjectView.workspacePathResolver;
    ArtifactLocationDecoder artifactLocationDecoder = new ArtifactLocationDecoderImpl(blazeInfo, workspacePathResolver);
    WorkspaceLanguageSettings workspaceLanguageSettings = LanguageSupport.createWorkspaceLanguageSettings(projectViewSet);
    syncStats.setLanguagesActive(new ArrayList<>(workspaceLanguageSettings.activeLanguages));
    syncStats.setWorkspaceType(workspaceLanguageSettings.getWorkspaceType());
    for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
        syncPlugin.installSdks(context);
    }
    if (!ProjectViewVerifier.verifyProjectView(project, context, workspacePathResolver, projectViewSet, workspaceLanguageSettings)) {
        return SyncResult.FAILURE;
    }
    final BlazeProjectData newBlazeProjectData;
    WorkingSet workingSet = FutureUtil.waitForFuture(context, workingSetFuture).timed("WorkingSet", EventType.Other).withProgressMessage("Computing VCS working set...").onError("Could not compute working set").run().result();
    if (context.isCancelled()) {
        return SyncResult.CANCELLED;
    }
    if (context.hasErrors()) {
        return SyncResult.FAILURE;
    }
    if (workingSet != null) {
        printWorkingSet(context, workingSet);
    }
    SyncState.Builder syncStateBuilder = new SyncState.Builder();
    SyncState previousSyncState = oldBlazeProjectData != null ? oldBlazeProjectData.syncState : null;
    List<TargetExpression> targets = Lists.newArrayList();
    if (syncParams.addProjectViewTargets) {
        Collection<TargetExpression> projectViewTargets = projectViewSet.listItems(TargetSection.KEY);
        if (!projectViewTargets.isEmpty()) {
            syncStats.setBlazeProjectTargets(new ArrayList<>(projectViewTargets));
            targets.addAll(projectViewTargets);
            printTargets(context, "project view", projectViewTargets);
        }
    }
    if (syncParams.addWorkingSet && workingSet != null) {
        Collection<? extends TargetExpression> workingSetTargets = getWorkingSetTargets(projectViewSet, workingSet);
        if (!workingSetTargets.isEmpty()) {
            targets.addAll(workingSetTargets);
            syncStats.setWorkingSetTargets(new ArrayList<>(workingSetTargets));
            printTargets(context, "working set", workingSetTargets);
        }
    }
    if (!syncParams.targetExpressions.isEmpty()) {
        targets.addAll(syncParams.targetExpressions);
        printTargets(context, syncParams.title, syncParams.targetExpressions);
    }
    ShardedTargetsResult shardedTargetsResult = BlazeBuildTargetSharder.expandAndShardTargets(project, context, workspaceRoot, projectViewSet, workspacePathResolver, targets);
    if (shardedTargetsResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
        return SyncResult.FAILURE;
    }
    ShardedTargetList shardedTargets = shardedTargetsResult.shardedTargets;
    syncStats.setSyncSharded(shardedTargets.shardedTargets.size() > 1);
    BlazeConfigurationHandler configHandler = new BlazeConfigurationHandler(blazeInfo);
    boolean mergeWithOldState = !syncParams.addProjectViewTargets;
    BlazeIdeInterface.IdeResult ideQueryResult = getIdeQueryResult(project, context, projectViewSet, blazeVersionData, configHandler, shardedTargets, workspaceLanguageSettings, artifactLocationDecoder, syncStateBuilder, previousSyncState, mergeWithOldState);
    if (context.isCancelled()) {
        return SyncResult.CANCELLED;
    }
    context.output(PrintOutput.log("ide-query result: " + ideQueryResult.buildResult.status));
    if (ideQueryResult.targetMap == null || ideQueryResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
        context.setHasError();
        if (ideQueryResult.buildResult.outOfMemory()) {
            SuggestBuildShardingNotification.syncOutOfMemoryError(project, context);
        }
        return SyncResult.FAILURE;
    }
    TargetMap targetMap = ideQueryResult.targetMap;
    context.output(PrintOutput.log("Target map size: " + ideQueryResult.targetMap.targets().size()));
    BuildResult ideInfoResult = ideQueryResult.buildResult;
    ListenableFuture<ImmutableMultimap<TargetKey, TargetKey>> reverseDependenciesFuture = BlazeExecutor.getInstance().submit(() -> ReverseDependencyMap.createRdepsMap(targetMap));
    BuildResult ideResolveResult = resolveIdeArtifacts(project, context, workspaceRoot, projectViewSet, blazeVersionData, workspaceLanguageSettings, shardedTargets);
    if (ideResolveResult.status == BuildResult.Status.FATAL_ERROR) {
        context.setHasError();
        if (ideResolveResult.outOfMemory()) {
            SuggestBuildShardingNotification.syncOutOfMemoryError(project, context);
        }
        return SyncResult.FAILURE;
    }
    if (context.isCancelled()) {
        return SyncResult.CANCELLED;
    }
    Scope.push(context, (childContext) -> {
        childContext.push(new TimingScope("UpdateSyncState", EventType.Other));
        for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
            syncPlugin.updateSyncState(project, childContext, workspaceRoot, projectViewSet, workspaceLanguageSettings, blazeInfo, workingSet, workspacePathResolver, artifactLocationDecoder, targetMap, syncStateBuilder, previousSyncState);
        }
    });
    ImmutableMultimap<TargetKey, TargetKey> reverseDependencies = FutureUtil.waitForFuture(context, reverseDependenciesFuture).timed("ReverseDependencies", EventType.Other).onError("Failed to compute reverse dependency map").run().result();
    if (reverseDependencies == null) {
        return SyncResult.FAILURE;
    }
    newBlazeProjectData = new BlazeProjectData(syncStartTime, targetMap, blazeInfo, blazeVersionData, workspacePathResolver, artifactLocationDecoder, workspaceLanguageSettings, syncStateBuilder.build(), reverseDependencies);
    FileCaches.onSync(project, context, projectViewSet, newBlazeProjectData, syncParams.syncMode);
    ListenableFuture<?> prefetch = PrefetchService.getInstance().prefetchProjectFiles(project, projectViewSet, newBlazeProjectData);
    FutureUtil.waitForFuture(context, prefetch).withProgressMessage("Prefetching files...").timed("PrefetchFiles", EventType.Prefetching).onError("Prefetch failed").run();
    ListenableFuture<DirectoryStructure> directoryStructureFuture = DirectoryStructure.getRootDirectoryStructure(project, workspaceRoot, projectViewSet);
    refreshVirtualFileSystem(context, newBlazeProjectData);
    DirectoryStructure directoryStructure = FutureUtil.waitForFuture(context, directoryStructureFuture).withProgressMessage("Computing directory structure...").timed("DirectoryStructure", EventType.Other).onError("Directory structure computation failed").run().result();
    if (directoryStructure == null) {
        return SyncResult.FAILURE;
    }
    boolean success = updateProject(context, projectViewSet, blazeVersionData, directoryStructure, oldBlazeProjectData, newBlazeProjectData);
    if (!success) {
        return SyncResult.FAILURE;
    }
    SyncResult syncResult = SyncResult.SUCCESS;
    if (ideInfoResult.status == BuildResult.Status.BUILD_ERROR || ideResolveResult.status == BuildResult.Status.BUILD_ERROR) {
        final String errorType = ideInfoResult.status == BuildResult.Status.BUILD_ERROR ? "BUILD file errors" : "compilation errors";
        String message = String.format("Sync was successful, but there were %s. " + "The project may not be fully updated or resolve until fixed. " + "If the errors are from your working set, please uncheck " + "'Blaze > Sync > Expand Sync to Working Set' and try again.", errorType);
        context.output(PrintOutput.error(message));
        IssueOutput.warn(message).submit(context);
        syncResult = SyncResult.PARTIAL_SUCCESS;
    }
    return syncResult;
}
Also used : ShardedTargetsResult(com.google.idea.blaze.base.sync.sharding.BlazeBuildTargetSharder.ShardedTargetsResult) Builder(com.google.idea.blaze.base.model.SyncState.Builder) BlazeIdeInterface(com.google.idea.blaze.base.sync.aspects.BlazeIdeInterface) Builder(com.google.idea.blaze.base.model.SyncState.Builder) BlazeVersionData(com.google.idea.blaze.base.model.BlazeVersionData) BlazeConfigurationHandler(com.google.idea.blaze.base.command.info.BlazeConfigurationHandler) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) SyncState(com.google.idea.blaze.base.model.SyncState) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) WorkspacePathResolver(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver) ArtifactLocationDecoderImpl(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoderImpl) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) WorkingSet(com.google.idea.blaze.base.sync.workspace.WorkingSet) SyncResult(com.google.idea.blaze.base.sync.SyncListener.SyncResult) DirectoryStructure(com.google.idea.blaze.base.sync.projectstructure.DirectoryStructure) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) ShardedTargetList(com.google.idea.blaze.base.sync.sharding.ShardedTargetList) BlazeInfo(com.google.idea.blaze.base.command.info.BlazeInfo) BuildResult(com.google.idea.blaze.base.sync.aspects.BuildResult) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) BlazeVcsHandler(com.google.idea.blaze.base.vcs.BlazeVcsHandler)

Example 28 with TargetExpression

use of com.google.idea.blaze.base.model.primitives.TargetExpression in project intellij by bazelbuild.

the class PartialSyncAction method getTargets.

private static List<TargetExpression> getTargets(Project project, @Nullable VirtualFile virtualFile) {
    if (virtualFile == null) {
        return ImmutableList.of();
    }
    List<TargetExpression> targets = Lists.newArrayList();
    WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
    SourceToTargetMap.getInstance(project);
    if (!virtualFile.isDirectory()) {
        targets.addAll(SourceToTargetMap.getInstance(project).getTargetsToBuildForSourceFile(new File(virtualFile.getPath())));
    }
    if (targets.isEmpty()) {
        ProjectViewSet projectViewSet = ProjectViewManager.getInstance(project).getProjectViewSet();
        if (projectViewSet != null) {
            BuildSystem buildSystem = Blaze.getBuildSystem(project);
            ImportRoots importRoots = ImportRoots.builder(workspaceRoot, buildSystem).add(projectViewSet).build();
            BuildTargetFinder buildTargetFinder = new BuildTargetFinder(project, workspaceRoot, importRoots);
            TargetExpression targetExpression = buildTargetFinder.findTargetForFile(new File(virtualFile.getPath()));
            if (targetExpression != null) {
                targets.add(targetExpression);
            }
        }
    }
    return targets;
}
Also used : ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) BuildSystem(com.google.idea.blaze.base.settings.Blaze.BuildSystem) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) BuildTargetFinder(com.google.idea.blaze.base.sync.BuildTargetFinder)

Example 29 with TargetExpression

use of com.google.idea.blaze.base.model.primitives.TargetExpression in project intellij by bazelbuild.

the class PackageLister method expandPackageTargets.

/**
 * Expands all-in-package-recursive wildcard targets into all-in-single-package targets by
 * traversing the file system, looking for child blaze packages.
 *
 * <p>Returns null if directory traversal failed or was cancelled.
 */
@Nullable
static Map<TargetExpression, List<TargetExpression>> expandPackageTargets(BuildSystemProvider provider, BlazeContext context, WorkspacePathResolver pathResolver, Collection<WildcardTargetPattern> wildcardPatterns) {
    List<ListenableFuture<Entry<TargetExpression, List<TargetExpression>>>> futures = Lists.newArrayList();
    for (WildcardTargetPattern pattern : wildcardPatterns) {
        if (!pattern.isRecursive() || pattern.toString().startsWith("-")) {
            continue;
        }
        File dir = pathResolver.resolveToFile(pattern.getBasePackage());
        if (!FileOperationProvider.getInstance().isDirectory(dir)) {
            continue;
        }
        futures.add(FetchExecutor.EXECUTOR.submit(() -> {
            List<TargetExpression> expandedTargets = new ArrayList<>();
            traversePackageRecursively(provider, pathResolver, dir, expandedTargets);
            return Maps.immutableEntry(pattern.originalPattern, expandedTargets);
        }));
    }
    if (futures.isEmpty()) {
        return ImmutableMap.of();
    }
    FutureResult<List<Entry<TargetExpression, List<TargetExpression>>>> result = FutureUtil.waitForFuture(context, Futures.allAsList(futures)).withProgressMessage("Expanding wildcard target patterns...").timed("ExpandWildcardTargets", EventType.Other).onError("Expanding wildcard target patterns failed").run();
    if (!result.success()) {
        return null;
    }
    return result.result().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> x));
}
Also used : BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Map(java.util.Map) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) WorkspacePathResolver(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver) Nullable(javax.annotation.Nullable) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) WorkspacePathUtil(com.google.idea.blaze.base.util.WorkspacePathUtil) Collection(java.util.Collection) BuildSystemProvider(com.google.idea.blaze.base.bazel.BuildSystemProvider) FutureUtil(com.google.idea.blaze.base.async.FutureUtil) FutureResult(com.google.idea.blaze.base.async.FutureUtil.FutureResult) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) WildcardTargetPattern(com.google.idea.blaze.base.model.primitives.WildcardTargetPattern) Entry(java.util.Map.Entry) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FetchExecutor(com.google.idea.blaze.base.prefetch.FetchExecutor) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) WildcardTargetPattern(com.google.idea.blaze.base.model.primitives.WildcardTargetPattern) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 30 with TargetExpression

use of com.google.idea.blaze.base.model.primitives.TargetExpression in project intellij by bazelbuild.

the class WildcardTargetExpander method queryString.

private static String queryString(List<TargetExpression> targets) {
    StringBuilder builder = new StringBuilder();
    for (TargetExpression target : targets) {
        boolean excluded = target.isExcluded();
        if (builder.length() == 0) {
            if (excluded) {
                // an excluded target at the start of the list has no effect
                continue;
            }
            builder.append(target);
        } else {
            if (excluded) {
                builder.append(" - ");
                // trim leading '-'
                String excludedTarget = target.toString();
                builder.append(excludedTarget, 1, excludedTarget.length());
            } else {
                builder.append(" + ");
                builder.append(target);
            }
        }
    }
    return builder.toString();
}
Also used : TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression)

Aggregations

TargetExpression (com.google.idea.blaze.base.model.primitives.TargetExpression)31 Test (org.junit.Test)11 WildcardTargetPattern (com.google.idea.blaze.base.model.primitives.WildcardTargetPattern)10 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)8 File (java.io.File)6 Label (com.google.idea.blaze.base.model.primitives.Label)5 ImmutableList (com.google.common.collect.ImmutableList)3 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)3 BlazeCommandRunConfigurationCommonState (com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 FutureUtil (com.google.idea.blaze.base.async.FutureUtil)2 BuildSystemProvider (com.google.idea.blaze.base.bazel.BuildSystemProvider)2 BlazeCommand (com.google.idea.blaze.base.command.BlazeCommand)2 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)2 Kind (com.google.idea.blaze.base.model.primitives.Kind)2