Open renthraysk opened 1 month ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Thanks for the report with the repro. I could reproduce the issue. (Personally, I am surprised that rename works on sources in broken state :-))
gopls trace:
[Trace - 15:55:00.783 PM] Sending request 'textDocument/prepareRename - (39)'.
Params: {"textDocument":{"uri":"file:///Users/hakim/xx/main.go"},"position":{"line":7,"character":3}}
[Trace - 15:55:00.784 PM] Received response 'textDocument/prepareRename - (39)' in 0ms.
Result: {"range":{"start":{"line":7,"character":1},"end":{"line":7,"character":6}},"placeholder":"attrs"}
...
[Trace - 15:55:04.343 PM] Sending request 'textDocument/rename - (42)'.
Params: {"textDocument":{"uri":"file:///Users/hakim/xx/main.go"},"position":{"line":7,"character":3},"newName":"fhAttrs"}
[Trace - 15:55:04.344 PM] Received response 'textDocument/rename - (42)' in 0ms.
Result: {"documentChanges":[
{"textDocument":{
"version":1,"uri":"file:///Users/hakim/xx/main.go"},
"edits":[
{"range":{"start":{"line":7,"character":1},"end":{"line":7,"character":6}},"newText":"fhAttrs"},
{"range":{"start":{"line":13,"character":2},"end":{"line":13,"character":7}},"newText":"fhAttrs"}]}]}
This is because the rename()
function resolves this .
package main
import (
"log/slog"
)
func Log(log *slog.Logger, buf []byte) {
attrs := []slog.Attr{
0: {Key: "len"},
}
if len(buf) < 9 {
log._ // Notice here. Added a placeholder.
}
attrs[0].Value = slog.IntValue(len(buf))
}
The protocol.NewMapper()
function reads the original file without placeholders.
package main
import (
"log/slog"
)
func Log(log *slog.Logger, buf []byte) {
attrs := []slog.Attr{
0: {Key: "len"},
}
if len(buf) < 9 {
log.
}
attrs[0].Value = slog.IntValue(len(buf))
}
Change https://go.dev/cl/599975 mentions this issue: gopls/rename: Fix rename failure when code contains syntax error.
Go version
go version go1.22.5 linux/amd64 & golang.org/x/tools/gopls v0.16.1
Output of
go env
in your module/workspace:What did you do?
Using Rename Symbol on the first
attrs
to rename tofhAttrs
in the function below, results in the other usage of the symbol to be changed toafhAttrs0]
What did you see happen?
What did you expect to see?
Either fail to rename the symbol due to the syntax error, or work as expected.