Skip to content

incompatible_disallow_empty_glob: fail if a glob doesn't match anything #8195

@laurentlb

Description

@laurentlb

The glob() function tends to be error-prone, because any typo in a path will silently return an empty list. This has been a source of confusion and bugs for many users.

glob has an optional Boolean argument allow_empty. For example, let's say your code looks like:

glob([pattern_a, pattern_b], exclude = [pattern_c], allow_empty = False)

This code will fail if pattern_a or pattern_b doesn't match anything. It also fails if the whole function (after excluding pattern_c) doesn't match anything. We believe this behavior is safer in general.

In rare cases, this is intentional that a pattern doesn't match anything, e.g. when a source file is optional. When it happens, users should tell their intent explicitly by setting allow_empty = True.

  • What's going to change? The default value of allow_empty used to be True. It will change to False.

  • How to update your code? If a glob fails, use allow_empty = True to keep the old behavior.

In Bazel 8.0, the flag --incompatible_disallow_empty_glob will default to true. This means that the allow_empty argument defaults to False on all glob calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-change-8.0incompatible-changeIncompatible/breaking changenot staleIssues or PRs that are inactive but not considered staleteam-Loading-APIBUILD file and macro processing: labels, package(), visibility, globtype: process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions