Golang Rename in vs-code is unusable, Here’s a fix for it!

Rename is an essential tool in any coding language and vs-code is a very popular IDE for Go, so it is surprising that this issue was never solved. When looking it up online, I found out that the vs-code plugin for golang just runs golang’s own rename tool. Golang is fairly fast, and most golang tools are properly constructed, So why is it so slow?

The problem is that this tool often thinks it needs to search through the *Entire GOPATH* for any dependencies that might need renaming, which is never what anyone pressing F2 is expecting. Searching the git issues, I found that there is a fix out there, @github.com/parteek already commented out the global search code, but when compiling his gorename I still got the same issue, and there was no compiled binary. Turns out it somehow still uses the golang.org/x/tools/refactor/rename package when I build it…

So I thought I’ll make it easier, I reorganized the code into one package, and tested that it works. comparison with go 1.11 code shows that the “tools/refactor/rename” code hasn’t changed since.

installing:

go install github.com/amitbet/gorename

You should now have a new gorename.exe in your $GOPATH/bin directory.

Now renaming in vs-code works within two seconds (due to code checking and vetting done before running gorename)

Original hack: https://github.com/prateek/tools/commit/72b0e988ce6957db328582decda8829154c66545

Any Issues?

If you is still get a 100% CPU gorename.exe process, you are using it from the wrong directory, check the process' command line in task manager, or run

Windows:

where gorename.exe

Or on a mac:

which gorename.exe

Then change your PATH variable so the right gorename.exe is invoked, or overwrite the other one with the new one in your $GOPATH/bin directory.