go.nvim icon indicating copy to clipboard operation
go.nvim copied to clipboard

require("go.format").goimport() does not format code any longer

Open jens1205 opened this issue 11 months ago • 25 comments

Lately, my setup which is exactly like described in https://github.com/ray-x/go.nvim?tab=readme-ov-file#run-gofmt--goimport-on-save stopped working.

It still organizes imports, but is not doing any formating.

Same problem, if I call the lua function manually.

:lua require("go.format").goimport() just organizes the import, no formating :lua require("go.format").gofmt() formats (and intererstingly enough at least adds a missing import "fmt")

jens1205 avatar Mar 12 '24 09:03 jens1205

Sounds to me you might have multiple formater/import auto command hooked. I pushed another change to enforce re-format code when the setup is gopls. Please take a look. If it still not working, you might need to send me the minium init.lua for me to reproduce.

ray-x avatar Mar 12 '24 10:03 ray-x

I have the same behavior: :lua require("go.format").goimport() - only add import :lua require("go.format").gofmt() - just formats

dn-kolesnikov avatar Mar 12 '24 13:03 dn-kolesnikov

I have the same problem

edelars avatar Mar 12 '24 18:03 edelars

what config you are using? Could you confirm no other auto-format plugin was enabled when you testing the commands?

ray-x avatar Mar 12 '24 22:03 ray-x

what config you are using? Could you confirm no other auto-format plugin was enabled when you testing the commands?

My current config:

https://github.com/dn-kolesnikov/dotfiles/tree/master/nvim/.config/nvim

dn-kolesnikov avatar Mar 13 '24 03:03 dn-kolesnikov

I face the same issue. I tried to force formatting by executing :lua require('go.format').goimport() but it does nothing, however require('go.format').gofmt() works as expected.

I have :checkhealth if there is anything missing, but everything is green. :LspInfo shows only a single client attached to the buffer (gopls), there are no other formatters.

I have pulled latest updates and the issue still persists.

Just noticed LSP logs logging this on goimport() call, maybe it is related, I'm not that much familiar with the go.nvim to answer this.

[ERROR][2024-03-13 12:16:17] ...lsp/handlers.lua:580	"2024/03/13 12:16:17 tidy: diagnosing file:///Users/raimondaskazlauskas/Projects/gearjot/go/go.mod: err: exit status 1: stderr: go: downloading go.uber.org/goleak v1.1.12\ngo: downloading github.com/sclevine/spec v1.4.0\ngo: downloading github.com/jmespath/go-jmespath/internal/testify v1.5.1\ngo: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c\ngo: downloading github.com/go-test/deep v1.0.8\ngo: downloading github.com/nxadm/tail v1.4.8\ngo: github.com/gearjot/go imports\n\tgithub.com/maxbrunsfeld/counterfeiter/v6 tested by\n\tgithub.com/maxbrunsfeld/counterfeiter/v6.test imports\n\tgithub.com/sclevine/spec: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go imports\n\tgithub.com/maxbrunsfeld/counterfeiter/v6 tested by\n\tgithub.com/maxbrunsfeld/counterfeiter/v6.test imports\n\tgithub.com/sclevine/spec/report: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go/pkg/awslocation imports\n\tgo.uber.org/zap tested by\n\tgo.uber.org/zap.test imports\n\tgo.uber.org/goleak: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go/pkg/container imports\n\tgithub.com/aws/aws-sdk-go-v2/service/ses imports\n\tgithub.com/jmespath/go-jmespath tested by\n\tgithub.com/jmespath/go-jmespath.test imports\n\tgithub.com/jmespath/go-jmespath/internal/testify/assert: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go/pkg/middlewares imports\n\tgithub.com/getkin/kin-openapi/openapi3filter imports\n\tgopkg.in/yaml.v3 tested by\n\tgopkg.in/yaml.v3.test imports\n\tgopkg.in/check.v1: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go/pkg/validate/openapi/kin imports\n\tgithub.com/getkin/kin-openapi/openapi3 imports\n\tgithub.com/perimeterx/marshmallow tested by\n\tgithub.com/perimeterx/marshmallow.test imports\n\tgithub.com/go-test/deep: module lookup disabled by GOPROXY=off\ngo: github.com/gearjot/go/pkg/rest/app imports\n\tgithub.com/awslabs/aws-lambda-go-api-proxy/chi tested by\n\tgithub.com/awslabs/aws-lambda-go-api-proxy/chi.test imports\n\tgithub.com/onsi/ginkgo imports\n\tgithub.com/onsi/ginkgo/internal/remote imports\n\tgithub.com/nxadm/tail: module lookup disabled by GOPROXY=off\n\n"

Let me know if there is anything I could help you to fix the issue.

