Skip to content

Commit 4343d43

Browse files
sprsquishdoriable
andauthored
Do not format a file that contains an error in the LSP (#3447)
Co-authored-by: Doria Keung <[email protected]>
1 parent 62082b2 commit 4343d43

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

private/buf/buflsp/server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ func (s *server) Formatting(
208208
return nil, fmt.Errorf("received update for file that was not open: %q", params.TextDocument.URI)
209209
}
210210

211+
// We check the diagnostics on the file, if there are any build errors, we do not want
212+
// to format an invalid AST, so we skip formatting and return an error for logging.
213+
errorCount := 0
214+
for _, diagnostic := range file.diagnostics {
215+
if diagnostic.Severity == protocol.DiagnosticSeverityError {
216+
errorCount += 1
217+
}
218+
}
219+
if errorCount > 0 {
220+
return nil, fmt.Errorf("cannot format file %q, %v error(s) found.", file.uri.Filename(), errorCount)
221+
}
222+
211223
// Currently we have no way to honor any of the parameters.
212224
_ = params
213225
if file.fileNode == nil {

0 commit comments

Comments
 (0)