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

Ignore symlinks when formatting #57

Closed
wants to merge 2 commits into from
Closed

Ignore symlinks when formatting #57

wants to merge 2 commits into from

Conversation

Lucus16
Copy link
Contributor

@Lucus16 Lucus16 commented Feb 14, 2020

Before this, symlinks were replaced with a formatted file rather than
formatting the file behind the symlink. The choice was between ignoring
symlinks and following them. Following symlinks means potentially
formatting files in an unexpected location which seems much worse than
needing to wrap the arguments in a readlink -f when symlinks are
otherwise ignored.

Fixes #56.

@Lucus16
Copy link
Contributor Author

Lucus16 commented Feb 15, 2020

Ah, whoops, I broke nixfmt without arguments.

This separates data and functionality better.
Before this, symlinks were replaced with a formatted file rather than
formatting the file behind the symlink. The choice was between ignoring
symlinks and following them. Following symlinks means potentially
formatting files in an unexpected location which seems much worse than
needing to wrap the arguments in a `readlink -f` when symlinks are
otherwise ignored.
@Lucus16
Copy link
Contributor Author

Lucus16 commented Feb 15, 2020

That should fix that.

Copy link
Member

@kirelagin kirelagin left a comment

Choose a reason for hiding this comment

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

All good, but I do insist on using safe-exceptions. (Especially, given that it’s already a dependency anyway.)

@@ -9,6 +9,7 @@
module Main where

import Control.Concurrent (Chan, forkIO, newChan, readChan, writeChan)
import Control.Exception (IOException, try)
Copy link
Member

Choose a reason for hiding this comment

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

Please, only ever use Control.Exception.Safe from safe-exceptions.

Choose a reason for hiding this comment

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

Also apparently it's in a wrong commit 🙄

filterTarget :: (Target -> IO Result) -> Target -> IO Result
filterTarget operation StdioTarget = operation StdioTarget
filterTarget operation (FileTarget path) = do
isSymlink <- try $ pathIsSymbolicLink path
Copy link
Member

Choose a reason for hiding this comment

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

This will also catch async exceptions which you don’t want (this probably doesn’t matter in this specific case, but it’s better to place safe and future-proof). Use try for safe-exceptions.

@kirelagin
Copy link
Member

One option might be to add -L option to follow symlinks, but, I’d say, that’s too much trouble. Instead, we can just add an example of an invocation that would follow symlinks (find, xargs, readlink, something like this...).

Copy link

@gromakovsky gromakovsky left a comment

Choose a reason for hiding this comment

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

I don't have anything substantial to add. Using safe-exceptions sounds like a good practice, but some people don't like it (not me) and for IOException it should not matter, so I don't really care if you change that.

@Lucus16
Copy link
Contributor Author

Lucus16 commented Feb 17, 2020

I agree with using safe-exceptions and adding an example like nixfmt $(readlink -f $(find -name '*.nix')). Don't have time for it right now though, feel free to fix them and ask for my review.

@tomberek
Copy link
Contributor

We plan to deprecate the directory mode directly in nixfmt and instead encourage use of treefmt.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-05-28/46126/1

@infinisil
Copy link
Member

See #151

@infinisil infinisil closed this Aug 6, 2024
@infinisil infinisil deleted the ignore-symlinks branch August 6, 2024 18:41
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-08-06/50222/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Nixfmt does overwrite symlinks with reformatted contents
6 participants