a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.04k stars 264 forks source link

lsp: panic on codeAction #771

Closed xab3r closed 4 months ago

xab3r commented 4 months ago

Hi, I'm using SublimeText with LSP plugin. The Templ LSP always throws the error. Looks like it's happening on codeAction

$ templ version
v0.2.707
:: [20:00:01.561] --> templ textDocument/codeAction (4): {'textDocument': {'uri': 'file:///<...>/components/layout.templ'}, 'range': {'start': {'line': 8, 'character': 0}, 'end': {'line': 8, 'character': 0}}, 'context': {'diagnostics': [], 'triggerKind': 2}}
templ: panic: runtime error: invalid memory address or nil pointer dereference
templ: [signal SIGSEGV: segmentation violation code=0x2 addr=0x10 pc=0x104b0cd98]
templ: 
templ: goroutine 25 [running]:
templ: github.com/a-h/templ/cmd/templ/lspcmd/proxy.(*Server).CodeAction(0x140001b2050, {0x104cf1470, 0x140001b2230}, 0x14000200f00)
templ:  /Users/yevhen/developing/go/pkg/mod/github.com/a-h/templ@v0.2.707/cmd/templ/lspcmd/proxy/server.go:294 +0x358
templ: github.com/a-h/protocol.serverDispatch({0x104cf1470, 0x140001b2230}, {0x104cf6880, 0x140001b2050}, 0x140003e29c0, {0x104cf1748, 0x140001964c0})
templ:  /Users/yevhen/developing/go/pkg/mod/github.com/a-h/protocol@v0.0.0-20230224160810-b4eec67c1c22/server.go:158 +0x3e14
templ: github.com/a-h/protocol.NewServer.ServerHandler.func1({0x104cf1470, 0x140001b2230}, 0x140003e29c0, {0x104cf1748, 0x140001964c0})
templ:  /Users/yevhen/developing/go/pkg/mod/github.com/a-h/protocol@v0.0.0-20230224160810-b4eec67c1c22/server.go:36 +0x6c
templ: github.com/a-h/protocol.Handlers.ReplyHandler.func1({0x104cf1470, 0x140001b2230}, 0x1400019c300, {0x104cf1748, 0x140001964c0})
templ:  /Users/yevhen/developing/go/pkg/mod/go.lsp.dev/jsonrpc2@v0.10.0/handler.go:35 +0xc0
templ: github.com/a-h/protocol.Handlers.AsyncHandler.func2.2()
templ:  /Users/yevhen/developing/go/pkg/mod/go.lsp.dev/jsonrpc2@v0.10.0/handler.go:114 +0x78
templ: created by github.com/a-h/protocol.Handlers.AsyncHandler.func2 in goroutine 22
templ:  /Users/yevhen/developing/go/pkg/mod/go.lsp.dev/jsonrpc2@v0.10.0/handler.go:112 +0x164
a-h commented 4 months ago

Looks like we're missing a nil check at https://github.com/a-h/templ/blob/108500b21cdfabfbbb82e0d0d434eb1075bee8c8/cmd/templ/lspcmd/proxy/server.go#L294-L300

a-h commented 4 months ago

It seems that r.Edit can be nil, and there's no check.

a-h commented 4 months ago

If you can try the commit above, that would be great.

xab3r commented 4 months ago

@a-h Yeah, it doesn't panic with this commit. Thanks!

saada commented 3 months ago

In case others run into this, I had to patch the global templ installation to get this working

go install github.com/a-h/templ/cmd/templ@73474f65da4509c02aa0666f3277629c8c75e176
tim-rus commented 3 months ago

https://github.com/a-h/templ/issues/800#issuecomment-2183439322

Rolled back to gopls@v0.15.3 and that fixed the problem.