Rhymond avatar Mar 13 '24 05:03 Rhymond

I have the same problem.

dcy avatar Mar 13 '24 06:03 dcy

I have the same problem

My current config: https://github.com/edelars/nvim-dotfiles

edelars avatar Mar 13 '24 08:03 edelars

There must be something wrong. But go import can be use difference source, e.g. gopls/gopls_subcommand/goimport/gofumpt I do need a way to reproduce the issue. It will be extremely helpful that someone can use a minium init.lua to reproduce this issue. I have a minium init.lua here https://github.com/ray-x/go.nvim/blob/master/playground/init_lazy.lua you can download it and update the config to best match your local environment and then:

nvim -u init_lazy.lua your_go_code.go

And reproduce the issue by running GoImport/ require('go.format').goimport()

Also please check

  1. if your config pointing those tools to gopls, please make sure gopls is running by LspInfo
  2. do a healthcheck checkhealth go
  3. check if you have additional autoformat setup

ray-x avatar Mar 13 '24 22:03 ray-x

There must be something wrong. But go import can be use difference source, e.g. gopls/gopls_subcommand/goimport/gofumpt I do need a way to reproduce the issue. It will be extremely helpful that someone can use a minium init.lua to reproduce this issue. I have a minium init.lua here https://github.com/ray-x/go.nvim/blob/master/playground/init_lazy.lua you can download it and update the config to best match your local environment and then:

nvim -u init_lazy.lua your_go_code.go

And reproduce the issue by running GoImport/ require('go.format').goimport()

Also please check

  1. if your config pointing those tools to gopls, please make sure gopls is running by LspInfo
  2. do a healthcheck checkhealth go
  3. check if you have additional autoformat setup

I`m trying your config, same behavior GoImport only imported, GoFmt - formated

dn-kolesnikov avatar Mar 14 '24 09:03 dn-kolesnikov

Would you check if the latest version fixed this?

ray-x avatar Mar 14 '24 12:03 ray-x

Would you check if the latest version fixed this?

Yes! Thanks! Working awesome!

dn-kolesnikov avatar Mar 14 '24 13:03 dn-kolesnikov

It seems gofmt works, but goimport not work

dcy avatar Mar 16 '24 08:03 dcy

Same problem here, I just updated the plugin and it does not work anymore. I updated my config based on the readme, to no avail. I also have the error message golines failed 1.

Any way to revert the update I did (using Lazy)?

zyriab avatar Mar 26 '24 00:03 zyriab

Would you post your config? @zyriab

ray-x avatar Mar 26 '24 03:03 ray-x

@ray-x I just realized that I did not push this new update on my dotfiles repo, I reverted the update and still had the issue. Turns out that I had a dangling curly brace somewhere. I really gotta stop touching my Neovim config at 2AM 🫠

Thanks for the help and sorry!

zyriab avatar Mar 26 '24 12:03 zyriab

I'm at my wit's end for this as well. Whenever I try to run GoImports / require('go.format').goimport() , I will see this in the status line:

No code actions available

checkhealth go seems to be fine, and I don't think I have another formatter running, but is there a way to check that?

saikocat avatar Apr 05 '24 16:04 saikocat

on possibility is import capability was disabled. or your version of gopls does not support go import. Does importing with code action work for you?

Also, you can still use following config goimports='goimports'

ray-x avatar Apr 08 '24 02:04 ray-x

Thanks @ray-x ! This goimports='goimports' seems the best solution

  1. :%! goimports - this workaround loses the cursor position
  2. :lua vim.lsp.buf.code_action() - works too but many steps
  3. This works perfectly. Reasons why previously it works, but after a plugin update (3-4 weeks ago) it stopped working, explicitly setting it allows imports on write working again. Maybe mason lsp causes interference as well.
config = function()
      require('go').setup({
        goimports = 'goimports',
      })
    end,

saikocat avatar Apr 08 '24 03:04 saikocat

what is the version of gopls (gopls version) and neovim version?

ray-x avatar Apr 08 '24 05:04 ray-x

Here goes:

$ gopls version
golang.org/x/tools/gopls v0.15.2

$ nvim --version
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1702233742

saikocat avatar Apr 08 '24 05:04 saikocat

I did a quick look at the version you provided but failed to reproduce it. I'll keep an eye on it.

ray-x avatar Apr 08 '24 06:04 ray-x

I have the same problem. My gopls and nvim versions are the same as saikocat.

penzur avatar Apr 14 '24 14:04 penzur

require("go.format").goimports() seems to work, but the cursor will move to the next line/s, depending on the number of removed unused imports.

penzur avatar Apr 14 '24 14:04 penzur

require("go.format").goimports() not work for me.

ndzuki avatar Apr 29 '24 07:04 ndzuki