Skip to content

Commit 1f99a76

Browse files
committed
Add test
1 parent 7e40af0 commit 1f99a76

20 files changed

+452
-94
lines changed

src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTarget.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,4 @@ default boolean isRuleConfiguredTarget() {
139139
default ConfiguredTarget unwrapIfMerged() {
140140
return this;
141141
}
142-
143-
/**
144-
* This is only intended to be called from the query dialects of Starlark.
145-
*
146-
* @return a map of provider names to their values, or null if there are no providers
147-
*/
148-
@Override
149-
@Nullable
150-
default Dict<String, Object> getProvidersDictForQuery() {
151-
return null;
152-
}
153142
}

src/main/java/com/google/devtools/build/lib/analysis/OutputGroupInfo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ public static OutputGroupInfo get(ProviderCollection collection) {
175175
}
176176

177177
/**
178-
* Merges output groups from a list of output providers. The set of output groups must be
179-
* disjoint, except for the special validation output group, which is always merged.
178+
* Merges output groups from a list of output providers.
180179
*/
181180
@Nullable
182181
public static OutputGroupInfo merge(List<OutputGroupInfo> providers) throws MergingException {

src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/MergedConfiguredTarget.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ public void repr(Printer printer) {
374374

375375
@Override
376376
public Dict<String, Object> getProvidersDictForQuery() {
377-
return toProvidersDictForQuery(nonBaseProviders);
377+
return Dict.<String, Object>builder()
378+
.putAll(base.getProvidersDictForQuery())
379+
.putAll(toProvidersDictForQuery(nonBaseProviders))
380+
.buildImmutable();
378381
}
379382

380383
@Override

src/main/java/com/google/devtools/build/lib/buildtool/AqueryProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package com.google.devtools.build.lib.buildtool;
1515

1616
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableListMultimap;
1718
import com.google.common.collect.ImmutableMap;
1819
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
20+
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
1921
import com.google.devtools.build.lib.analysis.ConfiguredTargetValue;
2022
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionException;
2123
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
@@ -41,6 +43,7 @@
4143
import com.google.devtools.build.lib.server.FailureDetails.ActionQuery;
4244
import com.google.devtools.build.lib.server.FailureDetails.ActionQuery.Code;
4345
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
46+
import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey;
4447
import com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor;
4548
import com.google.devtools.build.lib.skyframe.actiongraph.v2.ActionGraphDump;
4649
import com.google.devtools.build.lib.skyframe.actiongraph.v2.AqueryConsumingOutputHandler;
@@ -146,6 +149,7 @@ protected PostAnalysisQueryEnvironment<ConfiguredTargetValue> getQueryEnvironmen
146149
CommandEnvironment env,
147150
TopLevelConfigurations topLevelConfigurations,
148151
ImmutableMap<String, BuildConfigurationValue> transitiveConfigurations,
152+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
149153
WalkableGraph walkableGraph) {
150154
ImmutableList<QueryFunction> extraFunctions =
151155
new ImmutableList.Builder<QueryFunction>()

src/main/java/com/google/devtools/build/lib/buildtool/CqueryProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
package com.google.devtools.build.lib.buildtool;
1515

1616
import com.google.common.collect.ImmutableList;
17+
import com.google.common.collect.ImmutableListMultimap;
1718
import com.google.common.collect.ImmutableMap;
19+
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
1820
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
1921
import com.google.devtools.build.lib.cmdline.TargetPattern;
2022
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
@@ -26,6 +28,7 @@
2628
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction;
2729
import com.google.devtools.build.lib.query2.engine.QueryExpression;
2830
import com.google.devtools.build.lib.runtime.CommandEnvironment;
31+
import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey;
2932
import com.google.devtools.build.skyframe.WalkableGraph;
3033
import net.starlark.java.eval.StarlarkSemantics;
3134

@@ -48,8 +51,8 @@ protected ConfiguredTargetQueryEnvironment getQueryEnvironment(
4851
CommandEnvironment env,
4952
TopLevelConfigurations configurations,
5053
ImmutableMap<String, BuildConfigurationValue> transitiveConfigurations,
51-
WalkableGraph walkableGraph)
52-
throws InterruptedException {
54+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
55+
WalkableGraph walkableGraph) {
5356
ImmutableList<QueryFunction> extraFunctions =
5457
new ImmutableList.Builder<QueryFunction>()
5558
.addAll(ConfiguredTargetQueryEnvironment.CQUERY_FUNCTIONS)
@@ -65,6 +68,7 @@ protected ConfiguredTargetQueryEnvironment getQueryEnvironment(
6568
extraFunctions,
6669
configurations,
6770
transitiveConfigurations,
71+
topLevelTargetAspects,
6872
mainRepoTargetParser,
6973
env.getPackageManager().getPackagePath(),
7074
() -> walkableGraph,

src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryProcessor.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.buildtool;
1515

16+
import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap;
1617
import static com.google.common.collect.ImmutableMap.toImmutableMap;
1718

19+
import com.google.common.collect.ImmutableListMultimap;
1820
import com.google.common.collect.ImmutableMap;
1921
import com.google.devtools.build.lib.analysis.AnalysisResult;
22+
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
2023
import com.google.devtools.build.lib.analysis.ViewCreationFailedException;
2124
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
2225
import com.google.devtools.build.lib.buildtool.BuildTool.ExitException;
@@ -39,6 +42,7 @@
3942
import com.google.devtools.build.lib.server.FailureDetails.ActionQuery;
4043
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
4144
import com.google.devtools.build.lib.server.FailureDetails.Query;
45+
import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey;
4246
import com.google.devtools.build.lib.skyframe.SkyframeExecutorWrappingWalkableGraph;
4347
import com.google.devtools.build.lib.util.DetailedExitCode;
4448
import com.google.devtools.build.lib.util.ExitCode;
@@ -48,6 +52,7 @@
4852
import java.io.IOException;
4953
import java.util.Collection;
5054
import java.util.Comparator;
55+
import java.util.Map;
5156
import java.util.Set;
5257
import java.util.function.Function;
5358

@@ -90,13 +95,26 @@ public void process(
9095
.build()));
9196
}
9297

98+
var queryOptions = getQueryOptions(env);
99+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects;
100+
if (queryOptions.useAspects) {
101+
topLevelTargetAspects =
102+
analysisResult.getAspectsMap().entrySet().stream()
103+
.collect(
104+
toImmutableListMultimap(
105+
entry -> entry.getKey().getBaseConfiguredTargetKey(), Map.Entry::getValue));
106+
} else {
107+
topLevelTargetAspects = ImmutableListMultimap.of();
108+
}
109+
93110
try (QueryRuntimeHelper queryRuntimeHelper =
94-
env.getRuntime().getQueryRuntimeHelperFactory().create(env, getQueryOptions(env))) {
111+
env.getRuntime().getQueryRuntimeHelperFactory().create(env, queryOptions)) {
95112
doPostAnalysisQuery(
96113
request,
97114
env,
98115
runtime,
99116
new TopLevelConfigurations(analysisResult.getTopLevelTargetsWithConfigs()),
117+
topLevelTargetAspects,
100118
env.getSkyframeExecutor().getTransitiveConfigurationKeys(),
101119
queryRuntimeHelper,
102120
queryExpression);
@@ -139,6 +157,7 @@ protected abstract PostAnalysisQueryEnvironment<T> getQueryEnvironment(
139157
CommandEnvironment env,
140158
TopLevelConfigurations topLevelConfigurations,
141159
ImmutableMap<String, BuildConfigurationValue> transitiveConfigurations,
160+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
142161
WalkableGraph walkableGraph)
143162
throws InterruptedException;
144163

@@ -158,10 +177,14 @@ private void doPostAnalysisQuery(
158177
CommandEnvironment env,
159178
BlazeRuntime runtime,
160179
TopLevelConfigurations topLevelConfigurations,
180+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
161181
Collection<SkyKey> transitiveConfigurationKeys,
162182
QueryRuntimeHelper queryRuntimeHelper,
163183
QueryExpression queryExpression)
164-
throws InterruptedException, QueryException, IOException, QueryRuntimeHelperException,
184+
throws InterruptedException,
185+
QueryException,
186+
IOException,
187+
QueryRuntimeHelperException,
165188
OptionsParsingException {
166189
WalkableGraph walkableGraph =
167190
SkyframeExecutorWrappingWalkableGraph.of(env.getSkyframeExecutor());
@@ -170,7 +193,12 @@ private void doPostAnalysisQuery(
170193

171194
PostAnalysisQueryEnvironment<T> postAnalysisQueryEnvironment =
172195
getQueryEnvironment(
173-
request, env, topLevelConfigurations, transitiveConfigurations, walkableGraph);
196+
request,
197+
env,
198+
topLevelConfigurations,
199+
transitiveConfigurations,
200+
topLevelTargetAspects,
201+
walkableGraph);
174202

175203
Iterable<NamedThreadSafeOutputFormatterCallback<T>> callbacks =
176204
postAnalysisQueryEnvironment.getDefaultOutputFormatters(
@@ -217,7 +245,7 @@ private void doPostAnalysisQuery(
217245
}
218246
callback.start();
219247
callback.process(aggregateResultsCallback.getResult());
220-
callback.close(/*failFast=*/ !result.getSuccess());
248+
callback.close(/* failFast= */ !result.getSuccess());
221249

222250
queryRuntimeHelper.afterQueryOutputIsWritten();
223251
}

src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ && getConfiguration(dep.dependency).isToolConfiguration())
479479
}
480480
}
481481
if (settings.contains(Setting.NO_IMPLICIT_DEPS)) {
482-
deps = deps.stream().filter(dep -> !dep.implicit).collect(Collectors.toList());
482+
deps = deps.stream().filter(dep -> !dep.implicit).collect(Collectors.toList());
483483
}
484484
return getDependencies(deps);
485485
}

