bazelbuild / bazelbuild/bazel

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

Open
#18,718 0 comments 0 reactions 0 assignees View on GitHub
help wanted P3 team-Rules-CPP type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

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 `.
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

Open the contributing guide

Research direction

Start by reproducing the minimal cc_library example from the issue with strip_include_prefix on Bazel 6.2.0, then inspect the generated _virtual_includes tree. The work is done when including third_party/package-a/header.hpp works without repeating the repository path in include_prefix, with regression coverage for the relative-path case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.