`exclude` is not taking precedence over .gitignore whitelisted file.
When defining a folder as followed in .gitignore
!folder/sub-folder
then this exclude no longer works
[tool.hatch.build.targets.wheel]
exclude = [
"folder/sub-folder"
]
(I also tried it with artifacts = ["!folder/sub-folder"] in case being in gitignore makes this act like an artifact or something, no gain)
I feel like the gitignore / vcs configuration should be used as a soft default, but anything we define should take precedence over it.
This is a .gitignore pattern to exclude all but 1 folder:
# Ignore all in the `folder`
folder/*
# Except this
!folder/sub-folder
This would definitely be a bug because I have tests for inclusion/exclusion configuration. If you have time, could you please try this library with your situation? https://github.com/cpburnz/python-pathspec
That is what Hatchling uses.
That library seems fine I think.
import pathspec
spec_text = """
x
!x/y
"""
spec = pathspec.GitIgnoreSpec.from_lines(spec_text.splitlines())
assert spec.match_file("x") is True
assert spec.match_file("x/y") is False
assert spec.match_file("x/z") is True
I think this test I edited replicates the problem.
def test_vcs_git_whitelisted_file(self, temp_dir):
with temp_dir.as_cwd():
config = {'tool': {'hatch': {'build': {'exclude': ['foo/bar']}}}}
builder = MockBuilder(str(temp_dir), config=config)
vcs_ignore_file = temp_dir / '.gitignore'
vcs_ignore_file.write_text('foo/*\n!foo/bar')
assert builder.config.path_is_excluded("foo/deb") is True
assert builder.config.path_is_excluded("foo/bar") is True # returns False -- should be excluded by config tool.hatch.build.exclude
Debugging in builders/config.py - def exclude_spec
If I move
if not self.ignore_vcs:
all_exclude_patterns.extend(self.load_vcs_exclusion_patterns())
up before the other patterns are added it works. The order is important here.
I'm not sure if there is a specific reason to load the vcs patterns last? all of test_config.py still seems to work if I do so.
I can make a PR tomorrow if you feel like it can move up.
Thank you so much for debugging! Yes indeed I would be very interested in your contribution.
Ready: https://github.com/pypa/hatch/pull/1278
https://github.com/pypa/hatch/pull/1278