Skip to content
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/nametake/golangci-lint-langserver

go 1.23.4

require github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2
require github.com/sourcegraph/jsonrpc2 v0.2.0

require github.com/google/go-cmp v0.6.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2 h1:5VGNYxMxzZ8Jb2bARgVl1DNg8vpcd9S8b4MbbjWQ8/w=
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
github.com/sourcegraph/jsonrpc2 v0.2.0 h1:KjN/dC4fP6aN9030MZCJs9WQbTOjWHhrtKVpzzSrr/U=
github.com/sourcegraph/jsonrpc2 v0.2.0/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
12 changes: 6 additions & 6 deletions golangci-lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import "strings"

type Issue struct {
FromLinter string `json:"FromLinter"`
Text string `json:"Text"`
Severity string `json:"Severity"`
SourceLines []string `json:"SourceLines"`
Replacement interface{} `json:"Replacement"`
FromLinter string `json:"FromLinter"`
Text string `json:"Text"`
Severity string `json:"Severity"`
SourceLines []string `json:"SourceLines"`
Replacement any `json:"Replacement"`
Pos struct {
Filename string `json:"Filename"`
Offset int `json:"Offset"`
Expand Down Expand Up @@ -42,7 +42,6 @@ func (i Issue) DiagSeverity() DiagnosticSeverity {
}
}

//nolint:unused,deadcode
type GolangCILintResult struct {
Issues []Issue `json:"Issues"`
Report struct {
Expand All @@ -51,5 +50,6 @@ type GolangCILintResult struct {
Enabled bool `json:"Enabled"`
EnabledByDefault bool `json:"EnabledByDefault,omitempty"`
} `json:"Linters"`
Error string `json:"Error"`
} `json:"Report"`
}
24 changes: 12 additions & 12 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
args := make([]string, 0, len(h.command))
args = append(args, h.command[1:]...)
args = append(args, dir)
//nolint:gosec
cmd := exec.Command(h.command[0], args...)
if strings.HasPrefix(path, h.rootDir) {
cmd.Dir = h.rootDir
file = path[len(h.rootDir)+1:]
} else {
cmd.Dir = dir
}
h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd", cmd)

h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd:", cmd.Args)

b, err := cmd.Output()
if err == nil {
Expand Down Expand Up @@ -134,7 +134,7 @@ func (h *langHandler) linter() {

diagnostics, err := h.lint(uri)
if err != nil {
h.logger.Printf("%s", err)
h.logger.Printf("%s\n", err)

continue
}
Expand All @@ -146,12 +146,12 @@ func (h *langHandler) linter() {
URI: uri,
Diagnostics: diagnostics,
}); err != nil {
h.logger.Printf("%s", err)
h.logger.Printf("%s\n", err)
}
}
}

func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
h.logger.DebugJSON("golangci-lint-langserver: request:", req)

switch req.Method {
Expand All @@ -176,7 +176,7 @@ func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *json
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}
}

func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
var params InitializeParams
if err := json.Unmarshal(*req.Params, &params); err != nil {
return nil, err
Expand All @@ -198,13 +198,13 @@ func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, r
}, nil
}

func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
close(h.request)

return nil, nil
}

func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
var params DidOpenTextDocumentParams
if err := json.Unmarshal(*req.Params, &params); err != nil {
return nil, err
Expand All @@ -215,15 +215,15 @@ func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.C
return nil, nil
}

