bazelbuild / bazelbuild/bazel

`cc_shared_library` dependency of `cc_test` results in redundant shared objects in runfiles

Open
#21,833 6 comments 0 reactions 0 assignees View on GitHub
P3 stale 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:

When a `cc_test` depends on a `cc_shared_library`, it correctly links against the `cc_shared_library`. However, the `cc_library` shared object still ends up in runfiles. This results in building all of the (transitive) dependencies twice.

### Which category does this issue belong to?

C++ Rules

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

https://github.com/brians-neptune/bazel-cc_shared_library-cc_test has a pretty minimal example. Here's the `BUILD` file, all the C/C++ files are empty (except for an empty `main` in `test.cc`):
```python
cc_library(
name = "dep",
srcs = ["dep.cc"],
hdrs = ["dep.h"],
)

cc_shared_library(
name = "dep_shared",
deps = [":dep"],
)

cc_test(
name = "test",
srcs = ["test.cc"],
dynamic_deps = [
":dep_shared",
],
deps = [":dep"],
)
```
`liblibdep.so` and `libdep_shared.so` are both in runfiles:
```shell
$ bazel cquery --output=starlark --starlark:expr='target.data_runfiles.files' test
INFO: Analyzed target //:test (1 packages loaded, 6 targets configured).
INFO: Found 1 target...
depset([, , , ], order = "postorder")
```
but only `libdep_shared.so` is actually used:
```shell
$ ldd bazel-bin/test
linux-vdso.so.1 (0x00007fff5ccd8000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007e330095c000)
libdep_shared.so => /home/brian/Desktop/bazel-cc_shared_library-cc_test/bazel-bin/_solib_k8/_U/libdep_shared.so (0x00007e3300958000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007e3300600000)
/lib64/ld-linux-x86-64.so.2 (0x00007e3300a60000)
```

### Which operating system are you running Bazel on?

Ubuntu 22.04

### What is the output of `bazel info release`?

release 7.0.2

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

_No response_

### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the minimal BUILD example using cc_library, cc_shared_library, and cc_test, then reproduce the issue with the shown bazel cquery and ldd commands. Trace how dynamic_deps and deps contribute to test runfiles; done means the unused cc_library shared object is absent while libdep_shared.so remains available to the test.

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
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.