Skip to content

Incorrectly ignored files when subfolder/.gitignore contains */* #3315

@aaossa

Description

@aaossa

Describe the bug

When a subfolder contains a .gitignore file with the special rule */* (ignore subdirs contents) Black incorrectly ignores any file at the same level of the .gitignore.

To Reproduce

A minimal git repository:

.
└── repository/
    ├── .git/
    └── subdir/
        ├── .gitignore  # Contains */*
        └── main.py

Any change in the contents of main.py will be "seen" by Git but not Black, resulting in "No Python files are present to be formatted. Nothing to do 😴" in the following cases:

  • black . (from repository/)
  • black subdir/ (from repository/)
  • black . (from repository/subdir/)

It works as expected when SRC is the path to the file:

  • black subdir/main.py (from repository/)
  • black main.py (from repository/subdir/)

When using black . --verbose, the output confirms that the file is ignored because of the .gitignore file:

> black . --verbose
Identified `/repository` as project root containing a .git directory.
Sources to be formatted: "."
.git ignored: matches the --exclude regular expression
subdir/.gitignore ignored: matches the .gitignore file content
subdir/main.py ignored: matches the .gitignore file content
No Python files are present to be formatted. Nothing to do 😴

Notice that files in other folders of the repository are targeted as expected. Only the folder that contains the .gitignore file has its contents incorrectly ignored.

Also, the following repository structure works as expected for both Git and Black in any case, which confirms that the problem comes when the .gitignore is in a subdirectory with Python files:

.
└── repository/
    ├── .git/
    ├── .gitignore  # Contains */*
    └── main.py

Expected behavior

Black should be able to detect and format the contents of subdir/ that are visible for Git (like subdir/main.py).

Environment

  • Black's version: main
  • OS and Python version: Linux/Python 3.8.10

TLDR

These steps help to visualize the problem:

  1. Setup the repository
mkdir demo && cd demo
git init
echo "var=[1,2,3]" > root.py
mkdir subdir
echo "var=[1,2,3]" > subdir/subdir.py
echo "*/*" > subdir/.gitignore
git add -A
git commit -m "First commit"
  1. Confirm that subdir/subdir.py is visible to Git:
echo "var=[3,4,5]" > subdir/subdir.py
git status
  1. Black reaches root.py but not subdir/subdir.py
black . --verbose

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: file collectionRelated to file collection (e.g. gitignore & cache) or file discovery and all of its configuration.T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions