go.nvim
go.nvim copied to clipboard
require("go.format").goimport() does not format code any longer
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")
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.
I have the same behavior: :lua require("go.format").goimport() - only add import :lua require("go.format").gofmt() - just formats
I have the same problem
what config you are using? Could you confirm no other auto-format plugin was enabled when you testing the commands?
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
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.
I have the same problem.
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
- if your config pointing those tools to gopls, please make sure gopls is running by
LspInfo
- do a healthcheck
checkhealth go
- check if you have additional autoformat setup
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
- if your config pointing those tools to gopls, please make sure gopls is running by
LspInfo
- do a healthcheck
checkhealth go
- check if you have additional autoformat setup
I`m trying your config, same behavior GoImport only imported, GoFmt - formated
Would you check if the latest version fixed this?
Would you check if the latest version fixed this?
Yes! Thanks! Working awesome!
It seems gofmt works, but goimport not work
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)?
Would you post your config? @zyriab
@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!
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?
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'
Thanks @ray-x ! This goimports='goimports'
seems the best solution
-
:%! goimports
- this workaround loses the cursor position -
:lua vim.lsp.buf.code_action()
- works too but many steps - 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,
what is the version of gopls (gopls version
) and neovim version?
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
I did a quick look at the version you provided but failed to reproduce it. I'll keep an eye on it.
I have the same problem. My gopls and nvim versions are the same as saikocat.
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.
require("go.format").goimports() not work for me.