Search in sources :

Example 41 with TargetKey

use of com.google.idea.blaze.base.ideinfo.TargetKey in project intellij by bazelbuild.

the class JdepsFileReader method loadJdepsFiles.

/**
 * Loads any updated jdeps files since the last invocation of this method.
 */
@Nullable
public JdepsMap loadJdepsFiles(Project project, BlazeContext parentContext, ArtifactLocationDecoder artifactLocationDecoder, Iterable<TargetIdeInfo> targetsToLoad, SyncState.Builder syncStateBuilder, @Nullable SyncState previousSyncState) {
    JdepsState oldState = previousSyncState != null ? previousSyncState.get(JdepsState.class) : null;
    JdepsState jdepsState = Scope.push(parentContext, (context) -> {
        context.push(new TimingScope("LoadJdepsFiles", EventType.Other));
        return doLoadJdepsFiles(project, context, artifactLocationDecoder, oldState, targetsToLoad);
    });
    if (jdepsState == null) {
        return null;
    }
    syncStateBuilder.put(JdepsState.class, jdepsState);
    return targetKey -> jdepsState.targetToJdeps.get(targetKey);
}
Also used : BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) PrefetchService(com.google.idea.blaze.base.prefetch.PrefetchService) Callable(java.util.concurrent.Callable) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) Lists(com.google.common.collect.Lists) Scope(com.google.idea.blaze.base.scope.Scope) Map(java.util.Map) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) Nullable(javax.annotation.Nullable) FileDiffer(com.google.idea.blaze.base.filecache.FileDiffer) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ImmutableMap(com.google.common.collect.ImmutableMap) FutureUtil(com.google.idea.blaze.base.async.FutureUtil) SyncState(com.google.idea.blaze.base.model.SyncState) FileInputStream(java.io.FileInputStream) PrintOutput(com.google.idea.blaze.base.scope.output.PrintOutput) Maps(com.google.common.collect.Maps) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) ExecutionException(java.util.concurrent.ExecutionException) BlazeExecutor(com.google.idea.blaze.base.async.executor.BlazeExecutor) AtomicLong(java.util.concurrent.atomic.AtomicLong) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Deps(com.google.devtools.build.lib.view.proto.Deps) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) InputStream(java.io.InputStream) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) Nullable(javax.annotation.Nullable)

Example 42 with TargetKey

use of com.google.idea.blaze.base.ideinfo.TargetKey in project intellij by bazelbuild.

the class JdepsFileReader method doLoadJdepsFiles.

