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

fix #457: Make --exclude option follow symlinked directories #458

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- Fixes a bug where `--exclude` would not follow symlinks when globbing
([#457](https://github.com/tconbeer/sqlfmt/issues/457) - thank you [@jeancochrane](https://github.com/jeancochrane)!).

## [0.19.1] - 2023-07-13

### Bug Fixes
Expand Down
15 changes: 8 additions & 7 deletions src/sqlfmt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,19 @@ def get_matching_paths(paths: Iterable[Path], mode: Mode) -> Set[Path]:
for s in mode.exclude:
if PurePath(s).is_absolute():
exclude_set.update([Path(g) for g in glob(s, recursive=True)])
elif mode.exclude_root is not None:
else:
exclude_root = mode.exclude_root or Path.cwd()
try:
exclude_set.update(mode.exclude_root.glob(s))
# Prefer glob.glob() to Path.glob() here, even though the
# latter would be more concise, since Path.glob() does not
# resolve symlinks properly in Python <3.13
exclude_set.update(
[Path(g) for g in glob(str(exclude_root / s), recursive=True)]
Copy link
Contributor Author

@jeancochrane jeancochrane Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a bit hacky to cast the Path object to a string here, but it's the only way to get it to work with glob.glob(), which expects a string. It's probably fine, and hopefully the tests would tell us if it wasn't, but part of me is a bit nervous about the platform compatibility of this change -- It works fine on my Ubuntu machine, but I can't help being nervous that e.g. Windows may handle this string conversion incorrectly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be safe to do it this way, but thanks for calling out this change!

)
except IndexError:
# for some reason Path.glob(".") raises an index error,
# although glob.glob(".") returns ["."]
pass
Comment on lines 112 to 115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can remove this error handling block given that we're no longer calling Path.glob() at all?

else:
try:
exclude_set.update(Path.cwd().glob(s))
except IndexError:
pass

return include_set - exclude_set

Expand Down
9 changes: 8 additions & 1 deletion tests/unit_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def all_files(file_discovery_dir: Path) -> Set[Path]:
p / "a_directory/one_file.sql",
p / "a_directory/nested_directory/another_file.sql",
p / "a_directory/nested_directory/j2_extension.sql.jinja",
p / "a_directory/symlink_source_directory/symlink_file.sql",
p / "a_directory/symlink_target_directory/symlink_file.sql",
}
return files

Expand All @@ -74,7 +76,12 @@ def test_file_discovery(
[
["**/*_file*"],
["**/*.sql"],
["**/top*", "**/a_directory/*", "**/a_directory/**/another_file.sql"],
[
"**/top*",
"**/a_directory/*",
"**/a_directory/**/another_file.sql",
"**/a_directory/**/symlink_file.sql",
],
],
)
def test_file_discovery_with_excludes(
Expand Down