mhartington/formatter.nvim

`sed: illegal option --` on MacOS

Closed this issue · 2 comments

Shell command and output (optional):

> sed --in-place
sed: illegal option -- -
usage: sed script [-Ealnru] [-i extension] [file ...]
        sed [-Ealnu] [-i extension] [-e script] ... [-f script_file] ... [file ...]

Which configuration?
Type (custom or builtin):
Filetype: *
Formatter: defaults.sed (removes trailing whitespace)

Configuration(s) (post all the variants you tried):

-- ...      
    require('formatter').setup {
        filetype = {
          ['*'] = {
            require('formatter.filetypes.any').remove_trailing_whitespace
          }
        }
      }
-- ...

Expected behavior
sed removes trailing whitespace.

Actual behaviour
sed doesn't understand -- flags on MacOS.

Additional context
If the sed executable's path was configurable, we could install GNU sed on MacOS using homebrew, etc, and use gsed instead.

sry for the wait

ill add detection for macos using vim.fn.has for that config

I think instead of --in-place you'd want to use -i '' here.

I believe that would also work with GNU sed, but I haven't tested that. It might be safest to detect macOS/BSD versus Linux/other GNU(?) and use the appropriate option.

Alternatively, don't use that flag at all and just read the output from sed's stdout.

local sed = function(pattern, replacement, flags)
    return {
        exe = "sed",
        args = { util.quote_cmd_arg(util.wrap_sed_replace(pattern, replacement, flags)) },
        stdin = true,
    }
end

works for me locally on macOS; I can open a PR with it later if that's helpful.