Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jens1205 opened this issue Mar 12, 2024 · 25 comments
Open

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

jens1205 opened this issue Mar 12, 2024 · 25 comments

Comments

@jens1205
Copy link

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")

@ray-x
Copy link
Owner

ray-x commented Mar 12, 2024

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.

@dn-kolesnikov
Copy link

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

@edelars
Copy link

edelars commented Mar 12, 2024

I have the same problem

@ray-x
Copy link
Owner

ray-x commented Mar 12, 2024

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

@dn-kolesnikov
Copy link

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

@Rhymond
Copy link

Rhymond commented Mar 13, 2024

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.

@dcy
Copy link

dcy commented Mar 13, 2024

I have the same problem.

@edelars
Copy link

edelars commented Mar 13, 2024

I have the same problem

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

@ray-x
Copy link
Owner

ray-x commented Mar 13, 2024

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

@dn-kolesnikov
Copy link

dn-kolesnikov commented Mar 14, 2024

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

@ray-x
Copy link
Owner

ray-x commented Mar 14, 2024

Would you check if the latest version fixed this?

@dn-kolesnikov
Copy link

Would you check if the latest version fixed this?

Yes! Thanks! Working awesome!

@dcy
Copy link

dcy commented Mar 16, 2024

It seems gofmt works, but goimport not work

@zyriab
Copy link

zyriab commented Mar 26, 2024

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)?

@ray-x
Copy link
Owner

ray-x commented Mar 26, 2024

Would you post your config? @zyriab

@zyriab
Copy link

zyriab commented Mar 26, 2024

@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!

@saikocat
Copy link

saikocat commented Apr 5, 2024

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?

@ray-x
Copy link
Owner

ray-x commented Apr 8, 2024

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'

@saikocat
Copy link

saikocat commented Apr 8, 2024

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,

@ray-x
Copy link
Owner

ray-x commented Apr 8, 2024

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

@saikocat
Copy link

saikocat commented Apr 8, 2024

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

@ray-x
Copy link
Owner

ray-x commented Apr 8, 2024

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

@penzur
Copy link

penzur commented Apr 14, 2024

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

@penzur
Copy link

penzur commented Apr 14, 2024

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.

@ndzuki
Copy link

ndzuki commented Apr 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants