bazelbuild / bazelbuild/bazel

Directories from glob are not marked as is_directory

Open
#12,954 7 comments 5 reactions 0 assignees View on GitHub
P4 team-Rules-Server type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the problem / feature request:

Directories included from [glob](https://docs.bazel.build/versions/master/be/functions.html#glob) using `exclude_directories = 0` do not correctly return `True` for [File.is_directory](https://docs.bazel.build/versions/master/skylark/lib/File.html#is_directory).

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

Running `bazel build ...` with the following files will reproduce this issue.

#### BUILD.bazel
```python
load("//:rule.bzl", "my_rule")

filegroup(
name = "srcs",
srcs = glob(
["**/**"],
exclude_directories = 0,
),
)

my_rule(
name = "my_rule",
data = ":srcs",
)
```

#### rule.bzl
```python
def _rule_impl(ctx):
for file in ctx.files.data:
if file.is_source:
print("{} is source".format(file))
else:
print("{} is directory".format(file))

my_rule = rule(
implementation = _rule_impl,
attrs = {
"data": attr.label(
allow_files = True,
default = "//:srcs",
)
}
)
```

#### dir/file.txt
```python
# This is a file in a directory
```

#### WORKSPACE.bazel
```python
workspace(name = "is_directory_test")
```

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

MacOS/Linux

### What's the output of `bazel info release`?

release 4.0.0

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

Note that ` is source` from the output below is incorrect. It's a directory. ` is directory` (or something to this effect, hitting the `else` condition in `rule.bzl`) should have been printed.
```command
bazel build ...
```
```output
INFO: Invocation ID: c54083cd-e13a-4092-91a5-90c4e4aef70c
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: is source
INFO: Analyzed 2 targets (1 packages loaded, 7 targets configured).
INFO: Found 2 targets...
INFO: Elapsed time: 0.257s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with BUILD.bazel, rule.bzl, WORKSPACE.bazel, and the glob(..., exclude_directories = 0) example, then inspect the File.is_directory behavior for the directory entry. Confirm the fix by ensuring the directory reaches the directory branch while regular files remain source files, and add or run the relevant Bazel test if one is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
build-system
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.