-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/gopls: rename fails with "conflicts with var in same block" when the new identifier already exists #41852
Comments
I'm not sure this would be safe behavior in general. Consider a case like: func _(greatName int) int {
badName := 123
greatName = badName - greatName
// lots of other code
return badName
} The user is looking at the bottom of the function and really doesn't like |
... that's what undo is for‽ |
What if the user doesn't notice their mistake to undo it? I agree with @muirdm that we can't support this case by default. Maybe with a |
Also remember that rename applies to the entire module including files not open in your editor. If the user is in the middle of a big change with many modified files then a botched rename might be impossible to undo automatically. |
Looking through old issues, I came upon this. I don't think we should do this. IMO, failing is the correct thing to do. One of the invariants we try to enforce is that refactoring operations do not break the build. |
I can't speak for everyone, but a build that is already deeply broken is my typical state when I'm working on a large change. I would much rather have a Since I've run into so many cases where (Today I usually just use |
FWIW @adonovan remarked that gopls had lifted most of the guards against renaming inside a broken package, and was surprised that it still worked most of the time. I guess gorename was very strict about not doing anything in the presence of errors. Sorry you can't use gopls renaming, and surprised. I use it ~all the time, and rarely have such annoyances. Perhaps we're using it differently. |
I use You raise an interesting question about strictness and utility; I was mulling the same question while developing https://go.dev/cl/519715 for cmd/fix and gopls. Batch tools like cmd/fix want to be completely strict while interactive tools like gopls may need to be more aggressive even at the risk of making mistakes--so long as the mistakes are easily observed and reverted; subtle semantic changes are a bad idea for any tool. But it's very hard to know when ignoring type errors will lead to the first kind of mistake or the second. |
On further thought: in theory — given complete control over the LSP protocol and some ideal UI — this could be a separate kind of refactoring from “rename”. We could call it, say, “merge variables”. 🤔 The downside of that approach is that it would complicate the UX: as a user I would have to learn yet another keybinding, or type out a much longer command by name, or figure out how to add a single keybinding that implements “rename or merge variables”. |
What if we allow slaphappy renames but save off a patch of the changes to let the user "undo" if they want? |
Shouldn't the IDE already be saving a patch of the changes? |
Not if the files aren't open in the IDE. But that is another option - if the renames are contained to a single file, be more aggressive. |
Yeah, usually I'm doing this for a variable local to a single function, so that would be viable for most of my use-cases. |
Rename edits should result in the files being opened by the client. |
This is related to (but slightly different from) #41851.
What did you do?
Follow the same steps to reproduce as for #41851, but for step (1) use a declared identifier instead of an undeclared one.
(https://play.golang.org/p/_L1Lkry0R0o)
What did you expect to see?
The identifier at the point, as well as all references to it, should be renamed, despite the fact that the new identifier conflicts with the existing declaration.
The resulting program should now have a
redeclared in this block
error (https://play.golang.org/p/IP2ZkF7qnIe), which the user may resolve by removing one or the other of the conflicting declarations.What did you see instead?
The text was updated successfully, but these errors were encountered: