Skip to content

Commit 75aae1d

Browse files
authored
Fix LSP error diagnostics on build (#4204)
1 parent 357f743 commit 75aae1d

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

private/buf/buflsp/file.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -854,36 +854,33 @@ func (f *file) RunChecks(ctx context.Context) {
854854
f.cancelChecks = cancel
855855

856856
go func() {
857-
image, diagnostics := buildImage(ctx, path, f.lsp.logger, opener)
858-
if image == nil {
859-
f.lsp.logger.DebugContext(ctx, "checks cancelled on image build", slog.String("uri", f.uri.Filename()))
860-
return
861-
}
862-
863-
f.lsp.logger.DebugContext(ctx, "checks running lint", slog.String("uri", f.uri.Filename()), slog.String("module", module.OpaqueID()))
864857
var annotations []bufanalysis.FileAnnotation
865-
if err := checkClient.Lint(
866-
ctx,
867-
workspace.GetLintConfigForOpaqueID(module.OpaqueID()),
868-
image,
869-
bufcheck.WithPluginConfigs(workspace.PluginConfigs()...),
870-
bufcheck.WithPolicyConfigs(workspace.PolicyConfigs()...),
871-
); err != nil {
872-
var fileAnnotationSet bufanalysis.FileAnnotationSet
873-
if !errors.As(err, &fileAnnotationSet) {
874-
if errors.Is(err, context.Canceled) {
875-
f.lsp.logger.DebugContext(ctx, "checks cancelled", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
876-
} else if errors.Is(err, context.DeadlineExceeded) {
877-
f.lsp.logger.WarnContext(ctx, "checks deadline exceeded", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
858+
image, diagnostics := buildImage(ctx, path, f.lsp.logger, opener)
859+
if image != nil {
860+
f.lsp.logger.DebugContext(ctx, "checks running lint", slog.String("uri", f.uri.Filename()), slog.String("module", module.OpaqueID()))
861+
if err := checkClient.Lint(
862+
ctx,
863+
workspace.GetLintConfigForOpaqueID(module.OpaqueID()),
864+
image,
865+
bufcheck.WithPluginConfigs(workspace.PluginConfigs()...),
866+
bufcheck.WithPolicyConfigs(workspace.PolicyConfigs()...),
867+
); err != nil {
868+
var fileAnnotationSet bufanalysis.FileAnnotationSet
869+
if !errors.As(err, &fileAnnotationSet) {
870+
if errors.Is(err, context.Canceled) {
871+
f.lsp.logger.DebugContext(ctx, "checks cancelled", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
872+
} else if errors.Is(err, context.DeadlineExceeded) {
873+
f.lsp.logger.WarnContext(ctx, "checks deadline exceeded", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
874+
} else {
875+
f.lsp.logger.WarnContext(ctx, "checks failed", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
876+
}
877+
return
878+
}
879+
if len(fileAnnotationSet.FileAnnotations()) == 0 {
880+
f.lsp.logger.DebugContext(ctx, "checks lint passed", slog.String("uri", f.uri.Filename()))
878881
} else {
879-
f.lsp.logger.WarnContext(ctx, "checks failed", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
882+
annotations = append(annotations, fileAnnotationSet.FileAnnotations()...)
880883
}
881-
return
882-
}
883-
if len(fileAnnotationSet.FileAnnotations()) == 0 {
884-
f.lsp.logger.DebugContext(ctx, "checks lint passed", slog.String("uri", f.uri.Filename()))
885-
} else {
886-
annotations = append(annotations, fileAnnotationSet.FileAnnotations()...)
887884
}
888885
}
889886

private/buf/buflsp/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func buildImage(
9191
}
9292
}
9393
if len(compiled) == 0 || compiled[0] == nil {
94-
return nil, nil // Image failed to build.
94+
return nil, diagnostics // Image failed to build.
9595
}
9696
compiledFile := compiled[0]
9797

0 commit comments

Comments
 (0)