Skip to content

Commit 62486a7

Browse files
committed
fix: didn't work with goheader linter
1 parent 5cc8588 commit 62486a7

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

handler.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os/exec"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/sourcegraph/jsonrpc2"
1112
)
@@ -29,6 +30,21 @@ type langHandler struct {
2930
noLinterName bool
3031

3132
rootURI string
33+
rootDir string
34+
}
35+
36+
func (h *langHandler) errToDiagnostics(err error) []Diagnostic {
37+
var message string
38+
switch e := err.(type) {
39+
case *exec.ExitError:
40+
message = string(e.Stderr)
41+
default:
42+
h.logger.DebugJSON("golangci-lint-langserver: errToDiagnostics message", message)
43+
message = e.Error()
44+
}
45+
return []Diagnostic{
46+
{Severity: DSError, Message: message},
47+
}
3248
}
3349

3450
func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
@@ -37,18 +53,31 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
3753
path := uriToPath(string(uri))
3854
dir, file := filepath.Split(path)
3955

56+
args := make([]string, 0, len(h.command))
57+
args = append(args, h.command[1:]...)
58+
args = append(args, dir)
4059
//nolint:gosec
41-
cmd := exec.Command(h.command[0], h.command[1:]...)
42-
cmd.Dir = dir
60+
cmd := exec.Command(h.command[0], args...)
61+
if strings.HasPrefix(path, h.rootURI) {
62+
cmd.Dir = h.rootDir
63+
file = path[len(h.rootDir)+1:]
64+
} else {
65+
cmd.Dir = dir
66+
}
67+
h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd", cmd)
4368

4469
b, err := cmd.Output()
4570
if err == nil {
4671
return diagnostics, nil
72+
} else if len(b) == 0 {
73+
// golangci-lint would output critical error to stderr rather than stdout
74+
// https://github.com/nametake/golangci-lint-langserver/issues/24
75+
return h.errToDiagnostics(err), nil
4776
}
4877

4978
var result GolangCILintResult
5079
if err := json.Unmarshal(b, &result); err != nil {
51-
return diagnostics, err
80+
return h.errToDiagnostics(err), nil
5281
}
5382

5483
h.logger.DebugJSON("golangci-lint-langserver: result:", result)
@@ -142,6 +171,7 @@ func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, r
142171
}
143172

144173
h.rootURI = params.RootURI
174+
h.rootDir = uriToPath(params.RootURI)
145175
h.conn = conn
146176
h.command = params.InitializationOptions.Command
147177

0 commit comments

Comments
 (0)