jose-elias-alvarez / null-ls.nvim

Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.
Other
3.64k stars 790 forks source link

[swiftformat] Unable to format file, filename is missing #876

Closed kkharji closed 2 years ago

kkharji commented 2 years ago

FAQ

Issues

Neovim Version

v0.8.0-dev

Operating System

linux

Minimal config

swiftformat.with {
        extra_args = function(params)
          local local_swiftformat = params.cwd .. "/.swiftformat"

          if stat(local_swiftformat) ~= nil then
            return { "--config", local_swiftformat  }
          end

          local global_swiftformat = vim.fn.expand "~/.config/.swiftformat"
          if stat(global_swiftformat) ~= nil then
            return { "--config", global_swiftformat }
          end

          return {  }
        end,
      }

Steps to reproduce

  1. create .swiftformat file `--header "{file}\nCopyright (c) {year}"
  2. write swift file buffer

Expected behavior

file path to be passed as argument

Actual behavior

Failed to apply {file} template in file header as file name was not provided.

also with another variable Failed to apply {created} template in file header as file info is unavailable

Debug log

[DEBUG Thu May 19 13:44:43 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:346: spawning command "swiftformat" at /Users/tami5/sources/swift/Control with args { "--config", "/Users/tami5/sources/swift/Control/.swiftformat" }
[TRACE Thu May 19 13:44:43 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:217: error output: Running SwiftFormat...
error: Failed to apply {file} template in file header as file name was not provided.

[TRACE Thu May 19 13:44:43 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:218: output: nil
[TRACE Thu May 19 13:44:43 2022] ...t/null-ls.nvim/lua/null-ls/helpers/generator_factory.lua:222: ignoring stderr due to generator options
[TRACE Thu May 19 13:44:43 2022] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method textDocument/didSave
[TRACE Thu May 19 13:44:43 2022] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_SAVE
[DEBUG Thu May 19 13:44:43 2022] ...ack/packer/start/null-ls.nvim/lua/null-ls/generators.lua:24: no generators available
[TRACE Thu May 19 13:44:44 2022] .../site/pack/packer/start/null-ls.nvim/lua/null-ls/rpc.lua:146: received LSP notification for method textDocument/didChange

Help

No

Implementation help

No response

Requirements

jose-elias-alvarez commented 2 years ago

I don't think this is a null-ls issue, since the following command to pass the file's content via stdin (which is the equivalent of what null-ls is doing) also produces the same error:

cat test.swift | swiftformat --config .swiftformat

Based on their documentation, this command worked for me:

cat test.swift | swiftformat stdin --stdinpath test.swift --config .swiftformat 

I'd guess that adding additional arguments means you have to explicitly state that input is coming from stdin, since cat test.swift | swiftformat works. This is just a guess, though, since I've never worked with Swift or this formatter before.

kkharji commented 2 years ago

Indeed, it's not null-ls issues per-se but how null-ls support swiftformat.

Thanks a lot for taking the time to look into this one @jose-elias-alvarez . I guess I only need to append --stdinpath to arguments.

Do you think it may make sense to have by default, I can open a pr?

jose-elias-alvarez commented 2 years ago

Sure, I agree that it makes sense to be more explicit about the argument structure if it's causing issues. PR welcome!