bazelbuild/bazel

strip_include_prefix for relative path doesn't include prior prefix in tree

Open

#18,718 opened on Jun 19, 2023

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (4,465 forks)batch import
P3help wantedteam-Rules-CPPtype: bug

Repository metrics

Stars
 (25,384 stars)
PR merge metrics
 (Avg merge 22d 20h) (77 merged PRs in 30d)

Description

Description of the bug:

I have some code, which for ideological reasons, I've moved into a directory third_party in my repository rather than using a repository rule.

Here is an example

third_party
└── package-a
      ├── include
      │        └── header.hpp
      └─── source.cpp

I keep the include directory so that the source checked into third_party matches the layout of the organizational structure of the 3rd party repository as much as possible.

Here is a minimal cc_library I wanted to create

cc_library(
    name = "package-a",
    hdrs = ["include/header.hpp"],
    srcs = ["source.cpp"]
    strip_include_prefix = "include"
    visibility = ["//visibility:public"],
)

I can correctly include the header using the following: #include <package-a/header.hpp>. This is evident by the virtual_includes created: bazel-out/k8-dbg/bin/third_party/package-a/_virtual_includes/package-a

What I consider to be the "bug" however is that I cannot include the file starting from "third_party" with the prefix included.

I would like to include as follows: #include "third_party/package-a/header.hpp"

Most use of strip_include_prefix is probably from external dependencies brought in via repository rules. In those cases, the include directory is likely already at the root of the repository so this bug does not manifest itself.

Workaround

There is a workaround, which is a bit repetitive but achieves the desired result is by adding include_prefix.

cc_library(
    name = "package-a",
    hdrs = ["include/header.hpp"],
    srcs = ["source.cpp"]
    strip_include_prefix = "include"
    include_prefix = "third_party/package-a"
    visibility = ["//visibility:public"],
)

It's a bit odd to re-declare the position in my repository again via "third_party/package-a" in the include_prefix

Which operating system are you running Bazel on?

Linux (6.1.25-1rodete1-amd64)

What is the output of bazel info release?

release 6.2.0

Have you found anything relevant by searching the web?

I saw https://github.com/bazelbuild/bazel/issues/7568 is somewhat a similar request to have more control of the header mapping.

As a Googler, I find the output of strip_include_prefix incorrect as a I believe internally it also allows you to include with the relative prefix missing starting from third_party,

Contributor guide