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

Implement breaking lints in the LSP #3404

Merged
merged 10 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ issues:
# G115 checks for use of truncating conversions.
path: private/buf/buflsp/file.go
text: "G115:"
- linters:
- gosec
# G115 checks for use of truncating conversions.
path: private/buf/buflsp/image.go
text: "G115:"
- linters:
- gosec
# G115 checks for use of truncating conversions.
Expand Down
46 changes: 35 additions & 11 deletions private/buf/buflsp/buflsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/bufbuild/buf/private/buf/bufctl"
"github.com/bufbuild/buf/private/bufpkg/bufcheck"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/slogext"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
Expand Down Expand Up @@ -65,11 +66,13 @@ func Serve(
&connWrapper{Conn: conn, logger: container.Logger()},
zap.NewNop(), // The logging from protocol itself isn't very good, we've replaced it with connAdapter here.
),
container: container,
logger: container.Logger(),
controller: controller,
checkClient: checkClient,
rootBucket: bucket,
wktBucket: wktBucket,
runner: command.NewRunner(),
}
lsp.fileManager = newFileManager(lsp)
off := protocol.TraceOff
Expand All @@ -89,14 +92,16 @@ func Serve(
// Its handler methods are not defined in buflsp.go; they are defined in other files, grouped
// according to the groupings in
type lsp struct {
conn jsonrpc2.Conn
client protocol.Client
conn jsonrpc2.Conn
client protocol.Client
container appext.Container

logger *slog.Logger
controller bufctl.Controller
checkClient bufcheck.Client
rootBucket storage.ReadBucket
fileManager *fileManager
runner command.Runner

wktBucket storage.ReadBucket

Expand Down Expand Up @@ -130,19 +135,38 @@ func (l *lsp) init(_ context.Context, params *protocol.InitializeParams) error {
// to inject debug logging, tracing, and timeouts to requests.
func (l *lsp) newHandler() jsonrpc2.Handler {
actual := protocol.ServerHandler(newServer(l), nil)
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) (retErr error) {
defer slogext.DebugProfile(l.logger, slog.String("method", req.Method()), slog.Any("params", req.Params()))()
return jsonrpc2.AsyncHandler(func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
l.logger.Debug(
"handling request",
slog.String("method", req.Method()),
slog.Any("params", req.Params()),
)
defer slogext.DebugProfile(
l.logger,
slog.String("method", req.Method()),
slog.Any("params", req.Params()),
)()

replier := l.wrapReplier(reply, req)

// Verify that the server has been initialized if this isn't the initialization
// request.
var err error
if req.Method() != protocol.MethodInitialize && l.initParams.Load() == nil {
return replier(ctx, nil, fmt.Errorf("the first call to the server must be the %q method", protocol.MethodInitialize))
// Verify that the server has been initialized if this isn't the initialization
// request.
err = replier(ctx, nil, fmt.Errorf("the first call to the server must be the %q method", protocol.MethodInitialize))
} else {
l.lock.Lock()
err = actual(ctx, replier, req)
l.lock.Unlock()
}

l.lock.Lock()
defer l.lock.Unlock()
return actual(ctx, replier, req)
}
if err != nil {
l.logger.Error(
"error while replying to request",
slog.String("method", req.Method()),
slogext.ErrorAttr(err),
)
}
return nil
})
}
24 changes: 24 additions & 0 deletions private/buf/buflsp/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buflsp

// Configuration keys for the LSP.
// Keep in sync with bufbuild/vscode-buf/package.json.
mcy marked this conversation as resolved.
Show resolved Hide resolved
const (
ConfigCLIPath = "buf.commandLine.path"
ConfigCLIUpdates = "buf.commandLine.checkForUpdates"
doriable marked this conversation as resolved.
Show resolved Hide resolved
ConfigBreakingStrategy = "buf.linting.breakingAgainst.strategy"
ConfigBreakingGitRef = "buf.linting.breakingAgainst.gitRef"
doriable marked this conversation as resolved.
Show resolved Hide resolved
)
Loading