Skip to content

Add an LSP server command#3316

Merged
mcy merged 25 commits intomainfrom
mcy/lsp
Sep 26, 2024
Merged

Add an LSP server command#3316
mcy merged 25 commits intomainfrom
mcy/lsp

Conversation

@mcy
Copy link
Copy Markdown
Member

@mcy mcy commented Sep 12, 2024

This PR adds a new command, buf beta lsp, which serves an LSP for Protobuf files.

Most of the code lives under buflsp. Currently, the LSP implements:

  • Parse and diagnose.
  • Format on save.
  • Go to definition, inc. go to definition of Buf dependencies.
  • Semantic highlighting.
  • Symbol tooltips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 12, 2024

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedSep 25, 2024, 4:46 PM

@mcy mcy changed the title Add an LSP server command. Add an LSP server command Sep 12, 2024
@mcy mcy marked this pull request as ready for review September 12, 2024 20:27
Comment thread private/buf/buflsp/file_manager.go Outdated
}

// newFiles creates a new file manager.
func newFiles(lsp *lsp) *fileManager {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newFileManager

Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/file.go Outdated
Comment thread private/buf/buflsp/buflsp.go Outdated
@mcy mcy requested a review from doriable September 16, 2024 19:59
Comment thread private/buf/buflsp/mutex.go
Comment thread private/buf/buflsp/mutex.go Outdated
Comment thread private/buf/buflsp/mutex.go
Comment thread private/buf/buflsp/mutex.go Outdated
Copy link
Copy Markdown
Member

@bufdev bufdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my phone but don't forget changelog if we haven't done it

Comment thread private/buf/buflsp/mutex.go Outdated

func (mu *mutex) storeWho(id uint64) {
for {
// This has to be a CAS loop to avoid races with a poisoning p.
Copy link
Copy Markdown
Contributor

@emcfarlane emcfarlane Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. If you are unable to swap it's confirmed to be poisoned as the lock is always held.

if !mu.who.CompareAndSwap(0, id) {
	panic("buflsp/mutex.go: non-reentrant lock locked twice by the same request")
}

mu.storeWho(0)

mu.pool.check(id, mu, true)
mu.lock.Unlock()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mu.lock.Unlock() should be deferred, as this never gets called on panic of storeWho. So go routines will still deadlock on line 150 as they wait for this Unlock. Although does this matter? Would only matter if using recovers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcy can you check over all of your concurrency logic here before we merge?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah -- I think that's the crux of this, it seems to me that panic's are used under the assumption that we are crashing the program to avoid attempting to recover from bad code-patterns (and thus, hopefully, preventing us from merging bad patterns). That being said, deferring here to make the pattern with storeWho a little less fragile is probably a good thing.

Comment thread private/buf/buflsp/file_manager.go Outdated
// by the editor.
type fileManager struct {
lsp *lsp
table refcount.Map[protocol.URI, file]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doriable can you do a pass on naming here? None of these names match our style

Copy link
Copy Markdown
Member

@doriable doriable left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some comments around style/readability.

Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/buflsp.go
Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/buflsp.go Outdated
Comment thread private/buf/buflsp/file.go Outdated
Comment thread private/buf/buflsp/file_manager.go Outdated
Comment thread private/buf/buflsp/mutex.go Outdated
Comment thread private/buf/buflsp/report.go
Comment thread private/buf/buflsp/report.go Outdated
Comment thread private/buf/buflsp/file.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants