Skip to content

Commit

Permalink
fix: resolve symlinks when checking which files to format
Browse files Browse the repository at this point in the history
Currently `find_project_root()` resolves symlinks, but `get_sources()`
doesn't, so trying to format a file that contains symlinks will fail:
```
$ python -m black --check /home/nfvs/code/black/src/black/files.py
No Python files are present to be formatted. Nothing to do 😴
```

This commit changes methods used in `get_sources()` to, like
`find_project_root()`, use `Path.resolve()` instead of
`Path.absolute()`, which will follow symlinks.
  • Loading branch information
nfvs committed Feb 7, 2024
1 parent 32230e6 commit 689ecad
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def get_root_relative_path(
) -> Optional[str]:
"""Returns the file path relative to the 'root' directory"""
try:
root_relative_path = path.absolute().relative_to(root).as_posix()
root_relative_path = path.resolve().relative_to(root).as_posix()
except ValueError:
if report:
report.path_ignored(path, f"is a symbolic link that points outside {root}")
Expand Down Expand Up @@ -341,7 +341,7 @@ def gen_python_files(

assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
for child in paths:
root_relative_path = child.absolute().relative_to(root).as_posix()
root_relative_path = child.resolve().relative_to(root).as_posix()

# First ignore files matching .gitignore, if passed
if gitignore_dict and _path_is_ignored(
Expand Down

0 comments on commit 689ecad

Please sign in to comment.