Skip to content

Return errors instead of panics #638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

makhov
Copy link

@makhov makhov commented Mar 16, 2025

tsgo heavily overuses panics, that can lead to the situations mentioned in #631:

The typescript-go-lsp server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.

PR is intended to start moving the codebase towards more idiomatic error handling.
Atm, I've changed 2 panics and we probably need to agree on the approach: I can keep adding changes to this PR, but it'll be big and hard to follow, or I can create a separate PRs to keep them small and simple.

@makhov
Copy link
Author

makhov commented Mar 16, 2025

@microsoft-github-policy-service agree

program := l.GetProgram()
file := program.GetSourceFile(fileName)
if file == nil {
panic("file not found")
Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that these are mainly invariants that shouldn't be broken, such that we'd want to know about them. But, maybe we need to surface these differently (like gopls' bug.Report), or as a general panic handler (sort of like old TS with exceptions, which we also did not use except in tragic cases).

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, but it happened in the mentioned issue, and I'm not sure it should crash the LSP server.

I don't have any strong opinion, because it's always up to the team and the project. But in general, panics are for unexpected errors, this one (even if it should never happen in real life) is kinda expected.
And I'm not a big fan of global recovery exactly because if some panic happens, we'd want to know about it.

I think the same problem would be reported in issue even without panic anyway, since it's a bug (at least looks like the one).

Choose a reason for hiding this comment

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

I agree, lsp servers shouldn't crash for file not found cases. At least the ts lsp , go lsp doesn't crash. I think we need an error channel to manage errors async. The Current version #645 seems to be halting at every error.

@makhov makhov marked this pull request as ready for review March 17, 2025 13:01
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.

3 participants