hedronvision / bazel-compile-commands-extractor

Goal: Enable awesome tooling for Bazel users of the C language family.
Other
696 stars 112 forks source link

Third party library library from Bazel (sub)repository is not recognized #183

Closed ormandi closed 7 months ago

ormandi commented 7 months ago

Context:

I have a Bazel repository that:

Still I'm getting Bazel lists no applicable compile commands @third_party_repo//:third_party_lib error message, and the corresponding headers are not found even I also add test targets (binaries) depending on the third party library.

Example output:

...
>>> Analyzing commands used in //first/party:some_test
>>> Finished extracting commands for //first/party:some_test
...
>>> Analyzing commands used in @third_party_repo//:third_party_lib
>>> Bazel lists no applicable compile commands for @third_party_repo//:third_party_lib
    If this is a header-only library, please instead specify a test or binary target that compiles it (search "header-only" in README.md).
    Continuing gracefully...
...
$ bazel query "somepath(//first/party:some_test, @third_party_repo//:third_party_lib)"
//first/party:some_test
@third_party_repo//:third_party_lib
Loading: 0 packages loaded
$ bazel test -c opt --test_output=all --nocache_test_results //first/party:some_test
...
//first/party:some_test                                 PASSED in 1.0s

Executed 1 out of 1 test: 1 test passes.
...
INFO: Build completed successfully, 35 total actions

Obviously I use things from third_party_lib in some_test.

What hapeens?

But in VSCode I'm getting the the "usual" 'third_paty.hpp' file not found clang(pp_file_not_found) and Use of undeclared identifier 'third_paty_scope' clang(undeclared_var_use) erros.

What is the expectation?

VSCode should know about third_paty.hpp and third_paty_scope and should not yield error.

Notes

Any idea how to fix this?

ormandi commented 7 months ago

Btw, after rereading what I wrote and poking around my codebase a bit, I realized that the culprit is probably the presence of foreign cc rules here, since I have other third party dependencies (e.g. gtest (https://github.com/google/googletest) and absl (https://github.com/abseil/abseil-cpp)) where the underlying build happens using Bazel (and not foreign cc rules) since those are Bazel-based libraries at the first place.

I.e. comparing the example of third_party_lib library mentioned in my original post, and e.g. my absl third party dependency; the only difference is that the third_party_lib uses foreign_cc_rule 's make whereas absl does not since it comes with Bazel BUILD files; and yet VSCode can find the absl headers, but not the headers corresponding to third_party_lib.

Also this makes a lot of sense, since I assume foreign cc rule does not exhibit compile commands as those build steps are really described within the underlying build system of the third party package (e.g. make). Would this be the case?

If so, my follow-up question is this: Is there a workaround here? I.e. is there a way to e.g. manually provide some kind of "fake" build commands for foreign rule cc-based targets to make these targets accessible by VSCode?

ormandi commented 7 months ago

I've just found: https://github.com/hedronvision/bazel-compile-commands-extractor/blob/main/ImplementationReadme.md Pretty cool that you're offering these insights.

ormandi commented 7 months ago

Ah, no bug here.. Apologies, it is my bad.

Upon finally finding Clangds output on processing compile_commands.json, I've noticed lines like: bazel-out/darwin-fastbuild/bin/external/third_party_lib/include. Too bad I usually run everything with -c opt (unless I actually debug something) due to a bunch of benchmark being present in the codebase requiring optimized code. Of course, -c opt writes into bazel-out/darwin-opt/...... So, the mentioned header file was missing under fastbuild.

After building without -c opt fixed the everything. I may call this out somewhere visible.