Skip to content

Commit

Permalink
Add scope validation for GraphlessBlazeQueryEnvironment.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 346273035
  • Loading branch information
zhengwei143 authored and copybara-github committed Dec 8, 2020
1 parent 2a07fed commit ba5bf5f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 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 @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.query2.query;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Expand Down Expand Up @@ -153,6 +154,8 @@ public QueryTaskFuture<Void> getTargetsMatchingPattern(

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

Expand Down

0 comments on commit ba5bf5f

Please sign in to comment.