func (h *langHandler) handleTextDocumentDidClose(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleTextDocumentDidClose(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
return nil, nil
}

func (h *langHandler) handleTextDocumentDidChange(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleTextDocumentDidChange(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
return nil, nil
}

func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
var params DidSaveTextDocumentParams
if err := json.Unmarshal(*req.Params, &params); err != nil {
return nil, err
Expand All @@ -234,6 +234,6 @@ func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.C
return nil, nil
}

func (h *langHandler) handlerWorkspaceDidChangeConfiguration(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
func (h *langHandler) handlerWorkspaceDidChangeConfiguration(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result any, err error) {
return nil, nil
}
73 changes: 73 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,79 @@ func TestLangHandler_lint_Integration(t *testing.T) {
},
},
},
{
name: "monorepo with multiple go.mod and .golangci.yaml files (foo module)",
h: &langHandler{
logger: newStdLogger(false),
command: command,
rootDir: filepath.Dir("./testdata/monorepo"),
},
filePath: "./testdata/monorepo/foo/main.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 8,
Character: 0,
},
End: Position{
Line: 8,
Character: 0,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("wsl"),
Message: "wsl: block should not end with a whitespace (or comment)",
RelatedInformation: nil,
},
},
},
{
name: "monorepo with multiple go.mod and .golangci.yaml files (bar module)",
h: &langHandler{
logger: newStdLogger(false),
command: command,
rootDir: filepath.Dir("./testdata/monorepo"),
},
filePath: "./testdata/monorepo/bar/main.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 3,
Character: 4,
},
End: Position{
Line: 3,
Character: 4,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("unused"),
Message: "unused: var foo is unused",
RelatedInformation: nil,
},
{
Range: Range{
Start: Position{
Line: 8,
Character: 0,
},
End: Position{
Line: 8,
Character: 0,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("wsl"),
Message: "wsl: block should not end with a whitespace (or comment)",
RelatedInformation: nil,
},
},
},
}

for _, tt := range tests {
Expand Down
12 changes: 6 additions & 6 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package main
import (
"encoding/json"
"log"
"os"
)

var _ logger = (*stdLogger)(nil)

type logger interface {
Printf(format string, args ...interface{})
DebugJSON(label string, arg interface{})
Printf(format string, args ...any)
DebugJSON(label string, arg any)
}

type stdLogger struct {
Expand All @@ -21,22 +20,23 @@ type stdLogger struct {
func newStdLogger(debug bool) *stdLogger {
return &stdLogger{
debug: debug,
stderr: log.New(os.Stderr, "", 0),
stderr: log.New(log.Writer(), "", log.LstdFlags),
}
}

func (l *stdLogger) Printf(format string, args ...interface{}) {
func (l *stdLogger) Printf(format string, args ...any) {
l.stderr.Printf(format, args...)
}

func (l *stdLogger) DebugJSON(label string, arg interface{}) {
func (l *stdLogger) DebugJSON(label string, arg any) {
if !l.debug {
return
}

b, err := json.Marshal(arg)
if err != nil {
l.stderr.Println(err)
return
}

l.stderr.Println(label, string(b))
Expand Down
2 changes: 0 additions & 2 deletions lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type InitializeResult struct {

type TextDocumentSyncKind int

//nolint:unused,deadcode
const (
TDSKNone TextDocumentSyncKind = iota
TDSKFull
Expand Down Expand Up @@ -89,7 +88,6 @@ type DiagnosticRelatedInformation struct {

type DiagnosticSeverity int

//nolint:unused,deadcode
const (
DSError DiagnosticSeverity = iota + 1
DSWarning
Expand Down
5 changes: 5 additions & 0 deletions testdata/monorepo/bar/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"
linters:
enable:
- unused
- wsl
3 changes: 3 additions & 0 deletions testdata/monorepo/bar/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module foo

go 1.22.10
9 changes: 9 additions & 0 deletions testdata/monorepo/bar/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

var bar = "bar"
var foo = "foo"

func Bar() {
_ = bar

}
6 changes: 6 additions & 0 deletions testdata/monorepo/foo/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"
linters:
enable:
- wsl
disable:
- unused
3 changes: 3 additions & 0 deletions testdata/monorepo/foo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module foo

go 1.22.10
9 changes: 9 additions & 0 deletions testdata/monorepo/foo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

var bar = "bar"
var foo = "foo"

func Bar() {
_ = bar

}
1 change: 0 additions & 1 deletion uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func uriToPath(uri string) string {
}

func isWindowsDriveURIPath(uri string) bool {
//nolint:gomnd
if len(uri) < 4 {
return false
}
Expand Down