Skip to content

Commit

Permalink
Do not format a file that contains an error in the LSP (#3447)
Browse files Browse the repository at this point in the history
Co-authored-by: Doria Keung <[email protected]>
  • Loading branch information
sprsquish and doriable authored Nov 11, 2024
1 parent 62082b2 commit 4343d43
Showing 1 changed file with 12 additions and 0 deletions.
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

0 comments on commit 4343d43

Please sign in to comment.