private JdepsState doLoadJdepsFiles(Project project, BlazeContext context, ArtifactLocationDecoder artifactLocationDecoder, @Nullable JdepsState oldState, Iterable<TargetIdeInfo> targetsToLoad) {
    JdepsState state = new JdepsState();
    if (oldState != null) {
        state.targetToJdeps = Maps.newHashMap(oldState.targetToJdeps);
        state.fileToTargetMap = Maps.newHashMap(oldState.fileToTargetMap);
    }
    Map<File, TargetKey> fileToTargetMap = Maps.newHashMap();
    for (TargetIdeInfo target : targetsToLoad) {
        assert target != null;
        JavaIdeInfo javaIdeInfo = target.javaIdeInfo;
        if (javaIdeInfo != null) {
            ArtifactLocation jdepsFile = javaIdeInfo.jdepsFile;
            if (jdepsFile != null) {
                fileToTargetMap.put(artifactLocationDecoder.decode(jdepsFile), target.key);
            }
        }
    }
    List<File> updatedFiles = Lists.newArrayList();
    List<File> removedFiles = Lists.newArrayList();
    state.fileState = FileDiffer.updateFiles(oldState != null ? oldState.fileState : null, fileToTargetMap.keySet(), updatedFiles, removedFiles);
    ListenableFuture<?> fetchFuture = PrefetchService.getInstance().prefetchFiles(project, updatedFiles, true, false);
    if (!FutureUtil.waitForFuture(context, fetchFuture).timed("FetchJdeps", EventType.Prefetching).withProgressMessage("Reading jdeps files...").run().success()) {
        return null;
    }
    for (File removedFile : removedFiles) {
        TargetKey targetKey = state.fileToTargetMap.remove(removedFile);
        if (targetKey != null) {
            state.targetToJdeps.remove(targetKey);
        }
    }
    AtomicLong totalSizeLoaded = new AtomicLong(0);
    List<ListenableFuture<Result>> futures = Lists.newArrayList();
    for (File updatedFile : updatedFiles) {
        futures.add(submit(() -> {
            totalSizeLoaded.addAndGet(updatedFile.length());
            try (InputStream inputStream = new FileInputStream(updatedFile)) {
                Deps.Dependencies dependencies = Deps.Dependencies.parseFrom(inputStream);
                if (dependencies != null) {
                    List<String> dependencyStringList = Lists.newArrayList();
                    for (Deps.Dependency dependency : dependencies.getDependencyList()) {
                        // available for use in the same package
                        if (dependency.getKind() == Deps.Dependency.Kind.EXPLICIT || dependency.getKind() == Deps.Dependency.Kind.IMPLICIT) {
                            dependencyStringList.add(dependency.getPath());
                        }
                    }
                    TargetKey targetKey = fileToTargetMap.get(updatedFile);
                    return new Result(updatedFile, targetKey, dependencyStringList);
                }
            } catch (FileNotFoundException e) {
                logger.info("Could not open jdeps file: " + updatedFile);
            }
            return null;
        }));
    }
    try {
        for (Result result : Futures.allAsList(futures).get()) {
            if (result != null) {
                state.fileToTargetMap.put(result.file, result.targetKey);
                state.targetToJdeps.put(result.targetKey, result.dependencies);
            }
        }
        context.output(PrintOutput.log(String.format("Loaded %d jdeps files, total size %dkB", updatedFiles.size(), totalSizeLoaded.get() / 1024)));
    } catch (InterruptedException | ExecutionException e) {
        logger.error(e);
        return null;
    }
    return state;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 43 with TargetKey

use of com.google.idea.blaze.base.ideinfo.TargetKey in project intellij by bazelbuild.

the class BlazeRenderErrorContributor method reportGeneratedResources.

/**
 * We can't find generated resources. If a layout uses them, the layout won't render correctly.
 */
private void reportGeneratedResources(AndroidResourceModule resourceModule, TargetMap targetMap, ArtifactLocationDecoder decoder) {
    Map<String, Throwable> brokenClasses = logger.getBrokenClasses();
    if (brokenClasses == null || brokenClasses.isEmpty()) {
        return;
    }
    // Sorted entries for deterministic error message.
    SortedMap<ArtifactLocation, TargetIdeInfo> generatedResources = Maps.newTreeMap(getGeneratedResources(targetMap.get(resourceModule.targetKey)));
    for (TargetKey dependency : resourceModule.transitiveResourceDependencies) {
        generatedResources.putAll(getGeneratedResources(targetMap.get(dependency)));
    }
    if (generatedResources.isEmpty()) {
        return;
    }
    HtmlBuilder builder = new HtmlBuilder();
    builder.add("Generated resources will not be discovered by the IDE:");
    builder.beginList();
    for (Map.Entry<ArtifactLocation, TargetIdeInfo> entry : generatedResources.entrySet()) {
        ArtifactLocation resource = entry.getKey();
        TargetIdeInfo target = entry.getValue();
        builder.listItem().add(resource.getRelativePath()).add(" from ");
        addTargetLink(builder, target, decoder);
    }
    builder.endList().add("Please avoid using generated resources, ").addLink("then ", "sync the project", " ", getLinkManager().createSyncProjectUrl()).addLink("and ", "refresh the layout", ".", getLinkManager().createRefreshRenderUrl());
    addIssue().setSeverity(HighlightSeverity.ERROR, // Reported above broken classes
    HIGH_PRIORITY + 1).setSummary("Generated resources").setHtmlContent(builder).build();
}
Also used : TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) HtmlBuilder(com.android.utils.HtmlBuilder) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) Map(java.util.Map) TransitiveDependencyMap(com.google.idea.blaze.base.targetmaps.TransitiveDependencyMap) SourceToTargetMap(com.google.idea.blaze.base.targetmaps.SourceToTargetMap) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) SortedMap(java.util.SortedMap)

Example 44 with TargetKey

use of com.google.idea.blaze.base.ideinfo.TargetKey in project intellij by bazelbuild.

the class BlazeRenderErrorContributor method reportResourceTargetShouldDependOnClassTarget.

/**
 * Blaze doesn't resolve class dependencies from resources until building the final
 * android_binary, so we could end up with resources that ultimately build correctly, but fail to
 * find their class dependencies during rendering in the layout editor.
 */
