bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Directories from glob are not marked as is_directory

Open UebelAndre opened this issue 4 years ago • 6 comments

Description of the problem / feature request:

Directories included from glob using exclude_directories = 0 do not correctly return True for File.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

load("//:rule.bzl", "my_rule")

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

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

rule.bzl

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

# This is a file in a directory

WORKSPACE.bazel

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 <source file dir> is source from the output below is incorrect. It's a directory. <source file dir> is directory (or something to this effect, hitting the else condition in rule.bzl) should have been printed.

bazel build ...
INFO: Invocation ID: c54083cd-e13a-4092-91a5-90c4e4aef70c
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: <source file BUILD.bazel> is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: <source file WORKSPACE.bazel> is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: <source file dir> is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: <source file dir/file.txt> is source
DEBUG: /Users/user/Code/test/is_directory/rule.bzl:4:18: <source file rule.bzl> 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

UebelAndre avatar Feb 03 '21 08:02 UebelAndre

I got bit by this today too. It is not just from glob, it happens with explicit paths to directories as well. Here a a reproduction. https://github.com/aiuto/samples/tree/main/bazel_issue_12954

Is there any chance of bumping this up from P4?

aiuto avatar May 12 '21 20:05 aiuto

This directory expansion in args as well.

Currently, if you want to use a directory, it has to come as the output of a rule.

pauldraper avatar Mar 15 '22 04:03 pauldraper

I got bit by this today too. It is not just from glob, it happens with explicit paths to directories as well.

Correct. glob() simply returns a list of strings, with which you can pass as files, strings, execute conditional logic, etc.

The correct title of this issue is "Source directories are not marked as is_directory"

I know that source directories aren't really supported in Bazel. I was using them for efficient processing of hundreds of npm dependencies. There isn't really a great alternative.

pauldraper avatar Apr 30 '23 16:04 pauldraper

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

github-actions[bot] avatar Jul 04 '24 01:07 github-actions[bot]

Comment

pauldraper avatar Jul 04 '24 02:07 pauldraper

A related issue, with possibly the same root cause: DirectoryExpander, as used in the Args object, does not expand source directories.

cameron-martin avatar Jun 09 '25 13:06 cameron-martin

@cameron-martin , yes. This is what I meant when I said

This [affects] directory expansion in args as well.

pauldraper avatar Nov 24 '25 06:11 pauldraper