hedronvision / bazel-compile-commands-extractor

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

Header not on include path: Cannot include external headers. #136

Closed felberj closed 10 months ago

felberj commented 1 year ago

Note: I think this might be a clangd issue. I decided to post this issue anyways because maybe its not. I am happy to contribute a fix to clangd or this repository, but I am still investigating where the actual bug lies.

Symptoms:

I cant include "external" headers anymore:

My logs are full of

SNIP [stderr] I[15:08:14.654] Failed to generate include insertion edits for adding header (FileURI='file:///private/var/tmp/_bazel_jonas/0fa0c77467b4f4c4aae1dcdcec9af663/external/com_google_absl/absl/strings/string_view.h', IncludeHeader='file:///private/var/tmp/_bazel_jonas/0fa0c77467b4f4c4aae1dcdcec9af663/external/com_google_absl/absl/strings/string_view.h') into /Users/jonas/src/my/project/foo.cc: Header not on include path SNIP

I assume the error comes from here and more specifically here

I assume this might have something to do with resolving the "external" symlink so it cannot generate a good include path anymore?

Lets look at compile_commands.json:

  {
    "file": "external/com_google_absl/absl/strings/string_view.h",
    "arguments": [
      "clang",
      "-xc++",
      "-D_FORTIFY_SOURCE=1",
      "-fstack-protector",
      "-fcolor-diagnostics",
      "-Wall",
      "-Wthread-safety",
      "-Wself-assign",
      "-fno-omit-frame-pointer",
      "-O0",
      "-DDEBUG",
      "-std=c++11",
      "-iquote",
      ".",
      "-iquote",
      "bazel-out/darwin_arm64-fastbuild/bin",
      "-iquote",
      "external/com_google_absl",
      "-iquote",
      "bazel-out/darwin_arm64-fastbuild/bin/external/com_google_absl",
      "-MD",
      "-MF",
      "bazel-out/darwin_arm64-fastbuild/bin/model/_objs/context/context.d",
      "-DBAZEL_CURRENT_REPOSITORY=\"\"",
      "-frandom-seed=bazel-out/darwin_arm64-fastbuild/bin/model/_objs/context/context.o",
      "-isysroot",
      "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
      "-F/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
      "-F/Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/Library/Frameworks",
      "-no-canonical-prefixes",
      "-pthread",
      "--std=c++20",
      "-no-canonical-prefixes",
      "-Wno-builtin-macro-redefined",
      "-D__DATE__=\"redacted\"",
      "-D__TIMESTAMP__=\"redacted\"",
      "-D__TIME__=\"redacted\"",
      "-target",
      "arm64-apple-macosx12.3",
      "-c",
      "model/context.cc",
      "-o",
      "bazel-out/darwin_arm64-fastbuild/bin/model/_objs/context/context.o"
    ],
    "directory": "/Users/jonas/src/my_project"
  },

We have

      "-iquote",
      "external/com_google_absl",

and the symlink resolves to the same path that is in the error message:

 % realpath external/com_google_absl
/private/var/tmp/_bazel_jonas/0fa0c77467b4f4c4aae1dcdcec9af663/external/com_google_absl

(Side note) I am also sure why the compile command has the following:

      "-c",
      "model/context.cc",
      "-o",
      "bazel-out/darwin_arm64-fastbuild/bin/model/_objs/context/context.o"

So this smells like a clangd bug to me. It looks like they dont resolve the include path. I will continue with my investigation next week. Pointers welcome :)

% clangd --version
Homebrew clangd version 16.0.6
Features: mac+xpc
Platform: arm64-apple-darwin22.5.0

hedron_git_hash = "3dddf205a1f5cde20faf2444c1757abe0564ff4c"

Steps to reproduce:

0. Setup files

BUILD

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "main",
    srcs = [
        "main.cc",
    ],
    deps = [
        "@com_google_absl//absl/strings",
    ],
)

WORKSPACE

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
hedron_git_hash = "3dddf205a1f5cde20faf2444c1757abe0564ff4c"
http_archive(
    name = "hedron_compile_commands",

    # Replace the commit hash in both places (below) with the latest, rather than using the stale one here.
    # Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
    url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/" + hedron_git_hash + ".tar.gz",
    strip_prefix = "bazel-compile-commands-extractor-" + hedron_git_hash,
    # When you first run this tool, it'll recommend a sha256 hash to put here with a message like: "DEBUG: Rule 'hedron_compile_commands' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = ..."
)
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")
hedron_compile_commands_setup()

# absl
http_archive(
  name = "com_google_absl",
  urls = ["https://github.com/abseil/abseil-cpp/archive/e85868cbef04e8e8ff518e09b6af3970bbe5d2eb.zip"],
  strip_prefix = "abseil-cpp-e85868cbef04e8e8ff518e09b6af3970bbe5d2eb",
)
http_archive(
  name = "bazel_skylib",
  urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz"],
  sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
)

main.cc

#include "absl/strings/string_view.h"

int main() {
  return 0;
}

(my .bazelrc also needed build --cxxopt="--std=c++20")

1. generate json

% bazel run @hedron_compile_commands//:refresh_all

2. delete include from main.cc

int main() {
  return 0;
}

3. edit main.cc

I tried to add absl::string_view and then the error comes up.

felberj commented 1 year ago

Relecant clangd issues: https://github.com/clangd/clangd/issues/124 and from that, probably https://github.com/llvm/llvm-project/commit/b324c64b6d4c807c44d2aba4b408dd573b850f1b. Looks like clangd has changed their handling of symlinks.

cemlyn007 commented 1 year ago

Do you know of any work arounds at the moment? (I am a big noob)

cpsauer commented 10 months ago

Hey guys--any updates here that I should know?

(Sorry I've been so slow to reply. Was just totally underwater on other urgent things.)

Seems like y'all pretty clearly scoped this down to a clangd issue, so I'm going to close it here for now, but holler if you think that's a mistake and we can open it right back up.

crossXliu commented 2 months ago

I replaced paths that follow the -iquote or -isystem flags with their real paths in the _convert_compile_commands function in refresh.template.py. After making these changes, the function worked as expected.