-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Present a more user-friendly error if .gitignore is invalid #2414
Conversation
tests/test_black.py
Outdated
) | ||
assert result.exit_code == 1 | ||
assert result.stderr_bytes is not None | ||
assert b"Invalid git pattern: '!\\n'" in result.stderr_bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the error emitted by pathspec 0.9.0 thanks to cpburnz/python-pathspec#50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems fragile to test the exact error message produced by pathspec. Maybe we should check only for the "Could not parse" bit from black's own code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me! Especially if we'd be upgrading the dependency quietly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
tests/test_black.py
Outdated
) | ||
assert result.exit_code == 1 | ||
assert result.stderr_bytes is not None | ||
assert b"Invalid git pattern: '!\\n'" in result.stderr_bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems fragile to test the exact error message produced by pathspec. Maybe we should check only for the "Could not parse" bit from black's own code?
assert result.stderr_bytes is not None | ||
|
||
gitignore = path / ".gitignore" | ||
assert f"Could not parse {gitignore}" in result.stderr_bytes.decode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here - it only checks for the presence of black's own error message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine to me, but I will note that code wasn't immediately clear and I was quite scared about "if gitignore is None: sys.exit(1)" bit since I thought it would also exit on cases where a gitignore file didn't exist.
Nevertheless, thank you!
Good point. One idea could be to lift the Another option might be to move the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this doesn't go far enough, it didn't occur to me before but we actually support nested gitignores these days, and uhm, this PR doesn't fully fix the ungraceful crash when a nested gitignore is invalid.
❯ tree -a test
test
├── a
│ ├── a.py
│ └── .gitignore
└── a.py
1 directory, 3 files
~/programming/oss/black on gitignore [$?] via v3.8.5 (bm-venv)
❯ dropblackcache -y ; black test
no caching was found!
Could not parse test/a/.gitignore: Invalid git pattern: '!\n'
Traceback (most recent call last):
File "/home/ichard26/programming/oss/black/bm-venv/bin/black", line 33, in <module>
sys.exit(load_entry_point('black', 'console_scripts', 'black')())
File "/home/ichard26/programming/oss/black/src/black/__init__.py", line 1281, in patched_main
main()
File "/home/ichard26/programming/oss/black/bm-venv/lib/python3.8/site-packages/click/core.py", line 1137, in __call__
return self.main(*args, **kwargs)
File "/home/ichard26/programming/oss/black/bm-venv/lib/python3.8/site-packages/click/core.py", line 1062, in main
rv = self.invoke(ctx)
File "/home/ichard26/programming/oss/black/bm-venv/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/ichard26/programming/oss/black/bm-venv/lib/python3.8/site-packages/click/core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "/home/ichard26/programming/oss/black/bm-venv/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/ichard26/programming/oss/black/src/black/__init__.py", line 431, in main
sources = get_sources(
File "/home/ichard26/programming/oss/black/src/black/__init__.py", line 536, in get_sources
sources.update(
File "/home/ichard26/programming/oss/black/src/black/files.py", line 227, in gen_python_files
gitignore + get_gitignore(child) if gitignore is not None else None,
TypeError: unsupported operand type(s) for +: 'PathSpec' and 'NoneType'
Sorry for not realizing this earlier!
ooh thanks for catching! Will cover and add test |
Updated! Great catch. I went ahead and caught the exception one method higher so that it covers both callsites. Added a test for the nested case as well (it failed before, now it passes). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick update!
Thank you so much for your contribution! This project is only possible by contributions like these 🖤. You're awesome, @nipunn1313. It's great to see the project get easier / more friendly to use :) Congrats on your first PR here! If you have any feedback on experience contributing please let me know via #2238. |
Fixes #2359