bazelbuild / bazelbuild/bazel

CcInfo linkopts are not deduplicated across transitions

Open
#19,103 7 comments 2 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:

If you have rules that return CcInfo providers and the same rule is referenced directly and through a custom transition, the `linkopts` from the original rule are not deduplicated, and result in the same arguments being passed twice to the linker, which potentially causes unexpected results.

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

With this setup where `transition_library` applies a meaningless transition:

```bzl
load("//:foo.bzl", "transition_library")

cc_import(
name = "lib1",
linkopts = [
"-Wl,-rpath,bar",
],
static_library = "lib1.a",
)

transition_library(
name = "transition",
dep = ":lib1",
)

cc_binary(
name = "main",
srcs = ["main.cc"],
deps = [
":lib1",
":transition",
],
)
```

The `static_library` from `lib1` is correctly only passed once to the linker, but `-Wl,-rpath,bar` is passed twice. With Xcode 15 Apple's linker is stricter about flag duplication and this results in a warning:

```
ld: warning: duplicate -rpath 'bar' ignored
```

Previously this rpath was added to the binary twice, which was probably harmless but also unexpected.

Using this project: [reproccimport.zip](https://github.com/bazelbuild/bazel/files/12186071/reproccimport.zip) run `USE_BAZEL_VERSION=last_green bazelisk build main --linkopt=-v` and note that `-rpath bar` shows up twice in the linker command line (if you're testing with Xcode it will fail since I enabled `-fatal_warnings` to demonstrate the issue).

This is the graph from cquery showing how the transition impacts the dependency graph:

![ci](https://github.com/bazelbuild/bazel/assets/283886/fa055675-e56e-4cb4-aaf9-c6697d54229d)

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

macOS

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

b0d7248a69a970eaf9b3a3209feb1017641861c0

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

bazelisk last_green

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

_No response_

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

Not a regression, also existed in bazel 5

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

_No response_

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

https://github.com/bazelbuild/rules_swift/issues/1083

Contributor guide

Open the contributing guide

Research direction

Start with the reproccimport.zip example and run the provided bazelisk build command, then inspect how CcInfo and linkopts are propagated across direct and transitioned dependencies. The fix is complete when the reproduced linker command contains the rpath once while the static library remains correctly linked once.

Written by the indexing model from the issue text.

Assessment

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.