Skip to content

Commit 3236636

Browse files
authored
fix: sdist.exclude takes precedence over .gitignore (#872)
Previously, sdist.exclude and the built-in EXCLUDE_LINES were prepended to the list of .gitignore rules. However, a common pattern of using * to exclude everything and then using !-rules to list what should be included renders this feature useless. This commit changes the behavior of each_unignored_file to sequentially consider the following sets of independent rules: 1. Explicit inclusions 2. Explicit exclusions 3. Global exclusions from the .gitignore files 4. The built-in exclusions 5. Nested exclusions from .gitignore Fixes #871
1 parent cafc039 commit 3236636

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/scikit_build_core/build/_file_processor.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ def each_unignored_file(
3636
"""
3737
Runs through all non-ignored files. Must be run from the root directory.
3838
"""
39-
exclude_lines = EXCLUDE_LINES + list(exclude)
40-
39+
global_exclude_lines = []
4140
for gi in [Path(".git/info/exclude"), Path(".gitignore")]:
4241
ignore_errs = [FileNotFoundError, NotADirectoryError]
4342
with contextlib.suppress(*ignore_errs), gi.open(encoding="utf-8") as f:
44-
exclude_lines += f.readlines()
43+
global_exclude_lines += f.readlines()
4544

4645
nested_excludes = {
4746
p.parent: pathspec.GitIgnoreSpec.from_lines(
@@ -51,7 +50,10 @@ def each_unignored_file(
5150
if p != Path(".gitignore")
5251
}
5352

54-
exclude_spec = pathspec.GitIgnoreSpec.from_lines(exclude_lines)
53+
user_exclude_spec = pathspec.GitIgnoreSpec.from_lines(list(exclude))
54+
global_exclude_spec = pathspec.GitIgnoreSpec.from_lines(global_exclude_lines)
55+
builtin_exclude_spec = pathspec.GitIgnoreSpec.from_lines(EXCLUDE_LINES)
56+
5557
include_spec = pathspec.GitIgnoreSpec.from_lines(include)
5658

5759
for dirstr, _, filenames in os.walk(str(starting_path), followlinks=True):
@@ -63,8 +65,16 @@ def each_unignored_file(
6365
yield p
6466
continue
6567

68+
# Always exclude something excluded
69+
if user_exclude_spec.match_file(p):
70+
continue
71+
6672
# Ignore from global ignore
67-
if exclude_spec.match_file(p):
73+
if global_exclude_spec.match_file(p):
74+
continue
75+
76+
# Ignore built-in patterns
77+
if builtin_exclude_spec.match_file(p):
6878
continue
6979

7080
# Check relative ignores (Python 3.9's is_relative_to workaround)

0 commit comments

Comments
 (0)