bazelbuild / bazelbuild/bazel

Expand template outputs being reported as `source file`

Open
#15,850 8 comments 0 reactions 0 assignees View on GitHub
not stale P4 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:

I'm not 100% confident this issue is specific to [ctx.actions.expand_template](https://docs.bazel.build/versions/main/skylark/lib/actions.html#expand_template) but in certain circumstances, files generated in rules are incorrectly reported as "source file" in queries. They should instead be reported as "generated file" if the file is the output of an action.

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

#### Workspace Example

The code below results in unexpected behavior when performing:

```
bazel query 'kind("source file", deps(//:pybin, 1))'
```

This outputs:
```
//:expanded.py
```

But this is unexpected. `expanded.py` is a generated file and should not be reported as "source file". It should instead be "generated file".

##### BUILD.bazel

```starlark
load(":defs.bzl", "expand_template")

expand_template(
name = "expanded_src",
template = "template.txt",
)

py_binary(
name = "pybin",
srcs = [":expanded_src"],
main = "expanded.py",
)
```

##### defs.bzl

```starlark
def _expand_template_impl(ctx):
output = ctx.actions.declare_file("expanded.py")

ctx.actions.expand_template(
output = output,
template = ctx.file.template,
substitutions = {},
)

return [DefaultInfo(
files = depset([output]),
)]

expand_template = rule(
implementation = _expand_template_impl,
attrs = {
"template": attr.label(
allow_single_file = True,
mandatory = True,
),
}
)
```

##### template.txt

```text
print("Hello World")
```

##### WORKSPACE.bazel

```starlark
workspace(name = "generated_file")
```

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

Linux, MacOS

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

release 5.2.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 master; git rev-parse HEAD` ?

_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 BUILD.bazel and defs.bzl workspace example and run the reported bazel query against the expand_template rule. Trace how the ctx.actions.expand_template output is classified by the query, then verify that expanded.py is reported as a generated file rather than a source file.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.