Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file based filter #99

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c13cf11
file based filter
xinzhengzhang Dec 23, 2022
0ea3fd6
Unify --file parsing logic
cpsauer Dec 24, 2022
4244747
file based filter: deal with header lib
xinzhengzhang Mar 18, 2023
0befc09
End loop if header is found & Add max attempts limit
xinzhengzhang Mar 3, 2023
92e83c1
Improve my language for --file docstring
cpsauer Mar 18, 2023
bf0405e
rm dupe itertools import
cpsauer Mar 18, 2023
9d7f9ab
use variable above
xinzhengzhang Mar 27, 2023
9903410
check file if resolved in a smaller scope
xinzhengzhang Mar 27, 2023
d7e1e31
Improve --file editor wrapper message
cpsauer Apr 24, 2023
d6f60cb
Fix no output error message and spacing.
cpsauer Apr 24, 2023
2b69f05
Fix small old typo
cpsauer May 2, 2023
242d601
Prevent template substitution in comment
cpsauer May 2, 2023
e17428f
ws
cpsauer May 2, 2023
5be98b1
Further improve --file editor wrapper message
cpsauer May 2, 2023
2e980ab
Alphabetize threading import
cpsauer May 2, 2023
8d5f2fe
Document interaction of --file and exclude_* template arguments
cpsauer May 2, 2023
e288d49
Entangled cleanups around thread structure
cpsauer May 2, 2023
4a251e5
Reduce unnecessary work with in header finding with --file flag
cpsauer May 2, 2023
0d86038
improve comments in _get_command
cpsauer May 2, 2023
da95ea6
Mark problems, fix typos, fix depleted iterator bug
cpsauer May 2, 2023
2a10207
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 2, 2023
f4fa941
Fix aquery inputs() statement not working for headers
cpsauer May 3, 2023
2e10a53
Add a few more WIP notes
cpsauer May 3, 2023
0851bca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 3, 2023
0f120b6
Fix typo
cpsauer May 3, 2023
9d23866
Add note about allpaths issue
cpsauer May 3, 2023
2d09c75
Refine and note discussion points in _filter_through_headers
cpsauer May 4, 2023
0722615
Improve TODOs
cpsauer May 4, 2023
14e707e
Enrich todos and notes after another read through of the bazel [ac]qu…
cpsauer May 4, 2023
bd7bde1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2023
89f583a
refresh.template.py: Estimate the number of actions that can be run b…
xinzhengzhang May 5, 2023
ef33509
refresh.template.py: quoting target_statement
xinzhengzhang May 5, 2023
f8b046e
refersh.template.py: running a preliminary query to get specified fi…
xinzhengzhang May 5, 2023
9ce1716
refresh.template.py: simplify/unify logic around target_statement_can…
xinzhengzhang May 5, 2023
40b9131
refresh.template.py: changed the _convert_compile_commands to return …
xinzhengzhang May 5, 2023
6a6777a
normalize file path if the --file is a subpath of the cwd
xinzhengzhang May 5, 2023
0453be4
refresh.template.py: for --file, don't continue traversing targets af…
xinzhengzhang May 5, 2023
97644fd
Revert "refresh.template.py: for --file, don't continue traversing ta…
xinzhengzhang May 6, 2023
e8a986b
Revert "normalize file path if the --file is a subpath of the cwd"
xinzhengzhang May 6, 2023
e67c0bd
Revert "refresh.template.py: changed the _convert_compile_commands to…
xinzhengzhang May 6, 2023
b785c3e
Revert "refresh.template.py: simplify/unify logic around target_state…
xinzhengzhang May 6, 2023
a152055
Revert "refersh.template.py: running a preliminary query to get spec…
xinzhengzhang May 6, 2023
1d8f62e
Revert "refresh.template.py: quoting target_statement"
xinzhengzhang May 6, 2023
33b2310
Revert "refresh.template.py: Estimate the number of actions that can …
xinzhengzhang May 6, 2023
84822ba
Add link to deduped aspects cquery/aquery aspects issue
cpsauer May 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unify --file parsing logic
Promote multiple --file assert to log_error
Why? It's a usage error we're communicating to the user, not a code invariant we're checking in debug mode.
We could recover, but we don't, because we expect only automated usage and want to give clear feedback.

More pythonic if

Improve my --file flag messaging consistency

Call library to escape regex
Helps avoid edge cases! Also handles the most common special character, '.'

Add an error to catch unsupported --file <file> form

Tweak -- logic now that --file arguments could be after

--file docs

Make aquery spacing consistent

Flip the if statement in constructor target_statment

Use let to combine attr query

Merge compile_commands.json if --file

Small simplification: endswith already can take a tuple of possibilities (as elsewhere)

Small simplification of header path for file flag.
(This code section likely to be replaced later, though.)

Fix my typo on --file= discoverable docs

Some tweaks to merge
Adds note about behavior, some edge case handling around other flags that might start with --file, permissions issues, etc.
  • Loading branch information
cpsauer authored and xinzhengzhang committed Mar 18, 2023
commit 0ea3fd662a54bdb835107f65850f9fb0bf737d8c
49 changes: 32 additions & 17 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

Interface (after template expansion):
- `bazel run` to regenerate compile_commands.json, so autocomplete (and any other clang tooling!) reflect the latest Bazel build files.
- No arguments are needed; info from the rule baked into the template expansion.
- No arguments are needed; info from the rule is baked into the template expansion.
- Any arguments passed are interpreted as arguments needed for the builds being analyzed.
- The one exception is --file=<file_target>, which can be used to update commands for just one file. This is intended for programmatic use from editor plugins.
- Requires being run under Bazel so we can access the workspace root environment variable.
- Output: a compile_commands.json in the workspace root that clang tooling (or you!) can look at to figure out how files are being compiled by Bazel
- Crucially, this output is de-Bazeled; The result is a command that could be run from the workspace root directly, with no Bazel-specific requirements, environment variables, etc.
Expand Down Expand Up @@ -835,14 +836,20 @@ def _get_commands(target: str, flags: str):
# Log clear completion messages
log_info(f">>> Analyzing commands used in {target}")

