bazelbuild / bazelbuild/bazel

cc_library does not correctly handle so/dylib sources if a srcs arg is directory output of another rule

Open
#27,133 1 comment 1 reaction 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:

cc_library supports so/dynlib as sources (see https://bazel.build/reference/be/c-cpp#cc_library)

this works correctly when so/dynlib sources are supplied as individual files, but

In some cases one might not have these sources directly but have a directory output of another rule, for example in my case it is `whl_filegroup` of `rules_python`

in these cases bazel seems to hit this codepath that checks every file in the input directory to confirm it is an actual source:

https://github.com/bazelbuild/bazel/blob/d8eedb7613e13ae1807aecf87c7116f76cf476fd/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionTemplate.java#L139-L148

with this codepath not considering dynlib/so files to be a valid source files:

https://github.com/bazelbuild/bazel/blob/d8eedb7613e13ae1807aecf87c7116f76cf476fd/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionTemplate.java#L107-L115

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

in example/helper.bzl:
```
def _impl(ctx):
out_dir = ctx.actions.declare_directory(ctx.label.name)
ctx.actions.run_shell(
outputs = [out_dir],
command = """
mkdir -p {out}/subdir
echo 'hello world' > {out}/subdir/file.so
""".format(out=out_dir.path),
)
return [DefaultInfo(files = depset([out_dir]))]

dir_rule = rule(
implementation = _impl,
)
```

example/BUILD.bazel file next to it:
```
load("//example:helper.bzl", "dir_rule")

dir_rule(
name = "dir_output",
)

cc_library(
name = "example_cc_lib",
visibility = ["//visibility:private"],
srcs = [":dir_output"],
)
```

`bazel build //example:example_cc_lib`

Gets error like:
```
ERROR: /Users/romanoid/example/BUILD.bazel:30:11: Compiling all C++ files in dir_output failed: Artifact 'bazel-out/darwin_arm64-fastbuild/bin/example/dir_output/subdir/file.so' expanded from the directory artifact 'bazel-out/darwin_arm64-fastbuild/bin/example/dir_output' is neither header nor source file.
Target //example:example_cc_lib failed to build
Use --verbose_failures to see the command lines of failed build steps.
```

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

MacOS

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

release 8.3.1

### 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 src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionTemplate.java, especially the source-file check at lines 107-115 and directory expansion at lines 139-148. Reproduce the issue with the helper.bzl and BUILD.bazel example, then verify that a .so file under a directory output can be used by cc_library without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.