private void reportResourceTargetShouldDependOnClassTarget(TargetIdeInfo target, TargetMap targetMap, ArtifactLocationDecoder decoder) {
    Collection<String> missingClasses = logger.getMissingClasses();
    if (missingClasses == null || missingClasses.isEmpty()) {
        return;
    }
    // Sorted entries for deterministic error message.
    SortedSetMultimap<String, TargetKey> missingClassToTargetMap = TreeMultimap.create();
    SourceToTargetMap sourceToTargetMap = SourceToTargetMap.getInstance(project);
    ImmutableCollection transitiveDependencies = TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key);
    for (String missingClass : missingClasses) {
        File sourceFile = getSourceFileForClass(missingClass);
        if (sourceFile == null) {
            continue;
        }
        ImmutableCollection<TargetKey> sourceTargets = sourceToTargetMap.getRulesForSourceFile(sourceFile);
        if (sourceTargets.stream().noneMatch(sourceTarget -> sourceTarget.equals(target.key) || transitiveDependencies.contains(sourceTarget))) {
            missingClassToTargetMap.putAll(missingClass, sourceTargets);
        }
    }
    if (missingClassToTargetMap.isEmpty()) {
        return;
    }
    HtmlBuilder builder = new HtmlBuilder();
    addTargetLink(builder, target, decoder).add(" contains resource files that reference these classes:").beginList();
    for (String missingClass : missingClassToTargetMap.keySet()) {
        builder.listItem().addLink(missingClass, getLinkManager().createOpenClassUrl(missingClass)).add(" from ");
        for (TargetKey targetKey : missingClassToTargetMap.get(missingClass)) {
            addTargetLink(builder, targetMap.get(targetKey), decoder).add(" ");
        }
    }
    builder.endList().add("Please fix your dependencies so that ");
    addTargetLink(builder, target, decoder).add(" correctly depends on these classes, ").addLink("then ", "sync the project", " ", getLinkManager().createSyncProjectUrl()).addLink("and ", "refresh the layout", ".", getLinkManager().createRefreshRenderUrl()).newline().newline().addBold("NOTE: blaze can still build with the incorrect dependencies " + "due to the way it handles resources, " + "but the layout editor needs them to be correct.");
    addIssue().setSeverity(HighlightSeverity.ERROR, // Reported above missing classes.
    HIGH_PRIORITY + 1).setSummary("Missing class dependencies").setHtmlContent(builder).build();
}
Also used : SourceToTargetMap(com.google.idea.blaze.base.targetmaps.SourceToTargetMap) ImmutableCollection(com.google.common.collect.ImmutableCollection) HtmlBuilder(com.android.utils.HtmlBuilder) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) File(java.io.File)

Example 45 with TargetKey

use of com.google.idea.blaze.base.ideinfo.TargetKey in project intellij by bazelbuild.

the class ReverseDependencyMapTest method testThreeLevelGraph.

@Test
public void testThreeLevelGraph() {
    TargetMapBuilder builder = TargetMapBuilder.builder();
    TargetMap targetMap = builder.addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("test/BUILD")).setLabel("//l:l1").setKind("java_library").addDependency("//l:l3")).addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("test/BUILD")).setLabel("//l:l2").addDependency("//l:l3").setKind("java_library")).addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("test/BUILD")).setLabel("//l:l3").setKind("java_library")).addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("test/BUILD")).setLabel("//l:l4").addDependency("//l:l3").setKind("java_library")).addTarget(TargetIdeInfo.builder().setBuildFile(sourceRoot("test/BUILD")).setLabel("//l:l5").addDependency("//l:l4").setKind("java_library")).build();
    ImmutableMultimap<TargetKey, TargetKey> reverseDependencies = ReverseDependencyMap.createRdepsMap(targetMap);
    assertThat(reverseDependencies).containsEntry(TargetKey.forPlainTarget(Label.create("//l:l3")), TargetKey.forPlainTarget(Label.create("//l:l1")));
    assertThat(reverseDependencies).containsEntry(TargetKey.forPlainTarget(Label.create("//l:l3")), TargetKey.forPlainTarget(Label.create("//l:l2")));
    assertThat(reverseDependencies).containsEntry(TargetKey.forPlainTarget(Label.create("//l:l3")), TargetKey.forPlainTarget(Label.create("//l:l4")));
    assertThat(reverseDependencies).containsEntry(TargetKey.forPlainTarget(Label.create("//l:l4")), TargetKey.forPlainTarget(Label.create("//l:l5")));
}
Also used : TargetMapBuilder(com.google.idea.blaze.base.ideinfo.TargetMapBuilder) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Test(org.junit.Test)

Aggregations

TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)52 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)25 File (java.io.File)19 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)14 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)14 Nullable (javax.annotation.Nullable)12 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)11 List (java.util.List)11 Test (org.junit.Test)11 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)9 Project (com.intellij.openapi.project.Project)9 Map (java.util.Map)9 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 Collection (java.util.Collection)8 Set (java.util.Set)8 Sets (com.google.common.collect.Sets)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)7 ExecutionException (java.util.concurrent.ExecutionException)7