Skip to content

Commit

Permalink
Add validation of scope for GraphlessBlazeQueryEnvironment.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 346504986
  • Loading branch information
zhengwei143 authored and copybara-github committed Dec 9, 2020
1 parent daaa6cf commit d83e662
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static java.util.concurrent.TimeUnit.MINUTES;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -45,6 +46,7 @@
import com.google.devtools.build.lib.util.DetailedExitCode;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -277,6 +279,20 @@ public Map<Label, Target> getTargets(Iterable<Label> labels)
return resultBuilder.build();
}

protected void validateScopeOfTargets(Set<Target> targets) throws QueryException {
// Sets.filter would be more convenient here, but can't deal with exceptions.
if (labelFilter != Predicates.<Label>alwaysTrue()) {
// The labelFilter is always true for bazel query; it's only used for genquery rules.
Iterator<Target> targetIterator = targets.iterator();
while (targetIterator.hasNext()) {
Target target = targetIterator.next();
if (!validateScope(target.getLabel(), strictScope)) {
targetIterator.remove();
}
}
}
}

protected boolean validateScope(Label label, boolean strict) throws QueryException {
if (!labelFilter.test(label)) {
String error = String.format("target '%s' is not within the scope of the query", label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Expand Down Expand Up @@ -63,7 +62,6 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -172,17 +170,7 @@ private void getTargetsMatchingPatternImpl(String pattern, Callback<Target> call
// becomes quadratic in runtime.
Set<Target> targets = new LinkedHashSet<>(resolvedTargetPatterns.get(pattern));

// Sets.filter would be more convenient here, but can't deal with exceptions.
if (labelFilter != Predicates.<Label>alwaysTrue()) {
// The labelFilter is always true for bazel query; it's only used for genquery rules.
Iterator<Target> targetIterator = targets.iterator();
while (targetIterator.hasNext()) {
Target target = targetIterator.next();
if (!validateScope(target.getLabel(), strictScope)) {
targetIterator.remove();
}
}
}
validateScopeOfTargets(targets);

Set<PackageIdentifier> packages = CompactHashSet.create();
for (Target target : targets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -153,7 +154,9 @@ public QueryTaskFuture<Void> getTargetsMatchingPattern(

private void getTargetsMatchingPatternImpl(String pattern, Callback<Target> callback)
throws QueryException, InterruptedException {
callback.process(resolvedTargetPatterns.get(pattern));
Set<Target> targets = new LinkedHashSet<>(resolvedTargetPatterns.get(pattern));
validateScopeOfTargets(targets);
callback.process(targets);
}

@Override
Expand Down

0 comments on commit d83e662

Please sign in to comment.