bazelbuild / bazelbuild/bazel

CcToolchainConfigInfo variable libraries_to_link has no field shared_libraries

Open
#26,546 2 comments 0 reactions 0 assignees View on GitHub
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 learn to write cc-toolchain from [cc-toolchain-config-reference](https://bazel.build/versions/8.3.0/docs/cc-toolchain-config-reference#flag-groups), I want to add some flags before each shared library like `-Wl,--as-needed foo.so`, so I add a feature
```
feature(
name = "default_linker_flags",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
],
flag_groups = ([
flag_group(
flags = ["-lstdc++"],
),
flag_group (
iterate_over = "libraries_to_link",
flag_groups = [
flag_group (
iterate_over = "libraries_to_link.shared_libraries",
flags = ["-Wl,--as-needed", "%{libraries_to_link.shared_libraries.path}"],
),
],
)
]),
),
],
),
```
but get
```
Linking hello_lib.so failed: Invalid toolchain configuration: Cannot expand variable 'libraries_to_link.shared_libraries': structure libraries_to_link doesn't have a field named 'shared_libraries'
```

finally I solve it by
```
flag_group (
iterate_over = "libraries_to_link",
flag_groups = [
flag_group(
flags = ["-Wl,--as-needed", "%{libraries_to_link.path}"],
expand_if_equal = variable_with_value(
name = "libraries_to_link.type",
value = "versioned_dynamic_library",
),
),
],
expand_if_available = "libraries_to_link",
),
```
and
```
flag_group (
iterate_over = "libraries_to_link",
flags = ["-Wl,--as-needed", "%{libraries_to_link.path}"],
expand_if_equal = variable_with_value(
name = "libraries_to_link.type",
value = "versioned_dynamic_library",
),
),
```
doesn't work

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

Repo with an empty MODULE.bazel and:

BUILD.bazel:

```
cc_binary(
name = "hello_world",
srcs = ["hello_world.cpp"],
deps = [":hello_lib_import"],
)

cc_binary(
name = "hello_lib.so",
srcs = ["hello_lib.cpp"],
linkshared = True,
copts = ["-fPIC"],
visibility = ["//visibility:public"],
)

cc_import(
name = "hello_lib_import",
shared_library = ":hello_lib.so",
hdrs = [],
visibility = ["//visibility:public"],
)
```
hello_lib.cpp
```
#include

extern "C" void hello_from_lib() {
std::cout << "Hello from shared library!" << std::endl;
}
```

hello_world.cpp

```
#include

extern "C" void hello_from_lib();

int main() {
std::cout << "Hello, Bazel!" << std::endl;
hello_from_lib();
return 0;
}
```

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

ubuntu 18.04

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

8.3.0

### 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` ?

```text

```

### If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

_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 cc-toolchain-config-reference and reproduce the failure using the provided BUILD.bazel and C++ sources on Bazel 8.3.0. Trace how the feature's flag_group expands libraries_to_link and its available fields, then verify the behavior against the supplied shared-library example. Done means the documented variable structure and actual expansion behavior agree, with the reported configuration no longer failing unexpectedly.

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.