You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Yields compile_commands.json entries for a given target and flags, gracefully tolerating errors."""
835
+
"""Return compile_commands.json entries for a given target and flags, gracefully tolerating errors."""
836
+
def_get_commands(target_statment):
837
+
aquery_args= [
838
+
'bazel',
839
+
'aquery',
840
+
# Aquery docs if you need em: https://docs.bazel.build/versions/master/aquery.html
841
+
# Aquery output proto reference: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
842
+
# 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.
# 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.
845
+
'--output=jsonproto',
846
+
# 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.
847
+
'--include_artifacts=false',
848
+
# Shush logging. Just for readability.
849
+
'--ui_event_filters=-info',
850
+
'--noshow_progress',
851
+
# Disable param files, which would obscure compile actions
852
+
# Mostly, people enable param files on Windows to avoid the relatively short command length limit.
853
+
# For more, see compiler_param_file in https://bazel.build/docs/windows
854
+
# They are, however, technically supported on other platforms/compilers.
855
+
# That's all well and good, but param files would prevent us from seeing compile actions before the param files had been generated by compilation.
856
+
# Since clangd has no such length limit, we'll disable param files for our aquery run.
857
+
'--features=-compiler_param_file',
858
+
# Disable layering_check during, because it causes large-scale dependence on generated module map files that prevent header extraction before their generation
859
+
# For more context, see https://github.com/hedronvision/bazel-compile-commands-extractor/issues/83
860
+
# If https://github.com/clangd/clangd/issues/123 is resolved and we're not doing header extraction, we could try removing this, checking that there aren't erroneous red squigglies squigglies before the module maps are generated.
861
+
# If Bazel starts supporting modules (https://github.com/bazelbuild/bazel/issues/4005), we'll probably need to make changes that subsume this.
862
+
'--features=-layering_check',
863
+
] +additional_flags
864
+
865
+
aquery_process=subprocess.run(
866
+
aquery_args,
867
+
# MIN_PY=3.7: Replace PIPEs with capture_output.
868
+
stdout=subprocess.PIPE,
869
+
stderr=subprocess.PIPE,
870
+
encoding=locale.getpreferredencoding(),
871
+
check=False, # We explicitly ignore errors from `bazel aquery` and carry on.
872
+
)
873
+
874
+
875
+
# Filter aquery error messages to just those the user should care about.
876
+
# Shush known warnings about missing graph targets.
877
+
# The missing graph targets are not things we want to introspect anyway.
missing_targets_warning: typing.Pattern[str] =re.compile(r'(\(\d+:\d+:\d+\) )?(\033\[[\d;]+m)?WARNING: (\033\[[\d;]+m)?Targets were missing from graph:') # Regex handles --show_timestamps and --color=yes. Could use "in" if we ever need more flexibility.
Try adding them as flags in your refresh_compile_commands rather than targets.
863
931
In a moment, Bazel will likely fail to parse.""")
864
932
933
+
compile_commands= []
865
934
# First, query Bazel's C-family compile actions for that configured target
866
935
target_statment=f'deps({target})'
867
-
if {exclude_external_sources}:
868
-
# 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.
# For header files we try to find from hdrs and srcs to get the targets
876
-
# Since attr function can't query with full path, get the file name to query
877
944
fname=os.path.basename(file_path)
878
-
target_statment=f"let v = {target_statment} in attr(hdrs, '{fname}', $v) + attr(srcs, '{fname}', $v)"
879
-
aquery_args= [
880
-
'bazel',
881
-
'aquery',
882
-
# Aquery docs if you need em: https://docs.bazel.build/versions/master/aquery.html
883
-
# Aquery output proto reference: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
884
-
# 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.
# 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.
887
-
'--output=jsonproto',
888
-
# 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.
889
-
'--include_artifacts=false',
890
-
# Shush logging. Just for readability.
891
-
'--ui_event_filters=-info',
892
-
'--noshow_progress',
893
-
# Disable param files, which would obscure compile actions
894
-
# Mostly, people enable param files on Windows to avoid the relatively short command length limit.
895
-
# For more, see compiler_param_file in https://bazel.build/docs/windows
896
-
# They are, however, technically supported on other platforms/compilers.
897
-
# That's all well and good, but param files would prevent us from seeing compile actions before the param files had been generated by compilation.
898
-
# Since clangd has no such length limit, we'll disable param files for our aquery run.
899
-
'--features=-compiler_param_file',
900
-
# Disable layering_check during, because it causes large-scale dependence on generated module map files that prevent header extraction before their generation
901
-
# For more context, see https://github.com/hedronvision/bazel-compile-commands-extractor/issues/83
902
-
# If https://github.com/clangd/clangd/issues/123 is resolved and we're not doing header extraction, we could try removing this, checking that there aren't erroneous red squigglies squigglies before the module maps are generated.
903
-
# If Bazel starts supporting modules (https://github.com/bazelbuild/bazel/issues/4005), we'll probably need to make changes that subsume this.
904
-
'--features=-layering_check',
905
-
] +additional_flags
906
-
907
-
aquery_process=subprocess.run(
908
-
aquery_args,
909
-
# MIN_PY=3.7: Replace PIPEs with capture_output.
910
-
stdout=subprocess.PIPE,
911
-
stderr=subprocess.PIPE,
912
-
encoding=locale.getpreferredencoding(),
913
-
check=False, # We explicitly ignore errors from `bazel aquery` and carry on.
914
-
)
915
-
916
-
917
-
# Filter aquery error messages to just those the user should care about.
918
-
# Shush known warnings about missing graph targets.
919
-
# The missing graph targets are not things we want to introspect anyway.
missing_targets_warning: typing.Pattern[str] =re.compile(r'(\(\d+:\d+:\d+\) )?(\033\[[\d;]+m)?WARNING: (\033\[[\d;]+m)?Targets were missing from graph:') # Regex handles --show_timestamps and --color=yes. Could use "in" if we ever need more flexibility.
log_warning(f""">>> Bazel lists no applicable compile commands for {file_path} in {target}.
958
+
Continuing gracefully...""")
959
+
else:
960
+
if {exclude_external_sources}:
961
+
# 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.
0 commit comments