Skip to content

Commit 9ce1716

Browse files
committed
refresh.template.py: simplify/unify logic around target_statement_candidates and warning
1 parent f8b046e commit 9ce1716

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

refresh.template.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,8 @@ def _get_compile_commands_for_aquery(aquery_target_statement: str, additional_aq
939939
return []
940940

941941
if not getattr(parsed_aquery_output, 'actions', None): # Unifies cases: No actions (or actions list is empty)
942-
# TODO the different flags warning in stderr is common enough that we might want to either filter it or adjust this messaging.
943-
# TODO simplify/unify logic around warning about not being able to find commands.
944942
if aquery_process.stderr:
945943
log_warning(f""">>> Bazel lists no applicable compile commands for {target}, probably because of errors in your BUILD files, printed above.
946-
Continuing gracefully...""")
947-
elif not focused_on_file:
948-
log_warning(f""">>> Bazel lists no applicable compile commands for {target}
949-
If this is a header-only library, please instead specify a test or binary target that compiles it (search "header-only" in README.md).
950944
Continuing gracefully...""")
951945
return []
952946

@@ -988,12 +982,13 @@ def _get_commands(target: str, flags: str):
988982

989983

990984
# Then, actually query Bazel's compile actions for that configured target
991-
target_statement = f"deps('{target}')"
992-
compile_commands = [] # TODO simplify loop, especially if we can reduce it to one command per case (see below)? Move warning messages outside?
985+
target_statement_candidates = []
986+
file_path = None
987+
compile_commands = []
988+
993989
if file_flags:
994990
file_path = file_flags[0]
995-
found = False
996-
target_statement_candidates = []
991+
target_statement = f"deps('{target}')"
997992
if file_path.endswith(_get_files.source_extensions):
998993
target_statement_candidates.append(f"inputs('{re.escape(file_path)}', {target_statement})")
999994
else:
@@ -1009,24 +1004,30 @@ def _get_commands(target: str, flags: str):
10091004
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.
10101005
f'deps({target})', # TODO: Let's detect out-of-bazel, absolute paths and run this if and only if we're looking for a system header. We need to think about how we want to handle absolute paths more generally, perhaps normalizing them to relative if possible, like with the windows absolute path issue, above.
10111006
])
1012-
for target_statement in target_statement_candidates:
1013-
commands = list(_get_compile_commands_for_aquery(target_statement, additional_flags, file_path))
1014-
compile_commands.extend(commands) # If we did the work to generate a command, we'll update it, whether it's for the requested file or not.
1015-
if any(command['file'].endswith(file_path) for command in commands):
1016-
found = True
1017-
break
1018-
if not found:
1019-
log_warning(f""">>> Couldn't quickly find a compile command for {file_path} in {target}
1020-
Continuing gracefully...""") # TODO simplify/unify logic around warning about not being able to find commands.
10211007
else:
10221008
if {exclude_external_sources}:
10231009
# For efficiency, have bazel filter out external targets (and therefore actions) before they even get turned into actions or serialized and sent to us. Note: this is a different mechanism than is used for excluding just external headers.
1024-
target_statement = f"filter('^(//|@//)',{target_statement})"
1025-
compile_commands.extend(_get_compile_commands_for_aquery(target_statement, additional_flags))
1026-
if not compile_commands:
1027-
log_warning(f""">>> Bazel lists no applicable compile commands for {target}
1010+
target_statement_candidates.append(f"filter('^(//|@//)',{target_statement})")
1011+
1012+
found = False
1013+
for target_statement in target_statement_candidates:
1014+
commands = list(_get_compile_commands_for_aquery(target_statement, additional_flags, file_path))
1015+
compile_commands.extend(commands) # If we did the work to generate a command, we'll update it, whether it's for the requested file or not.
1016+
if file_flags:
1017+
if any(command['file'].endswith(file_path) for command in commands):
1018+
found = True
1019+
break
1020+
log_info(f""">>> Couldn't quickly find a compile command for {file_path} in {target} under {target_statement}
1021+
Continuing gracefully...""")
1022+
1023+
if file_flags and not found:
1024+
log_warning(f""">>> Couldn't quickly find a compile command for {file_path} in {target}
1025+
Continuing gracefully...""")
1026+
1027+
if not compile_commands:
1028+
log_warning(f""">>> Bazel lists no applicable compile commands for {target}
10281029
If this is a header-only library, please instead specify a test or binary target that compiles it (search "header-only" in README.md).
1029-
Continuing gracefully...""") # TODO simplify/unify logic around warning about not being able to find commands.
1030+
Continuing gracefully...""")
10301031

10311032
log_success(f">>> Finished extracting commands for {target}")
10321033
return compile_commands

0 commit comments

Comments
 (0)