Vim-go: Add support for gopls rename

Created on 22 Jun 2019  ·  23Comments  ·  Source: fatih/vim-go

It looks like gopls has just implemented support for rename, fixing long standing issue with gorename doesn't work outside of $GOPATH. As gorename unlikely will be fixed, maybe it's time to switch :GoRename implementation from gorename to gopls? https://go-review.googlesource.com/c/tools/+/182585/

Most helpful comment

gopls CLI support for rename, gopls rename just landed. As soon as it's released, I'll update vim-go to use it instead of gorename.

All 23 comments

Thanks for the heads up. I'm almost done with a short break; I'll add support for gopls rename to vim-go as soon as I merge the pending PRs after I get back from break.

Curious if any status update @bhcleek? Would be great to have!

This is high on my list, but there are some things in front of it. When I create the PR, this issue will be updated and then closed when the PR is merged.

FWIW, renaming only works at a package level currently with gopls, although arguable better than gorename not working at all for modules.

https://github.com/golang/go/issues/32877

FYI, related issue in gorename repo: https://github.com/golang/go/issues/27571

TL;DR; - gorename will no longer be supported. gopls already has rename functionality

gopls CLI support for rename, gopls rename just landed. As soon as it's released, I'll update vim-go to use it instead of gorename.

I was able to work around this with gorename with:

go mod vendor
export GO111MODULE=off

My package was in GOPATH, but with GO111MODULE=on, or without the dependencies vendored, it failed anyway.

Hi @bhcleek, gopls rename is on master but a new version has not been published. Do you have any vim-go branch around where I could try the gopls rename integration? Thanks.

Looks like it's released https://github.com/golang/go/issues/33030#issuecomment-549629508

I'm hesitant to modify vim-go to use gopls rename as it currently stands, because gopls rename seems to only rename identifiers in the current package.

Well, problem is, gorename currently more often doesn't work than work, even if project (module-aware) is still located inside GOPATH. So, to me it looks like switching to gopls rename will be an improvement - it's better to have reliable but limited feature (rename within package) than powerful but unreliable feature. Of course, it may make sense to add an option to choose between gorename and gopls rename, if this doesn't add too much work - but I think gorename will be useful only for old projects which doesn't switch to go modules yet.

Also, in my personal experience, I'm renaming only local package identifiers in 99% cases. And in all my current projects (which are all use modules but still located within GOPATH) in about 80% cases attempt to rename something fail, so I actually give up trying to use it and instead do rename manually with search&replace instead - it's less annoying than try and fail again and again, and cry about "good ol' days" when rename always work nice and reliable.

I suppose I can make gorename work if I somehow install in GOPATH all dependencies of current project using exact versions of these dependencies, and repeat this each time when I switch between projects. Problem is, I'm working as same time on dozens of different repos with microservices and libraries, so this sounds really tedious, and it's easier to use search&replace instead.

I've found that with modules enabled, the current GoRename dives into the modcache and tries to compile its contents. This fails each time, and fails the rename.

@bhcleek: I expect that gopls rename will work within an entire module with the next gopls release (https://github.com/golang/go/issues/32877). Also, just to check - is there a reason that it needed to be exposed on the command line for it to be added in vim-go?

Thanks, @stamblerre

It's the easiest way to replace gorename's use in vim-go without signficant refactors to interpret and apply the WorkspaceEdit messages in the response. That will probably come eventually, though.

@bhcleek Thanks, looks like rename works now. But I noticed it leaves *.orig files after rename - how to get rid of them?

I don't see a way to make gopls rename skip creating the backup files. If it's important for you, please consider submitting a PR to gopls.

I've opened an issue https://github.com/golang/go/issues/35551 and there is a CL https://go-review.googlesource.com/c/tools/+/207347, so I hope it'll be fixed soon!

Hey there,

I have gopls 0.3.1 and vim-go b686094cef83ecc913c666a6ebba7daacccecd47 and I still can't rename a simple func scoped local var :-(

Somehow it goes to modules and tries to compile stuff there and fails.

Anyone else with this problem?

Hey @gonzaloserrano

Do you have let g:go_rename_command = 'gopls' in your .vimrc?

@nstratos Thank you very much!

I had rename working some time ago with gopls, then it somehow breaks again, and since then it didn't work… but after your message I've checked my setup and noticed I've wrong variable set instead: let g:go_gorename_command = "gopls". I'm not sure is this variable was in use but then it was renamed by vim-go, or I just made a typo from the beginning (but then it's strange I had gopls rename working some time ago), but now it works again!

It does appear the parameter was renamed from g:go_gorename_command to g:go_rename_command two months ago in commit df4fde28392c5a2a3d41b1b49877c30c26f0f22c. So there was a period of about a month where it was named differently before it was changed.

Oh my, me too... never catched that 🤦‍♀️

Now it works indeed, thanks everyone for noticing the issue.

Whoa! Finally GoRename is working! 🎉
Thanks for the discovery of parameter rename, I wouldn't ever find it myself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielmanesku picture danielmanesku  ·  4Comments

jongillham picture jongillham  ·  3Comments

MattFlower picture MattFlower  ·  4Comments

korjavin picture korjavin  ·  4Comments

smontazeran picture smontazeran  ·  4Comments