Comments (8)
It will no longer error in Neovim nighly (as of last night, after this PR neovim/neovim#30288)
And it won't error in Stable after the next release as the fix got backported.
However, it still maps to the wrong spot if the source map doesn't exist.
I see where the problem is in convertGoRangeToTemplRange
.
Happy to take a look shortly if you want me to @a-h
from templ.
You went deep on this one @tris203 - well done! 😁
Thanks. The error is gone in neovim
But it will still you to the wrong line in the file until #912 is complete
from templ.
Thanks for providing a good reproduction. I had to run :set filetype=templ
to get the LSP to attach to the buffer, but once I'd done that, I was able to type the default leader followed by gd to trigger the error (\gd
).
I got similar logs as to the one in the repo. These are the important lines.
{"level":"info","ts":"2024-09-09T08:33:38+01:00","caller":"proxy/server.go:541","msg":"client -> server: Definition"}
{"level":"info","ts":"2024-09-09T08:33:38+01:00","caller":"proxy/server.go:71","msg":"updatePosition: found","uri":"file:///Users/adrian/github.com/catgoose/templ_issue_repro/views/index.templ","fromTempl":"6:5","toGo":"33:24"}
{"level":"info","ts":"2024-09-09T08:33:38+01:00","caller":"proxy/server.go:564","msg":"client -> server: Definition end"}
In particular this:
{
"level":"info",
"ts":"2024-09-09T08:33:38+01:00",
"caller":"proxy/server.go:71",
"msg":"updatePosition: found",
"uri":"file:///Users/adrian/github.com/catgoose/templ_issue_repro/views/index.templ",
"fromTempl":"6:5",
"toGo":"33:24"
}
Note that templ believes that line 6, col 5 in the templ file maps to the line 33, col 24 in the Go code.
I can verify this in the templ LSP web server. Note the highlighted (yellow) section.
This looks correct.
I had updated my version of gopls as per your reproduction instructions. When I put it back, the problem went away, so I went back through the history of gopls to find when the error started happening.
If I install gopls v0.15.2 (go install golang.org/x/tools/[email protected]
), it works OK, but if I use v0.15.3 or greater (go install golang.org/x/tools/[email protected]
), then I get the error.
I'm not sure what changed between those versions. Maybe a reshape of the JSON structures to support a later version of the LSP spec? But, progress on reproduction.
from templ.
Comparison of gopls: golang/tools@gopls/v0.15.2...gopls/v0.15.3
from templ.
What is strange is such as with #893 if you open the file for the target definition, then go to definition works.
content.templ buffer not open, error presented when trying goto definition in index.templ:
{
"level": "info",
"ts": "2024-09-09T06:43:13-05:00",
"caller": "proxy/server.go:541",
"msg": "client -> server: Definition"
}
{
"level": "info",
"ts": "2024-09-09T06:43:13-05:00",
"caller": "proxy/server.go:71",
"msg": "updatePosition: found",
"uri": "file:///home/catgoose/git/templ_issue_repro/views/index.templ",
"fromTempl": "6:4",
"toGo": "33:23"
}
{
"level": "info",
"ts": "2024-09-09T06:43:13-05:00",
"caller": "proxy/server.go:564",
"msg": "client -> server: Definition end"
}
Opened content.templ:
{
"level": "info",
"ts": "2024-09-09T06:45:19-05:00",
"caller": "proxy/server.go:650",
"msg": "client -> server: DidOpen",
"uri": "file:///home/catgoose/git/templ_issue_repro/views/content.templ"
}
{
"level": "info",
"ts": "2024-09-09T06:45:19-05:00",
"caller": "proxy/server.go:674",
"msg": "setting source map cache contents",
"uri": "file:///home/catgoose/git/templ_issue_repro/views/content.templ"
}
{
"level": "info",
"ts": "2024-09-09T06:45:19-05:00",
"caller": "proxy/server.go:681",
"msg": "client -> server: DidOpen end"
}
{
"level": "info",
"ts": "2024-09-09T06:45:19-05:00",
"caller": "proxy/client.go:52",
"msg": "client <- server: PublishDiagnostics"
}
Now try goto definition in Index.templ on Content
{
"level": "info",
"ts": "2024-09-09T06:46:34-05:00",
"caller": "proxy/server.go:541",
"msg": "client -> server: Definition"
}
{
"level": "info",
"ts": "2024-09-09T06:46:34-05:00",
"caller": "proxy/server.go:71",
"msg": "updatePosition: found",
"uri": "file:///home/catgoose/git/templ_issue_repro/views/index.templ",
"fromTempl": "6:4",
"toGo": "33:23"
}
{
"level": "info",
"ts": "2024-09-09T06:46:35-05:00",
"caller": "proxy/server.go:564",
"msg": "client -> server: Definition end"
}
So it seems to me that gopls is trying to target the wrong position, but if the buffer is open it catches it self and says "that's not the right line, but I can see it's on this line because I'm attached to that buffer so we will return that position"
from templ.
I just noticed that!
The issue is that the gopls server is telling templ that the definition exists in a Go file that templ hasn't got in its sourceMap
that maps from templ positions to Go code positions, so it's returning a position in the file that might not exist instead.
The issue is in the convertGoRangeToTemplRange
function. If the Go range is in a *_templ.go
file that it doesn't know about, it probably needs to load the associated *.templ
file, and populate the source map for that file, then everything should work correctly.
However, I'm not sure if we can rely on loading the file from the URI... one way to find out...
from templ.
It will no longer error in Neovim nighly (as of last night, after this PR neovim/neovim#30288) And it won't error in Stable after the next release as the fix got backported.
However, it still maps to the wrong spot if the source map doesn't exist. I see where the problem is in
convertGoRangeToTemplRange
. Happy to take a look shortly if you want me to @a-h
Yes, I can confirm, the problem disappeared with the nightly version!
from templ.
You went deep on this one @tris203 - well done! 😁
from templ.
Related Issues (20)
- Anyway to instantiate a templ once per rendering context? HOT 4
- performance: templ parse takes a long time and uses high CPU when unclosed void elements are used HOT 8
- Little typo in streaming documentation
- bug: `templ generate --watch --proxy` triggers 2 browser reload events HOT 1
- bug: ambiguous child/string expression grammar HOT 4
- bug: go to definition causes error in Neovim v0.10.x HOT 11
- LSP - gopls command error HOT 3
- bug: ComponentScript rendered even when 'if condition' fails HOT 1
- GoToDef .Templ templ() instead of .go func () HOT 2
- bug(lsp): diagnostics error on Windows due to URI encoding HOT 1
- If you write the class attribute 2 times, the second one is silently discarded HOT 1
- bug: discrepancy between buffers in `runtime.go` and `runtime/bufferpool.go` HOT 3
- `templ generate` is loading my layout into styles.css HOT 5
- documentation: templ.SafeURL is not working as described HOT 2
- Setting children from go code HOT 2
- proposal: render individual template fragments
- Taking too much memory with (relatively) larger files HOT 3
- Switch fallthrough not supported
- Suggestion when convert golang template to a-h/templ HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from templ.