src/main/java/com/google/devtools/build/lib/query2/common/CqueryNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ default CqueryNode unwrapIfMerged() {
101101
/**
102102
* This is only intended to be called from the query dialects of Starlark.
103103
*
104-
* @return a map of provider names to their values, or null if there are no providers
104+
* @return a map of provider names to their values
105105
*/
106-
@Nullable
107106
default Dict<String, Object> getProvidersDictForQuery() {
108-
return null;
107+
return Dict.empty();
109108
}
110109
}

src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetAccessor.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.query2.cquery;
1515

16-
1716
import com.google.common.base.Preconditions;
1817
import com.google.common.collect.ImmutableList;
1918
import com.google.common.collect.ImmutableListMultimap;
2019
import com.google.common.collect.ImmutableMap;
2120
import com.google.common.collect.ImmutableSet;
2221
import com.google.common.collect.Multimaps;
22+
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
2323
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2424
import com.google.devtools.build.lib.analysis.ConfiguredTargetValue;
2525
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
26+
import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget;
2627
import com.google.devtools.build.lib.analysis.configuredtargets.OutputFileConfiguredTarget;
2728
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
2829
import com.google.devtools.build.lib.cmdline.Label;
@@ -61,9 +62,12 @@ public class ConfiguredTargetAccessor implements TargetAccessor<CqueryNode> {
6162
private final WalkableGraph walkableGraph;
6263
private final ConfiguredTargetQueryEnvironment queryEnvironment;
6364
private final SkyFunction.LookupEnvironment lookupEnvironment;
65+
private final ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects;
6466

6567
public ConfiguredTargetAccessor(
66-
WalkableGraph walkableGraph, ConfiguredTargetQueryEnvironment queryEnvironment) {
68+
WalkableGraph walkableGraph,
69+
ConfiguredTargetQueryEnvironment queryEnvironment,
70+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects) {
6771
this.walkableGraph = walkableGraph;
6872
this.queryEnvironment = queryEnvironment;
6973
this.lookupEnvironment =
@@ -80,6 +84,7 @@ public ConfiguredTargetAccessor(
8084
"Thread interrupted in the middle of looking up: " + key, e);
8185
}
8286
});
87+
this.topLevelTargetAspects = topLevelTargetAspects;
8388
}
8489

