Skip to content

Commit

Permalink
fix(qsync): NPE when trying to enable analysis for a directory or for…
Browse files Browse the repository at this point in the history
… a BUILD file (#6992)
  • Loading branch information
Tomasz Pasternak authored Nov 14, 2024
1 parent fc3990b commit 48f7ac5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public ImmutableSet<DependencyTrackingBehavior> getDependencyTrackingBehaviors(L
* the set of all targets defined in all build packages within the directory (recursively).
*/
public TargetsToBuild getProjectTargets(Context<?> context, Path workspaceRelativePath) {
if (workspaceRelativePath.endsWith("BUILD")) {
if (workspaceRelativePath.endsWith("BUILD") || workspaceRelativePath.endsWith("BUILD.bazel")) {
Path packagePath = workspaceRelativePath.getParent();
return TargetsToBuild.targetGroup(allTargets().get(packagePath));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.Label;
import com.google.idea.blaze.exception.BuildException;
import com.google.idea.blaze.qsync.deps.ArtifactTracker;
import com.google.idea.blaze.qsync.java.PackageReader;
Expand All @@ -31,7 +32,9 @@
import com.google.idea.blaze.qsync.query.QuerySummary;
import com.google.idea.blaze.qsync.testdata.TestData;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Builds a {@link BlazeProjectSnapshot} for a test project by running the logic from the various
Expand Down Expand Up @@ -59,7 +62,7 @@ public BlazeProjectSnapshot sync(TestData testProject) throws IOException, Build
/* testSources= */ ImmutableSet.of(),
/* systemExcludes = */ ImmutableSet.of()
);
QuerySummary querySummary = getQuerySummary(testProject);
QuerySummary querySummary = adaptQuerySummaryDueToABazelBug(getQuerySummary(testProject));
PostQuerySyncData pqsd =
PostQuerySyncData.builder()
.setProjectDefinition(projectDefinition)
Expand All @@ -85,4 +88,23 @@ public BlazeProjectSnapshot sync(TestData testProject) throws IOException, Build
.project(project)
.build();
}

/**
* We should add --consistent_labels flag to this target and all similar ones
* //querysync/javatests/com/google/idea/blaze/qsync/testdata:java_library_external_dep_query
* Unfortunately, due to a bug in bazel it doesn't work, so we have to adjust the rule names in code
* The bug is reported here. The method should be cleared and inlined after the
* bug <a href="https://github.com/bazelbuild/bazel/issues/24325">#24325</a> is fixed.
*/
private static QuerySummary adaptQuerySummaryDueToABazelBug(QuerySummary querySummary) {
var newRulesMap = querySummary.getRulesMap().entrySet().stream()
.collect(Collectors.toMap(
it -> Label.of(it.getKey().toString().replaceFirst("^//", "@@//")),
Map.Entry::getValue));
return QuerySummary.newBuilder()
.putAllPackagesWithErrors(querySummary.getPackagesWithErrors())
.putAllSourceFiles(querySummary.getSourceFilesMap())
.putAllRules(newRulesMap)
.build();
}
}
2 changes: 1 addition & 1 deletion shared/java/com/google/idea/blaze/common/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Label of(String label) {
}

public static Label fromPackageAndName(Path packagePath, Path name) {
return of(String.format("//%s:%s", packagePath, name));
return of(String.format("@@//%s:%s", packagePath, name));
}

public static Label fromPackageAndName(Path packagePath, String name) {
Expand Down

0 comments on commit 48f7ac5

Please sign in to comment.