Skip to content

Commit f8b046e

Browse files
committed
refersh.template.py: running a preliminary query to get specified file label
1 parent ef33509 commit f8b046e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

refresh.template.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,13 @@ def _get_commands(target: str, flags: str):
997997
if file_path.endswith(_get_files.source_extensions):
998998
target_statement_candidates.append(f"inputs('{re.escape(file_path)}', {target_statement})")
999999
else:
1000-
fname = os.path.basename(file_path) # TODO consider running a preliminary aquery to make this more specific, getting the targets that generate the given file. Use outputs() aquery function. Should also test the case where the file is generated/is coming in as a filegroup--that's likely to be fixed by this change.
1001-
header_target_statement = f"let v = {target_statement} in attr(hdrs, '{fname}', $v) + attr(srcs, '{fname}', $v)" # Bazel does not list headers as direct inputs, but rather hides them behind "middlemen", necessitating a query like this.
1000+
fname = os.path.basename(file_path)
1001+
label_candidates = subprocess.check_output(['bazel', 'query', f"filter('{fname}$', {target_statement})"], stderr = subprocess.PIPE, text = True).split()
1002+
# TODO compatible with windows file path
1003+
file_candidates = list(filter(lambda label: file_path in label.replace(':', '/'), label_candidates))
1004+
file_statement = '|'.join(file_candidates) if len(file_candidates) > 0 else fname
1005+
1006+
header_target_statement = f"let v = {target_statement} in attr(hdrs, '{file_statement}', $v) + attr(srcs, '{file_statement}', $v)" # Bazel does not list headers as direct inputs, but rather hides them behind "middlemen", necessitating a query like this.
10021007
target_statement_candidates.extend([
10031008
header_target_statement,
10041009
f"allpaths({target}, {header_target_statement})", # Ordering is ideal, breadth-first from the deepest dependency, despite the docs. TODO (1) There's a bazel bug that produces extra actions, not on the path but downstream, so we probably want to pass --noinclude_aspects per https://github.com/bazelbuild/bazel/issues/18289 to eliminate them (at the cost of some valid aspects). (2) We might want to benchmark with --infer_universe_scope (if supported) and --universe-scope=target with query allrdeps({header_target_statement}, <maybe some limited depth>) or rdeps, checking speed but also ordering (the docs indicate it is likely to be lost, which is a problem) and for inclusion of the header target. We'd guess it'll have the same aspects bug as allpaths. (3) We probably also also want to *just* run this query, not the whole list, since it captures the former and is therefore unlikely to add much latency, since a given header is probabably either used internally to the target (find on first match) for header-only (must traverse all paths in all targets until you get a match) for all top-level targets, and since we can separate out the last, see below.

0 commit comments

Comments
 (0)