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

Force Exclude does not work with symlinks #3826

Closed
slorg1 opened this issue Aug 2, 2023 · 4 comments · Fixed by #3846, #4013 or #4015
Closed

Force Exclude does not work with symlinks #3826

slorg1 opened this issue Aug 2, 2023 · 4 comments · Fixed by #3846, #4013 or #4015
Labels
T: bug Something isn't working

Comments

@slorg1
Copy link

slorg1 commented Aug 2, 2023

Describe the bug
I use the force-exclude feature to exclude files living in symlinked directories.
However, it looks like black uses the regexp from force-exclude after the symlink is resolved (not the symlinked path).

$ black -v /path_2_file/application/api/lib/dir_symlinked/__init__.py
Identified `/path_2_file` as project root containing a .git directory.
Using configuration from project root.
line_length: 135
target_version: ['py39']
force_exclude: (?:.*?application/api(?:-ext)?/lib/dir_symlinked/.+)

skip_string_normalization: True
Found input source: "application/lib/ref_dir/__init__.py"
/path_2_file/application/api/lib/dir_symlinked/__init__.py wasn't modified on disk since last run.

All done! ✨ 🍰 ✨
1 file left unchanged.

The resulting error is:

It should have ignored the file based on this path /path_2_file/application/api/lib/dir_symlinked/__init__.py rather than including it based on this path application/lib/ref_dir/__init__.py.
Said differently it used the resolved/resulting path rather than the path through the symlink

Environment

  • Black's version: 23.7.0
  • OS and Python version: MacOS Ventura 13.4.1, Python 3.9.12,

Additional context

I would be equally happy if I could simply tell black not to follow symlinks.

Thank you in advance for your help!

@slorg1 slorg1 added the T: bug Something isn't working label Aug 2, 2023
hauntsaninja added a commit to hauntsaninja/black that referenced this issue Aug 19, 2023
This means, for instance, that a gitignored symlink cannot affect your
formatting. Fixes psf#3527, fixes psf#3826
hauntsaninja added a commit to hauntsaninja/black that referenced this issue Aug 19, 2023
This means, for instance, that a gitignored symlink cannot affect your
formatting. Fixes psf#3527, fixes psf#3826
JelleZijlstra pushed a commit that referenced this issue Sep 7, 2023
This means, for instance, that a gitignored symlink cannot affect your
formatting. Fixes #3527, fixes #3826
@slorg1
Copy link
Author

slorg1 commented Oct 5, 2023

Hi,

Thank you for the update, I just tried it using black version 23.9.1 and sadly I got the same result as described in the issue here.

@JelleZijlstra could you please reopen this ticket? Thank you

@slorg1
Copy link
Author

slorg1 commented Oct 12, 2023

@hauntsaninja or @JelleZijlstra little bump on reopening this. Thank you!! 😄

@JelleZijlstra JelleZijlstra reopened this Oct 23, 2023
@hauntsaninja
Copy link
Collaborator

Thanks, I think this will be fixed once we deduplicate get_sources and gen_python_files

@sth
Copy link
Contributor

sth commented Oct 26, 2023

The problem in the concrete test case (and why it's not affected by #3846) is likely that the symlinked file is given explicitly on the command line and the command line parameters are separately normalized and checked against excludes:

black/src/black/__init__.py

Lines 642 to 660 in f7174bf

normalized_path: Optional[str] = normalize_path_maybe_ignore(
p, root, report
)
if normalized_path is None:
if verbose:
out(f'Skipping invalid source: "{normalized_path}"', fg="red")
continue
if verbose:
out(f'Found input source: "{normalized_path}"', fg="blue")
normalized_path = "/" + normalized_path
# Hard-exclude any files that matches the `--force-exclude` regex.
if force_exclude:
force_exclude_match = force_exclude.search(normalized_path)
else:
force_exclude_match = None
if force_exclude_match and force_exclude_match.group(0):
report.path_ignored(p, "matches the --force-exclude regular expression")
continue

Probably this code should be adjusted the same way as the code in src/black/files.py.

sth added a commit to sth/black that referenced this issue Oct 27, 2023
This adjusts exclusion checking for command line parameters the same way
as psf#3846 did for other files. Specifically, --force-exclude is now
checked before symlink resolution. This way symlink handling is
consistent between command line parameters and other collected files.

Fixes psf#3826.
sth added a commit to sth/black that referenced this issue Oct 27, 2023
This adjusts exclusion checking for command line parameters the same way
as psf#3846 did for other files. Specifically, --force-exclude is now
checked before symlink resolution. This way symlink handling is
consistent between command line parameters and other collected files.

Fixes psf#3826.
@hauntsaninja hauntsaninja reopened this Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment