Giter VIP home page Giter VIP logo

Comments (8)

tris203 avatar tris203 commented on September 15, 2024 2

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.

tris203 avatar tris203 commented on September 15, 2024 1

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.

a-h avatar a-h commented on September 15, 2024

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.

Screenshot 2024-09-09 at 08 33 12

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.

a-h avatar a-h commented on September 15, 2024

Comparison of gopls: golang/tools@gopls/v0.15.2...gopls/v0.15.3

from templ.

catgoose avatar catgoose commented on September 15, 2024

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.

a-h avatar a-h commented on September 15, 2024

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.

axzilla avatar axzilla commented on September 15, 2024

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.

a-h avatar a-h commented on September 15, 2024

You went deep on this one @tris203 - well done! 😁

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.