incu6us / goimports-reviser

Right imports sorting & code formatting tool (goimports alternative)
MIT License
607 stars 72 forks source link

goimports-reviser not importing and undoing on save #112

Closed kcaashish closed 1 year ago

kcaashish commented 1 year ago

My config for goimports-reviser is:

return {
  {
    "jose-elias-alvarez/null-ls.nvim",
    opts = function(_, opts)
      local nls = require("null-ls")
      local nls_sources = {
        -- go
        nls.builtins.formatting.goimports_reviser.with({
          generator_opts = {
            command = "goimports-reviser",
            args = { "-output", "stdout", "$FILENAME" },
            to_stdin = true,
          },
        }),
      }
      vim.list_extend(opts.sources, nls_sources, 1, #nls_sources)
    end,
  },
}

In a very simple go project with go.mod:

module minimal

go 1.19

main.go:

package main

func main() {
    fmt.Println("Hello, World!")
}

Notice the absence of imports statment as well. So there should be a undeclared name: fmt error. If I format the code, the imports are not auto imported.

If I delete the line fmt.Println() dd then hit :w, the line pops back up.

kcaashish commented 1 year ago

Screencast from 2023-03-24 14-10-39.webm

nanomoeamezao commented 1 year ago

You should try reaching out to null-ls devs about this, as it likely needs some edits in the builtin itself (related to reading the old saved file, but not the new unsaved version).

incu6us commented 1 year ago

hi @kcaashish I'm not familiar with null-ls, but I guess the problem is in your configuration to the plugin. Try to find out correct example for null-ls which will cover your case

kcaashish commented 1 year ago

Hey @incu6us, we did solve it by writing into a temp file. I think the issue here is taking input from stdin. Possibly this https://github.com/incu6us/goimports-reviser/issues/94! We had a discussion over at null-ls and chose to use temp file for now until this gets fixed in goimports-reviser. The discussion: https://github.com/jose-elias-alvarez/null-ls.nvim/discussions/1472#discussioncomment-5480747 Anything on this?

kcaashish commented 1 year ago

So instead of :

args = { "-output", "stdout", "$FILENAME" },
to_stdin = true,

we are using:

args = {"$FILENAME"},
to_temp_file = true,

Anyway to fix this?

incu6us commented 1 year ago

fixed stdin feature in v3.5.6