use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.
the class TargetPatternPhaseFunction method getTargetsToBuild.
/**
* Interpret the command-line arguments.
*
* @param options the command-line arguments in structured form
*/
private static ResolvedTargets<Target> getTargetsToBuild(Environment env, TargetPatternList options) throws InterruptedException {
List<SkyKey> patternSkyKeys = new ArrayList<>();
for (TargetPatternSkyKeyOrException keyOrException : TargetPatternValue.keys(options.getTargetPatterns(), FilteringPolicies.FILTER_MANUAL, options.getOffset())) {
try {
patternSkyKeys.add(keyOrException.getSkyKey());
} catch (TargetParsingException e) {
// Skip.
}
}
Map<SkyKey, ValueOrException<TargetParsingException>> resolvedPatterns = env.getValuesOrThrow(patternSkyKeys, TargetParsingException.class);
if (env.valuesMissing()) {
return null;
}
ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();
for (SkyKey key : patternSkyKeys) {
TargetPatternKey pattern = (TargetPatternKey) key.argument();
TargetPatternValue value;
try {
value = (TargetPatternValue) resolvedPatterns.get(key).get();
} catch (TargetParsingException e) {
// TODO(ulfjack): Report to EventBus.
String rawPattern = pattern.getPattern();
String errorMessage = e.getMessage();
env.getListener().handle(Event.error("Skipping '" + rawPattern + "': " + errorMessage));
builder.setError();
continue;
}
// TODO(ulfjack): This is terribly inefficient.
ResolvedTargets<Target> asTargets = TestSuiteExpansionFunction.labelsToTargets(env, value.getTargets().getTargets(), value.getTargets().hasError());
if (pattern.isNegative()) {
builder.filter(Predicates.not(Predicates.in(asTargets.getTargets())));
} else {
builder.merge(asTargets);
}
}
ResolvedTargets<Target> result = builder.filter(TargetUtils.tagFilter(options.getBuildTargetFilter())).build();
if (options.getCompileOneDependency()) {
TargetProvider targetProvider = new EnvironmentBackedRecursivePackageProvider(env);
try {
return new CompileOneDependencyTransformer(targetProvider).transformCompileOneDependency(env.getListener(), result);
} catch (MissingDepException e) {
return null;
} catch (TargetParsingException e) {
env.getListener().handle(Event.error(e.getMessage()));
return ResolvedTargets.failed();
}
}
return result;
}
use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.
the class SkyframeTargetPatternEvaluator method parseTargetPatternKeys.
ResolvedTargets<Target> parseTargetPatternKeys(List<String> targetPattern, Iterable<SkyKey> patternSkyKeys, int numThreads, boolean keepGoing, ExtendedEventHandler eventHandler, TargetPatternsResultBuilder finalTargetSetEvaluator) throws InterruptedException, TargetParsingException {
EvaluationResult<TargetPatternValue> result = skyframeExecutor.targetPatterns(patternSkyKeys, numThreads, keepGoing, eventHandler);
String errorMessage = null;
for (SkyKey key : patternSkyKeys) {
TargetPatternValue resultValue = result.get(key);
if (resultValue != null) {
ResolvedTargets<Label> results = resultValue.getTargets();
if (((TargetPatternValue.TargetPatternKey) key.argument()).isNegative()) {
finalTargetSetEvaluator.addLabelsOfNegativePattern(results);
} else {
finalTargetSetEvaluator.addLabelsOfPositivePattern(results);
}
} else {
TargetPatternValue.TargetPatternKey patternKey = (TargetPatternValue.TargetPatternKey) key.argument();
String rawPattern = patternKey.getPattern();
ErrorInfo error = result.errorMap().get(key);
if (error == null) {
Preconditions.checkState(!keepGoing);
continue;
}
if (error.getException() != null) {
// This exception may not be a TargetParsingException because in a nokeep_going build, the
// target pattern parser may swallow a NoSuchPackageException but the framework will
// bubble it up anyway.
Preconditions.checkArgument(!keepGoing || error.getException() instanceof TargetParsingException, error);
errorMessage = error.getException().getMessage();
} else if (!Iterables.isEmpty(error.getCycleInfo())) {
errorMessage = "cycles detected during target parsing";
skyframeExecutor.getCyclesReporter().reportCycles(error.getCycleInfo(), key, eventHandler);
} else {
throw new IllegalStateException(error.toString());
}
if (keepGoing) {
eventHandler.handle(Event.error("Skipping '" + rawPattern + "': " + errorMessage));
eventHandler.post(PatternExpandingError.skipped(rawPattern, errorMessage));
}
finalTargetSetEvaluator.setError();
if (eventHandler instanceof ParseFailureListener) {
ParseFailureListener parseListener = (ParseFailureListener) eventHandler;
parseListener.parsingError(rawPattern, errorMessage);
}
}
}
if (result.hasError()) {
Preconditions.checkState(errorMessage != null, "unexpected errors: %s", result.errorMap());
finalTargetSetEvaluator.setError();
if (!keepGoing) {
eventHandler.post(PatternExpandingError.failed(targetPattern, errorMessage));
throw new TargetParsingException(errorMessage);
}
}
WalkableGraph walkableGraph = Preconditions.checkNotNull(result.getWalkableGraph(), result);
return finalTargetSetEvaluator.build(walkableGraph);
}
use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.
the class TargetPatternValue method keys.
/**
* Returns an iterable of {@link TargetPatternSkyKeyOrException}, with {@link TargetPatternKey}
* arguments, in the same order as the list of patterns provided as input. If a provided pattern
* fails to parse, the element in the returned iterable corresponding to it will throw when its
* {@link TargetPatternSkyKeyOrException#getSkyKey} method is called.
*
* @param patterns The list of patterns, eg "-foo/biz...". If a pattern's first character is "-",
* it is treated as a negative pattern.
* @param policy The filtering policy, eg "only return test targets"
* @param offset The offset to apply to relative target patterns.
*/
@ThreadSafe
public static Iterable<TargetPatternSkyKeyOrException> keys(List<String> patterns, FilteringPolicy policy, String offset) {
TargetPattern.Parser parser = new TargetPattern.Parser(offset);
ImmutableList.Builder<TargetPatternSkyKeyOrException> builder = ImmutableList.builder();
for (String pattern : patterns) {
boolean positive = !pattern.startsWith("-");
String absoluteValueOfPattern = positive ? pattern : pattern.substring(1);
TargetPattern targetPattern;
try {
targetPattern = parser.parse(absoluteValueOfPattern);
} catch (TargetParsingException e) {
builder.add(new TargetPatternSkyKeyException(e, absoluteValueOfPattern));
continue;
}
TargetPatternKey targetPatternKey = new TargetPatternKey(targetPattern, positive ? policy : FilteringPolicies.NO_FILTER, /*isNegative=*/
!positive, offset, ImmutableSet.<PathFragment>of());
SkyKey skyKey = SkyKey.create(SkyFunctions.TARGET_PATTERN, targetPatternKey);
builder.add(new TargetPatternSkyKeyValue(skyKey));
}
return builder.build();
}
use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.
the class SkylarkAspectsTest method multipleAspects.
@Test
public void multipleAspects() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target,ctx):", " return struct()", "my_aspect = aspect(implementation = _aspect_impl)", "def _dummy_impl(ctx):", " pass", "r1 = rule(_dummy_impl, ", " attrs = { 'deps' : attr.label_list(aspects = [my_aspect, my_aspect]) })");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r1')", "r1(name = 't1')");
reporter.removeHandler(failFastHandler);
try {
AnalysisResult result = update("//test:r1");
assertThat(keepGoing()).isTrue();
assertThat(result.hasError()).isTrue();
} catch (TargetParsingException | ViewCreationFailedException expected) {
// expected.
}
assertContainsEvent("aspect //test:aspect.bzl%my_aspect added more than once");
}
use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method transformCompileOneDependency.
private Target transformCompileOneDependency(ExtendedEventHandler eventHandler, Target target) throws TargetParsingException, InterruptedException {
if (!(target instanceof FileTarget)) {
throw new TargetParsingException("--compile_one_dependency target '" + target.getLabel() + "' must be a file");
}
Rule result = null;
Iterable<Rule> orderedRuleList = getOrderedRuleList(target.getPackage());
for (Rule rule : orderedRuleList) {
Set<Label> labels = getInputLabels(rule);
if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
result = rule;
break;
}
if (result == null) {
result = rule;
}
}
}
if (result == null) {
throw new TargetParsingException("Couldn't find dependency on target '" + target.getLabel() + "'");
}
// If the rule has source targets, return it.
if (!RawAttributeMapper.of(result).getMergedValues("srcs", BuildType.LABEL_LIST).isEmpty()) {
return result;
}
// Try to find a rule in the same package that has 'result' as a dependency.
for (Rule rule : orderedRuleList) {
RawAttributeMapper attributes = RawAttributeMapper.of(rule);
// We don't know which path to follow for configurable attributes, so skip them.
if (attributes.isConfigurable("deps") || attributes.isConfigurable("srcs")) {
continue;
}
RuleClass ruleClass = rule.getRuleClassObject();
if (ruleClass.hasAttr("deps", BuildType.LABEL_LIST) && ruleClass.hasAttr("srcs", BuildType.LABEL_LIST)) {
for (Label dep : attributes.get("deps", BuildType.LABEL_LIST)) {
if (dep.equals(result.getLabel())) {
if (!attributes.get("srcs", BuildType.LABEL_LIST).isEmpty()) {
return rule;
}
}
}
}
}
return result;
}
Aggregations