bazelbuild / bazelbuild/rules_cc

`cc_binary` prints `False` instead of a Label from `throw_linked_but_not_exported_errors`

Open Beginner friendly
#841 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
247
Forks
196
PR merge metrics
No merged PRs in 30d

Description

The `libraries were linked statically by different cc_shared_libraries but not exported` error message does not print the offending shared library label when it is thrown from a `cc_binary`; instead it just says `False` as the label.

Given the following BUILD file:
```
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_shared_library")

cc_library(
name = "stlib",
srcs = glob(["*.cxx"]),
)
cc_library(
name = "shlibA-headers",
hdrs = glob(["*.h"]),
deps = ["stlib"],
)
cc_library(
name = "shlibA-exports",
srcs = glob(["*.cxx"]),
deps = ["shlibA-headers"],
)
cc_shared_library(
name = "shlibA",
deps = ["shlibA-exports"],
)

cc_binary(
name = "binary",
deps = ["stlib"],
dynamic_deps = ["shlibA"],
)

cc_library(
name = "shlibB-headers",
hdrs = glob(["*.h"]),
deps = ["shlibA-headers"],
)
cc_library(
name = "shlibB-exports",
srcs = glob(["*.cxx"]),
deps = ["shlibB-headers"],
)
cc_shared_library(
name = "shlibB",
deps = ["shlibB-exports"],
dynamic_deps = ["shlibA"],
)
```

Building `shlibB`:
```
> bazel build //:shlibB
ERROR: Q:/ws/BUILD:83:18: in cc_shared_library rule //:shlibB:
Traceback (most recent call last):
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_shared_library.bzl", line 656, column 114, in _cc_shared_library_impl
(exports, linker_inputs, curr_link_once_static_libs_set, precompiled_only_dynamic_libraries) = _filter_inputs(
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_shared_library.bzl", line 519, column 42, in _filter_inputs
_throw_linked_but_not_exported_errors(linked_statically_but_not_exported)
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_shared_library.bzl", line 537, column 9,
in _throw_linked_but_not_exported_errors
fail("".join(error_builder))
Error in fail: The following libraries were linked statically by different cc_shared_libraries but not exported:
cc_shared_library @@//:shlibA:
"@@//:stlib",
If you are sure that the previous libraries are exported by the cc_shared_libraries because:
1. You have visibility declarations in the source code
2. Or you are passing a visibility script to the linker to export symbols from them
then add those libraries to roots or exports_filter for each cc_shared_library.
ERROR: Q:/ws/BUILD:83:18: Analysis of target '//:shlibB' (config: 7603697) failed
```

Building `binary`:
```
> bazel build //:binary
ERROR: Q:/ws/BUILD:63:10: in cc_binary rule //:binary:
Traceback (most recent call last):
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_binary.bzl", line 856, column 44, in _impl
binary_info, providers = cc_binary_impl(ctx, [])
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_binary.bzl", line 665, column 110, in cc_binary_impl
cc_linking_outputs_binary, cc_launcher_info, deps_cc_linking_context = _create_transitive_linking_actions(
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_binary.bzl", line 385, column 75, in _create_transitive_linking_actions
cc_linking_context = _filter_libraries_that_are_linked_dynamically(ctx, feature_configuration, cc_linking_context)
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_binary.bzl", line 284, column 41, in _filter_libraries_that_are_linked_dynamically
throw_linked_but_not_exported_errors(linked_statically_but_not_exported)
File "Q:/_b/yccmakuk/external/rules_cc+/cc/private/rules_impl/cc_shared_library.bzl", line 537, column 9,
in _throw_linked_but_not_exported_errors
fail("".join(error_builder))
Error in fail: The following libraries were linked statically by different cc_shared_libraries but not exported:
cc_shared_library False:
"@@//:stlib",
If you are sure that the previous libraries are exported by the cc_shared_libraries because:
1. You have visibility declarations in the source code
2. Or you are passing a visibility script to the linker to export symbols from them
then add those libraries to roots or exports_filter for each cc_shared_library.
ERROR: Q:/ws/BUILD:63:10: Analysis of target '//:binary' (config: 7603697) failed
```

This appears to be due to the following difference:
- [`cc_shared_library`](https://github.com/bazelbuild/rules_cc/blob/679c0e48f2d4df5ef78cffc4cfcb50f5ad3c0c38/cc/private/rules_impl/cc_shared_library_impl.bzl#L450): `linked_statically_but_not_exported.setdefault(link_once_static_libs_map[owner], []).append(owner)`
- [`cc_binary`](https://github.com/bazelbuild/rules_cc/blob/679c0e48f2d4df5ef78cffc4cfcb50f5ad3c0c38/cc/private/rules_impl/cc_binary_impl.bzl#L264): `linked_statically_but_not_exported.setdefault(targets_to_be_linked_statically_map[owner], []).append(owner)`

Note that I am building using `rules_cc` version `0.2.16`, but the difference in the two files above still exists on `main`.
If I swap my local copy of `cc_binary` to use `link_once_static_libs_map`, the error message works correctly for me.

It looks like the `cc_binary` version was swapped from `link_once_static_libs_map` to `targets_to_be_linked_statically_map` back in 2023, when `cc_binary` was still part of Bazel: https://github.com/bazelbuild/bazel/commit/a77245215e61187dba763dd619cc60c62fb346bb
I'm not sure if there were other reasons to change this line for cc_binary but not cc_shared_library in that commit.

Contributor guide

Open the contributing guide

Research direction

Start with the linked locations in cc_binary_impl.bzl and cc_shared_library_impl.bzl, comparing how each builds linked_statically_but_not_exported. Reproduce the issue with the provided BUILD file and bazel build //:binary. Done means the error identifies the offending shared library with its Label rather than printing False, while the cc_shared_library behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.