bazelbuild / bazelbuild/bazel

Building a filegroup with output_group always includes runfiles

Open
#28,871 2 comments 0 reactions 0 assignees View on GitHub
P3 team-Rules-API 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 we use
```
filegroup(
name = "fg",
srcs = [":mytarget"],
output_group = "a",
)
```
to select a certain output group from `mytarget`, then `bazel build fg`, the runfiles of `mytarget` are also built regardless. This is different from the behavior of `bazel build mytarget --output_group=a`.

### Which category does this issue belong to?

Configurability

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

BUILD.bazel

```starlark
load(":myrule.bzl", "fail_rule", "myrule", "success")

success(
name = "success",
)

fail_rule(
name = "fail",
)

myrule(
name = "mytarget",
a = "success",
default = ":fail",
)

filegroup(
name = "fg",
srcs = [":mytarget"],
output_group = "a",
)

genrule(
name = "genrule",
srcs = [":fg"],
outs = ["genrule.out"],
cmd = "touch $@",
)
```

myrule.bzl

```starlark
def _success_impl(ctx):
file = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.write(file, "")
return DefaultInfo(files = depset([file]))

success = rule(
implementation = _success_impl,
)

def _fail_impl(ctx):
file = ctx.actions.declare_file(ctx.attr.name)
ctx.actions.run_shell(
command = "exit 1",
outputs = [file],
)
return DefaultInfo(
files = depset([file]),
)

fail_rule = rule(
implementation = _fail_impl,
)

def _myrule_impl(ctx):
return [
DefaultInfo(
files = depset(ctx.files.default),
runfiles = ctx.runfiles(files = ctx.files.default),
),
OutputGroupInfo(
a = ctx.files.a,
),
]

myrule = rule(
implementation = _myrule_impl,
attrs = {
"a": attr.label(allow_files = True),
"default": attr.label(allow_files = True),
},
)
```

With this setup, run the following commands:

```sh
# This succeeds -- it doesn't build `mytarget`'s runfiles
bazel build :mytarget --output_groups a

# This fails because it tries to build :fail -- why does it try to build the runfiles?
bazel build :fg

# This succeeds -- it doesn't build `mytarget`'s runfiles
bazel build :genrule
```

Building `:fg` is not the same as building `:mytarget --output_groups a` because the former builds `:mytarget`'s runfiles but the latter does not.

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

Linux

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

9.0.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?

https://bazel.build/reference/be/general#filegroup The document does not seem to mention that the runfiles are also built.

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

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by running the minimal reproduction in BUILD.bazel with the accompanying myrule.bzl, comparing the three bazel build commands and their handling of the fail target. Trace filegroup's output_group and runfiles behavior from that reproduction. Done means building :fg selects output group a without building mytarget's default runfiles, while :genrule continues to succeed.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.