# Pass along all arguments to aquery, except for --file=
additional_flags = shlex.split(flags) + [arg for arg in sys.argv[1:] if not arg.startswith('--file=')]
file_flags = [arg for arg in sys.argv[1:] if arg.startswith('--file=')]
assert len(file_flags) < 2, f"Only one or zero --file is supported current args = {sys.argv[1:]}"
file_flags = [arg[len('--file='):] for arg in sys.argv[1:] if arg.startswith('--file=')]
if len(file_flags) > 1:
log_error(">>> At most one --file flag is supported.")
sys.exit(1)
if any(arg.startswith('--file') for arg in additional_flags):
log_error(">>> Only the --file=<file_target> form is supported.")
sys.exit(1)

# Detect anything that looks like a build target in the flags, and issue a warning.
# Note that positional arguments after -- are all interpreted as target patterns. (If it's at the end, then no worries.)
# Note that positional arguments after -- are all interpreted as target patterns.
# And that we have to look for targets. checking for a - prefix is not enough. Consider the case of `-c opt` leading to a false positive
if ('--' in additional_flags[:-1]
if ('--' in additional_flags
or any(re.match(r'-?(@|:|//)', f) for f in additional_flags)):
log_warning(""">>> The flags you passed seem to contain targets.
Try adding them as targets in your refresh_compile_commands rather than flags.
Expand All @@ -860,25 +867,22 @@ def _get_commands(target: str, flags: str):
if {exclude_external_sources}:
# 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.
target_statment = f"filter('^(//|@//)',{target_statment})"
if (len(file_flags) == 1):
# Strip --file=
file_path = file_flags[0][7:]
# Query escape
file_path = file_path.replace("+", "\+").replace("-", "\-")
# For header file we try to find from hdrs and srcs to get the targets
if file_path.endswith('.h'):
# Since attr function can't query with full path, get the file name to query
head, tail = os.path.split(file_path)
target_statment = f"attr(hdrs, '{tail}', {target_statment}) + attr(srcs, '{tail}', {target_statment})"
if file_flags:
file_path = file_flags[0]
if file_path.endswith(_get_files.source_extensions):
target_statment = f"inputs('{re.escape(file_path)}', {target_statment})"
else:
target_statment = f"inputs('{file_path}', {target_statment})"
# For header files we try to find from hdrs and srcs to get the targets
# Since attr function can't query with full path, get the file name to query
fname = os.path.basename(file_path)
target_statment = f"let v = {target_statment} in attr(hdrs, '{fname}', $v) + attr(srcs, '{fname}', $v)"
aquery_args = [
'bazel',
'aquery',
# Aquery docs if you need em: https://docs.bazel.build/versions/master/aquery.html
# Aquery output proto reference: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
# One bummer, not described in the docs, is that aquery filters over *all* actions for a given target, rather than just those that would be run by a build to produce a given output. This mostly isn't a problem, but can sometimes surface extra, unnecessary, misconfigured actions. Chris has emailed the authors to discuss and filed an issue so anyone reading this could track it: https://github.com/bazelbuild/bazel/issues/14156.
f"mnemonic('(Objc|Cpp)Compile',{target_statment})",
f"mnemonic('(Objc|Cpp)Compile', {target_statment})",
# We switched to jsonproto instead of proto because of https://github.com/bazelbuild/bazel/issues/13404. We could change back when fixed--reverting most of the commit that added this line and tweaking the build file to depend on the target in that issue. That said, it's kinda nice to be free of the dependency, unless (OPTIMNOTE) jsonproto becomes a performance bottleneck compated to binary protos.
'--output=jsonproto',
# We'll disable artifact output for efficiency, since it's large and we don't use them. Small win timewise, but dramatically less json output from aquery.
Expand Down Expand Up @@ -1081,6 +1085,17 @@ def _ensure_cwd_is_workspace_root():
log_error(""">>> Not (over)writing compile_commands.json, since no commands were extracted and an empty file is of no use.
There should be actionable warnings, above, that led to this.""")
sys.exit(1)
# --file triggers incremental update of compile_commands.json
if any(arg.startswith('--file=') for arg in sys.argv[1:]) and os.path.isfile('compile_commands.json'):
previous_compile_command_entries = []
try:
with open('compile_commands.json') as compile_commands_file:
previous_compile_command_entries = json.load(compile_commands_file)
except:
log_warning(">>> Couldn't read previous compile_commands.json. Overwriting instead of merging...")
else:
updated_files = set(entry['file'] for entry in compile_command_entries)
compile_command_entries += [entry for entry in previous_compile_command_entries if entry['file'] not in updated_files]

# Chain output into compile_commands.json
with open('compile_commands.json', 'w') as output_file:
Expand Down
2 changes: 2 additions & 0 deletions refresh_compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ refresh_compile_commands(
# exclude_headers = "external",
# Still not fast enough?
# Make sure you're specifying just the targets you care about by setting `targets`, above.
# That's still not enough; I'm working on a huge codebase!
# This tool supports a fast, incremental mode that can be used to add/update commands as individual files are opened. If you'd be willing to collaborate on writing a simple editor plugin invokes this tool on file open, please write in! (And see --file flag in refresh.template.py)
```
"""

Expand Down