8590
@Override
@@ -159,7 +164,7 @@ public List<CqueryNode> getPrerequisites(
159164
rule,
160165
configConditions,
161166
keyedConfiguredTarget.getConfigurationChecksum(),
162-
/*alwaysSucceed=*/ false);
167+
/* alwaysSucceed= */ false);
163168
if (!attributeMapper.has(attrName)) {
164169
throw new QueryException(
165170
caller,
@@ -263,4 +268,16 @@ RuleConfiguredTarget getGeneratingConfiguredTarget(CqueryNode kct) throws Interr
263268
.build()))
264269
.getConfiguredTarget();
265270
}
271+
272+
CqueryNode mergeWithTopLevelAspects(CqueryNode cn) {
273+
if (!(cn.getLookupKey() instanceof ConfiguredTargetKey key)) {
274+
return cn;
275+
}
276+
try {
277+
return MergedConfiguredTarget.of((ConfiguredTarget) cn, topLevelTargetAspects.get(key));
278+
} catch (MergedConfiguredTarget.MergingException e) {
279+
throw new IllegalStateException(
280+
"Unexpectedly failed to merge target with top-level aspects: " + key, e);
281+
}
282+
}
266283
}

src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import com.google.common.base.Joiner;
1717
import com.google.common.base.Verify;
1818
import com.google.common.collect.ImmutableList;
19+
import com.google.common.collect.ImmutableListMultimap;
1920
import com.google.common.collect.ImmutableMap;
2021
import com.google.common.collect.ImmutableSet;
2122
import com.google.common.util.concurrent.AsyncFunction;
2223
import com.google.common.util.concurrent.Futures;
2324
import com.google.common.util.concurrent.MoreExecutors;
2425
import com.google.devtools.build.lib.actions.ActionLookupKey;
2526
import com.google.devtools.build.lib.analysis.AspectValue;
27+
import com.google.devtools.build.lib.analysis.ConfiguredAspect;
2628
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2729
import com.google.devtools.build.lib.analysis.ConfiguredTargetValue;
2830
import com.google.devtools.build.lib.analysis.TopLevelArtifactContext;
@@ -79,6 +81,7 @@
7981
public class ConfiguredTargetQueryEnvironment extends PostAnalysisQueryEnvironment<CqueryNode> {
8082
/** Common query functions and cquery specific functions. */
8183
public static final ImmutableList<QueryFunction> FUNCTIONS = populateFunctions();
84+
8285
/** Cquery specific functions. */
8386
public static final ImmutableList<QueryFunction> CQUERY_FUNCTIONS = getCqueryFunctions();
8487

@@ -101,13 +104,13 @@ public ConfiguredTargetQueryEnvironment(
101104
Iterable<QueryFunction> extraFunctions,
102105
TopLevelConfigurations topLevelConfigurations,
103106
ImmutableMap<String, BuildConfigurationValue> transitiveConfigurations,
107+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
104108
TargetPattern.Parser mainRepoTargetParser,
105109
PathPackageLocator pkgPath,
106110
Supplier<WalkableGraph> walkableGraphSupplier,
107111
Set<Setting> settings,
108112
TopLevelArtifactContext topLevelArtifactContext,
109-
LabelPrinter labelPrinter)
110-
throws InterruptedException {
113+
LabelPrinter labelPrinter) {
111114
super(
112115
keepGoing,
113116
eventHandler,
@@ -119,7 +122,8 @@ public ConfiguredTargetQueryEnvironment(
119122
walkableGraphSupplier,
120123
settings,
121124
labelPrinter);
122-
this.accessor = new ConfiguredTargetAccessor(walkableGraphSupplier.get(), this);
125+
this.accessor =
126+
new ConfiguredTargetAccessor(walkableGraphSupplier.get(), this, topLevelTargetAspects);
123127
this.configuredTargetKeyExtractor = CqueryNode::getLookupKey;
124128
this.topLevelArtifactContext = topLevelArtifactContext;
125129
}
@@ -130,19 +134,20 @@ public ConfiguredTargetQueryEnvironment(
130134
Iterable<QueryFunction> extraFunctions,
131135
TopLevelConfigurations topLevelConfigurations,
132136
ImmutableMap<String, BuildConfigurationValue> transitiveConfigurations,
137+
ImmutableListMultimap<ConfiguredTargetKey, ConfiguredAspect> topLevelTargetAspects,
133138
TargetPattern.Parser mainRepoTargetParser,
134139
PathPackageLocator pkgPath,
135140
Supplier<WalkableGraph> walkableGraphSupplier,
136141
CqueryOptions cqueryOptions,
137142
TopLevelArtifactContext topLevelArtifactContext,
138-
LabelPrinter labelPrinter)
139-
throws InterruptedException {
143+
LabelPrinter labelPrinter) {
140144
this(
141145
keepGoing,
142146
eventHandler,
143147
extraFunctions,
144148
topLevelConfigurations,
145149
transitiveConfigurations,
150+
topLevelTargetAspects,
146151
mainRepoTargetParser,
147152
pkgPath,
148153
walkableGraphSupplier,
@@ -322,15 +327,13 @@ private CqueryNode getConfiguredTarget(
322327
@Nullable
323328
protected CqueryNode getValueFromKey(SkyKey key) throws InterruptedException {
324329
SkyValue value = getConfiguredTargetValue(key);
325-
if (value == null) {
326-
return null;
327-
} else if (value instanceof ConfiguredTargetValue configuredTargetValue) {
328-
return configuredTargetValue.getConfiguredTarget();
329-
} else if (value instanceof AspectValue && key instanceof AspectKey aspectValue) {
330-
return aspectValue;
331-
} else {
332-
throw new IllegalStateException("unknown value type for CqueryNode");
333-
}
330+
return switch (value) {
331+
case ConfiguredTargetValue configuredTargetValue ->
332+
configuredTargetValue.getConfiguredTarget();
333+
case AspectValue ignored when key instanceof AspectKey aspectKey -> aspectKey;
334+
case null -> null;
335+
default -> throw new IllegalStateException("unknown value type for CqueryNode");
336+
};
334337
}
335338

336339
/**

0 commit comments

Comments
 (0)