Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of .gitignore negate pattern #3228

Closed
ajasmin opened this issue Aug 18, 2022 · 5 comments
Closed

Incorrect handling of .gitignore negate pattern #3228

ajasmin opened this issue Aug 18, 2022 · 5 comments
Labels
R: invalid This issue / pull request doesn't seem right T: bug Something isn't working

Comments

@ajasmin
Copy link

ajasmin commented Aug 18, 2022

Describe the bug

When adding a name to .gitignore and negating a specific directory of that name, black incorrectly ignores the files it contains.

.gitignore

build
!/foo/build

To Reproduce

By running this shell script you should see that git doesn't ignore the file but black does.

#!/usr/bin/env bash
set -e

# Create project directory
TMP_DIR="$(mktemp -d)"
cd "$TMP_DIR"

# Make this a git working copy
git -c init.defaultBranch=main init

# Create .gitignore
cat > .gitignore << "EOF"
build
!/foo/build
EOF

# Create a file which shouldn't be ignored
mkdir -p foo/build
cat > foo/build/bar.py << "EOF"
x =     1
EOF

# Check that git doesn't ignore the file
if git check-ignore foo/build/bar.py; then
    echo "Path ignored by git"  # You shouldn't see this
fi

# Run black. Verbose output shows that the directory is ignored
black -v .
@ajasmin ajasmin added the T: bug Something isn't working label Aug 18, 2022
@Jackenmen
Copy link
Contributor

Jackenmen commented Aug 18, 2022

It appears to be a bug with pathspec not handling exclusions correctly for directories without a trailing slash (this also gives you a workaround for this issue - just add a trailing slash):

import pathspec


gitignore = """\
build
!/foo/build
"""
spec = pathspec.PathSpec.from_lines("gitwildmatch", gitignore.splitlines())
# incorrectly returns True
print(spec.match_file("foo/build/file.py"))


gitignore = """\
build
!/foo/build/
"""
spec = pathspec.PathSpec.from_lines("gitwildmatch", gitignore.splitlines())
# correctly returns False
print(spec.match_file("foo/build/file.py"))

I don't think there are any open issues for this on the pathspec repo so could you report it there?
https://github.com/cpburnz/python-path-specification/issues

@ajasmin
Copy link
Author

ajasmin commented Aug 18, 2022

This example was poorly chosen as build is also part of black’s default exclude regex.

I will file a bug with pathspec

@JelleZijlstra
Copy link
Collaborator

Thanks, closing as the bug appears to be in our dependency, not in Black itself.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2022
@ichard26 ichard26 added the R: invalid This issue / pull request doesn't seem right label Aug 19, 2022
@Jackenmen
Copy link
Contributor

Since it doesn't seem like you got around to it, I made an issue to track this: cpburnz/python-pathspec#57

@ichard26
Copy link
Collaborator

Thank you @jack1142!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: invalid This issue / pull request doesn't seem right T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants