-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathaquery.rs
More file actions
367 lines (343 loc) · 13.8 KB
/
Copy pathaquery.rs
File metadata and controls
367 lines (343 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
use allocative::Allocative;
use buck2_artifact::actions::key::ActionKey;
use buck2_build_api::actions::query::ActionQueryNode;
use buck2_build_api::query::bxl::BxlAqueryFunctions;
use buck2_build_api::query::bxl::NEW_BXL_AQUERY_FUNCTIONS;
use buck2_build_api::query::oneshot::QUERY_FRONTEND;
use buck2_core::configuration::compatibility::IncompatiblePlatformReason;
use buck2_core::global_cfg_options::GlobalCfgOptions;
use buck2_core::provider::label::ConfiguredProvidersLabel;
use buck2_node::nodes::configured::ConfiguredTargetNode;
use buck2_query::query::syntax::simple::eval::set::TargetSet;
use buck2_query::query::syntax::simple::functions::helpers::CapturedExpr;
use derivative::Derivative;
use derive_more::Display;
use dice::DiceComputations;
use dupe::Dupe;
use futures::FutureExt;
use gazebo::prelude::OptionExt;
use starlark::any::ProvidesStaticType;
use starlark::environment::Methods;
use starlark::environment::MethodsBuilder;
use starlark::environment::MethodsStatic;
use starlark::eval::Evaluator;
use starlark::starlark_module;
use starlark::values::AllocValue;
use starlark::values::Heap;
use starlark::values::NoSerialize;
use starlark::values::StarlarkValue;
use starlark::values::Trace;
use starlark::values::UnpackValue;
use starlark::values::Value;
use starlark::values::ValueTyped;
use starlark::values::list::UnpackList;
use starlark::values::none::NoneOr;
use starlark::values::starlark_value;
use starlark::values::type_repr::StarlarkTypeRepr;
use crate::bxl::starlark_defs::context::BxlContext;
use crate::bxl::starlark_defs::context::ErrorPrinter;
use crate::bxl::starlark_defs::nodes::action::StarlarkAction;
use crate::bxl::starlark_defs::nodes::action::StarlarkActionQueryNode;
use crate::bxl::starlark_defs::providers_expr::AnyProvidersExprArg;
use crate::bxl::starlark_defs::providers_expr::ProvidersExpr;
use crate::bxl::starlark_defs::query_util::parse_query_evaluation_result;
use crate::bxl::starlark_defs::target_list_expr::ConfiguredTargetListExprArg;
use crate::bxl::starlark_defs::target_list_expr::TargetListExpr;
use crate::bxl::starlark_defs::targetset::StarlarkTargetSet;
use crate::bxl::starlark_defs::uquery::UnpackUnconfiguredQueryArgs;
use crate::bxl::value_as_starlark_target_label::ValueAsStarlarkTargetLabel;
#[derive(
ProvidesStaticType,
Derivative,
Display,
Trace,
NoSerialize,
Allocative
)]
#[derivative(Debug)]
#[display("{:?}", self)]
#[allocative(skip)]
pub(crate) struct StarlarkAQueryCtx<'v> {
#[derivative(Debug = "ignore")]
ctx: ValueTyped<'v, BxlContext<'v>>,
#[derivative(Debug = "ignore")]
// Overrides the GlobalCfgOptions in the BxlContext
global_cfg_options_override: GlobalCfgOptions,
}
#[starlark_value(type = "bxl.AqueryContext", StarlarkTypeRepr, UnpackValue)]
impl<'v> StarlarkValue<'v> for StarlarkAQueryCtx<'v> {
fn get_methods() -> Option<&'static Methods> {
static RES: MethodsStatic = MethodsStatic::new();
RES.methods_for_type::<Self::Canonical>(aquery_methods)
}
}
impl<'v> AllocValue<'v> for StarlarkAQueryCtx<'v> {
fn alloc_value(self, heap: Heap<'v>) -> Value<'v> {
heap.alloc_complex_no_freeze(self)
}
}
impl<'v> StarlarkAQueryCtx<'v> {
pub(crate) fn new(
ctx: ValueTyped<'v, BxlContext<'v>>,
global_target_platform: ValueAsStarlarkTargetLabel<'v>,
) -> buck2_error::Result<StarlarkAQueryCtx<'v>> {
let global_cfg_options = ctx.resolve_global_cfg_options(global_target_platform, vec![])?;
Ok(Self {
ctx,
global_cfg_options_override: global_cfg_options,
})
}
}
pub(crate) async fn get_aquery_env(
ctx: &BxlContext<'_>,
global_cfg_options_override: &GlobalCfgOptions,
) -> buck2_error::Result<Box<dyn BxlAqueryFunctions>> {
(NEW_BXL_AQUERY_FUNCTIONS.get()?)(
global_cfg_options_override.clone(),
ctx.project_root().dupe(),
ctx.cell_name(),
ctx.cell_resolver().dupe(),
)
.await
}
#[derive(StarlarkTypeRepr, UnpackValue)]
enum UnpackActionNodes<'v> {
ActionQueryNodes(UnpackList<StarlarkActionQueryNode>),
ActionQueryNodesSet(&'v StarlarkTargetSet<ActionQueryNode>),
ConfiguredProviders(AnyProvidersExprArg<'v>),
ConfiguredTargets(ConfiguredTargetListExprArg<'v>),
StarlarkActions(UnpackList<StarlarkAction>),
}
// Aquery operates on `ActionQueryNode`s. Under the hood, the target set of action query nodes is obtained
// by running analysis on a configured providers label. We can accept either a `TargetExpr`, `ProvidersExpr`,
// or a `TargetSet<ActionQueryNode>` (which would have been produced from a previous aquery). For `TargetExpr`
// and `ProvidersExpr`, we need to pass the aquery delegate a list of configured providers labels, and it will
// run analysis on them to construct the `ActionQueryNode`s.
async fn unpack_action_nodes<'v>(
this: &StarlarkAQueryCtx<'v>,
dice: &mut DiceComputations<'_>,
expr: UnpackActionNodes<'v>,
) -> buck2_error::Result<TargetSet<ActionQueryNode>> {
let aquery_env = get_aquery_env(&this.ctx, &this.global_cfg_options_override).await?;
let providers = match expr {
UnpackActionNodes::ActionQueryNodes(action_nodes) => {
return Ok(action_nodes.into_iter().map(|v| v.0).collect());
}
UnpackActionNodes::ActionQueryNodesSet(action_nodes) => return Ok(action_nodes.0.clone()),
UnpackActionNodes::StarlarkActions(actions) => {
let action_keys: Vec<ActionKey> = actions
.into_iter()
.map(|starlark_action| starlark_action.0.key().dupe())
.collect();
return aquery_env.get_action_nodes(dice, action_keys).await;
}
UnpackActionNodes::ConfiguredProviders(arg) => {
ProvidersExpr::<ConfiguredProvidersLabel>::unpack(
arg,
&this.global_cfg_options_override,
&this.ctx,
dice,
)
.await?
.labels()
.cloned()
.collect()
}
UnpackActionNodes::ConfiguredTargets(arg) => {
TargetListExpr::<ConfiguredTargetNode>::unpack_opt(
arg,
&this.global_cfg_options_override,
&this.ctx,
dice,
true,
false,
)
.await?
.as_provider_labels()
}
};
let (incompatible_targets, result) = aquery_env.get_target_set(dice, providers).await?;
if !incompatible_targets.is_empty() {
this.ctx.print_to_error_stream(
IncompatiblePlatformReason::skipping_message_for_multiple(incompatible_targets.iter()),
)?;
}
Ok(result)
}
/// The context for performing `aquery` operations in bxl. The functions offered on this ctx are
/// the same behaviour as the query functions available within aquery command.
///
/// An instance may be obtained with [`bxl.Context.aquery()`](../Context/#contextaquery).
///
/// Query results are `target_set`s of `action_query_node`s, which supports iteration,
/// indexing, `len()`, set addition/subtraction, and `equals()`.
///
/// Actions can be specified as:
/// - Target expressions (configured targets/providers)
/// - Existing action query nodes or target sets
/// - `bxl.Action` objects (obtained from `ctx.audit().output()`)
#[starlark_module]
fn aquery_methods(builder: &mut MethodsBuilder) {
/// The deps query for finding the transitive closure of dependencies.
fn deps<'v>(
this: &StarlarkAQueryCtx<'v>,
// TODO(nga): parameters should be either positional or named, not both.
universe: UnpackActionNodes<'v>,
#[starlark(default = NoneOr::None)] depth: NoneOr<i32>,
#[starlark(default = NoneOr::None)] filter: NoneOr<&'v str>,
eval: &mut Evaluator<'v, '_, '_>,
) -> starlark::Result<StarlarkTargetSet<ActionQueryNode>> {
Ok(this
.ctx
.via_dice(eval, |dice| {
dice.via(|dice| {
async {
let filter = filter
.into_option()
.try_map(buck2_query_parser::parse_expr)?;
let universe = unpack_action_nodes(this, dice, universe).await?;
let aquery_env =
get_aquery_env(&this.ctx, &this.global_cfg_options_override).await?;
aquery_env
.deps(
dice,
&universe,
depth.into_option().into(),
filter
.as_ref()
.map(|span| CapturedExpr { expr: span })
.as_ref(),
)
.await
}
.boxed_local()
})
})
.map(StarlarkTargetSet::from)?)
}
/// Obtain all the actions declared within the analysis of a given target.
///
/// This operation only makes sense on a target literal (it is a simple passthrough when passed
/// an action).
fn all_actions<'v>(
this: &StarlarkAQueryCtx<'v>,
// TODO(nga): parameters should be either positional or named, not both.
targets: UnpackActionNodes<'v>,
eval: &mut Evaluator<'v, '_, '_>,
) -> starlark::Result<StarlarkTargetSet<ActionQueryNode>> {
Ok(this
.ctx
.via_dice(eval, |dice| {
dice.via(|dice| {
async {
let targets = unpack_action_nodes(this, dice, targets).await?;
get_aquery_env(&this.ctx, &this.global_cfg_options_override)
.await?
.all_actions(dice, &targets)
.await
}
.boxed_local()
})
})
.map(StarlarkTargetSet::from)?)
}
/// Obtain the actions for all the outputs provided by the `DefaultInfo` for the targets passed
/// as input. This includes both the `default_outputs` and `other_outputs`.
///
/// This operation only makes sense on a target literal (it does nothing if passed something
/// else).
fn all_outputs<'v>(
this: &StarlarkAQueryCtx<'v>,
// TODO(nga): parameters should be either positional or named, not both.
targets: UnpackActionNodes<'v>,
eval: &mut Evaluator<'v, '_, '_>,
) -> starlark::Result<StarlarkTargetSet<ActionQueryNode>> {
Ok(this
.ctx
.via_dice(eval, |dice| {
dice.via(|dice| {
async {
let targets = unpack_action_nodes(this, dice, targets).await?;
get_aquery_env(&this.ctx, &this.global_cfg_options_override)
.await?
.all_outputs(dice, &targets)
.await
}
.boxed_local()
})
})
.map(StarlarkTargetSet::from)?)
}
/// The attrfilter query for rule attribute filtering.
fn attrfilter<'v>(
this: &StarlarkAQueryCtx<'v>,
// TODO(nga): parameters should be either positional or named, not both.
attr: &str,
value: &str,
targets: UnpackActionNodes<'v>,
eval: &mut Evaluator<'v, '_, '_>,
) -> starlark::Result<StarlarkTargetSet<ActionQueryNode>> {
Ok(this.ctx.via_dice(eval, |dice| {
dice.via(|dice| {
async {
let targets = unpack_action_nodes(this, dice, targets).await?;
targets
.attrfilter(attr, &|v| Ok(v == value))
.map(StarlarkTargetSet::from)
}
.boxed_local()
})
})?)
}
/// Evaluates some general query string. `query_args` can be a target_set of unconfigured nodes, or
/// a list of strings. Returns a `dict` of target labels mapped to their `target_set` results if `query_args`
/// was passed in, otherwise returns a single `target_set`.
///
/// Sample usage:
/// ```python
/// def _impl_eval(ctx):
/// result = ctx.aquery().eval(":foo")
/// ctx.output.print(result)
/// ```
fn eval<'v>(
this: &StarlarkAQueryCtx<'v>,
query: &'v str,
#[starlark(default = NoneOr::None)] query_args: NoneOr<UnpackUnconfiguredQueryArgs<'v>>,
eval: &mut Evaluator<'v, '_, '_>,
) -> starlark::Result<Value<'v>> {
let query_args = match query_args {
NoneOr::None => Vec::new(),
NoneOr::Other(query_args) => query_args.into_strings(),
};
let heap = eval.heap();
Ok(this.ctx.via_dice(eval, |dice| {
dice.via(|dice| {
async {
parse_query_evaluation_result(
QUERY_FRONTEND
.get()?
.eval_aquery(
dice,
&this.ctx.working_dir()?,
query,
&query_args,
this.global_cfg_options_override.clone(),
)
.await?,
heap,
)
}
.boxed_local()
})
})?)
}
}