hatch icon indicating copy to clipboard operation
hatch copied to clipboard

`exclude` is not taking precedence over .gitignore whitelisted file.

Open Wim-De-Clercq opened this issue 1 year ago • 6 comments

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

Wim-De-Clercq avatar Feb 22 '24 16:02 Wim-De-Clercq

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.

ofek avatar Feb 22 '24 16:02 ofek

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

Wim-De-Clercq avatar Feb 22 '24 16:02 Wim-De-Clercq

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

Wim-De-Clercq avatar Feb 22 '24 16:02 Wim-De-Clercq

Debugging in builders/config.py - def exclude_spec image

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.

Wim-De-Clercq avatar Feb 22 '24 16:02 Wim-De-Clercq

Thank you so much for debugging! Yes indeed I would be very interested in your contribution.

ofek avatar Feb 22 '24 17:02 ofek

Ready: https://github.com/pypa/hatch/pull/1278

Wim-De-Clercq avatar Feb 23 '24 08:02 Wim-De-Clercq

https://github.com/pypa/hatch/pull/1278

ofek avatar Mar 03 '24 21:03 ofek