mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.97k stars 332 forks source link

Resolve symlink when formatting single file #1054

Closed mechanicker closed 5 months ago

mechanicker commented 5 months ago

Fix for: https://github.com/mvdan/sh/issues/1053

Using regular os.Open on symlink, formatting and writing back the output breaks the link and creates a new file instead.

Following a symlink automatically is something to decide. Do we trust the use to have done due diligence when passing a symlink OR avoid surprise and return an error for symlinks?

This change resolves the symlink and operates on resolved path, updates the source path and retains the symlink.

mvdan commented 5 months ago

Thanks, but I think the right fix would be what is already described in https://github.com/mvdan/sh/issues/843#issuecomment-1344137219. Also, whatever fix we end up with would need tests.