Skip to content

Changed semantics for matching files in project root with --include #3975

@sth

Description

@sth

Describe the bug

If I have as file named somefile in the root of the project and I want to include it with --include, traditionally the appropriate regex would contain a leading slash:

black --include '^/somefile$' ...

In #3846 this changed, now the leading slash prevents the file from being matched. Instead --include '^somefile$' is required.
The related exclude regexes still need the leading slash.

The cause are the path processing changes in #3846. Slashes are only added to root_relative_path when checking excludes and are now missing from normalized_path, which is later used to check includes.

To Reproduce

$ python3 -m venv venv
$ source venv/bin/activate
$ touch pyproject.toml
$ echo 'print( )' > somefile
$ pip install black==23.7.0
...
$ black --check --include '^/somefile$' .
would reformat /home/sth/tst/somefile

Oh no! 💥 💔 💥
1 file would be reformatted.
$ pip install black==23.10.1
...
$ black --check --include '^/somefile$' .
No Python files are present to be formatted. Nothing to do 😴

Expected behavior

The file name matching for --include should work as it did previously, since the change was most likely unintentional and the matching of excludes still works the old way.

Environment

  • Black's version: 23.10.1
  • OS and Python version: Ubuntu 22.04, Python 3.10.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions