Skip to content
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

Do not format a file that contains an error in the LSP #3447

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions private/buf/buflsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ func (s *server) Formatting(
return nil, fmt.Errorf("received update for file that was not open: %q", params.TextDocument.URI)
}

// We check the diagnostics on the file, if there are any build errors, we do not want
// to format an invalid AST, so we skip formatting and return an error for logging.
errorCount := 0
for _, diagnostic := range file.diagnostics {
if diagnostic.Severity == protocol.DiagnosticSeverityError {
errorCount += 1
}
}
if errorCount > 0 {
return nil, fmt.Errorf("cannot format file %q, %v error(s) found.", file.uri.Filename(), errorCount)
}

// Currently we have no way to honor any of the parameters.
_ = params
if file.fileNode == nil {
Expand Down