From 689ecad038da5d80c3d74212379e0b27108f2d29 Mon Sep 17 00:00:00 2001 From: Nuno Santos Date: Wed, 7 Feb 2024 07:01:03 -0800 Subject: [PATCH] fix: resolve symlinks when checking which files to format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/black/files.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/files.py b/src/black/files.py index 960f13ee270..f75afe8da26 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -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}") @@ -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(