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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased]

- No changes yet.
- Breaking analysis support for `buf beta lsp`.

## [v1.47.2] - 2024-11-14

Expand Down
43 changes: 32 additions & 11 deletions private/buf/buflsp/buflsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ 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,
Expand All @@ -89,8 +90,9 @@ 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
Expand Down Expand Up @@ -130,19 +132,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
})
}
63 changes: 63 additions & 0 deletions private/buf/buflsp/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.
//
// These are part of the public API of the LSP server, in that any user of
// the LSP can configure it using these keys. The server requests them over
// the LSP protocol as-needed.
//
// Keep in sync with bufbuild/vscode-buf package.json.
const (
// The strategy for how to calculate the --against input for a breaking
// check. This must be one of the following values:
//
// - "git". Use a particular Git revision to find the against file.
//
// - "disk". Use the last-saved value on disk as the against file.
ConfigBreakingStrategy = "buf.checks.breaking.againstStrategy"
// The Git revision to use for calculating the --against input for a
// breaking check when using the "git" strategy.
ConfigBreakingGitRef = "buf.checks.breaking.againstGitRef"
)

const (
// Compare against the configured git branch.
againstGit againstStrategy = iota + 1
// Against the last saved file on disk (i.e. saved vs unsaved changes).
againstDisk
)

// againstStrategy is a strategy for selecting which version of a file to use as
// --against for the purposes of breaking lints.
type againstStrategy int

// parseAgainstStrategy parses an againstKind from a config setting sent by
// the client.
//
// Returns againstTrunk, false if the value is not recognized.
func parseAgainstStrategy(s string) (againstStrategy, bool) {
switch s {
// These values are the same as those present in the package.json for the
// VSCode client.
case "git":
return againstGit, true
case "disk":
return againstDisk, true
default:
return againstGit, false
}
}
Loading