Skip to content

Commit

Permalink
fix: golangci config, linter errors (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Oct 18, 2024
1 parent 68655f0 commit 0c956c5
Show file tree
Hide file tree
Showing 84 changed files with 373 additions and 370 deletions.
38 changes: 16 additions & 22 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ run:
- integration
concurrency: 4
issues-exit-code: 1
skip-dirs:
- "docs"
- "licenses"
- "pact"
- "ampli"
- ".bin"
- ".github"
- ".vscode"
- "build"
tests: true
timeout: 5m

Expand Down Expand Up @@ -40,7 +31,6 @@ linters-settings:
gosimple:
checks: ['all']
govet:
check-shadowing: true
enable-all: true
disable:
- fieldalignment
Expand Down Expand Up @@ -69,7 +59,6 @@ linters-settings:
- name: error-strings
- name: error-naming
- name: exported
disable-stuttering-check: true
- name: if-return
- name: increment-decrement
- name: var-naming
Expand All @@ -92,8 +81,6 @@ linters-settings:
stylecheck:
checks: ['all']
http-status-code-whitelist: []
varcheck:
exported-fields: true

linters:
enable:
Expand All @@ -109,14 +96,11 @@ linters:
- dupl
- durationcheck
- errname
# TODO(errorlint): revisit
#- errorlint
- errorlint
- exhaustive
- exportloopref
# TODO(forbidigo): revisit
#- forbidigo
# TODO(forcetypeassert): revisit this one! beware fragile asserts in this codebase
#- forcetypeassert
- copyloopvar
- forbidigo
- forcetypeassert
# TODO(goconst): revisit
#- goconst
# TODO(gocritic): revisit
Expand All @@ -127,7 +111,7 @@ linters:
# TODO(gofumpt): revisit
#- gofumpt
# TODO(goimports): revisit
#- goimports
- goimports
- goprintffuncname
# TODO(gosec): revisit; consequences of revoking non-owner file permissions?
#- gosec
Expand All @@ -153,7 +137,7 @@ linters:
- predeclared
- promlinter
# TODO(revive): revisit in a followup; extensive changes, some breaking. godoc requirement would be good to introduce
#- revive
# - revive
- rowserrcheck
- sqlclosecheck
# TODO(stylecheck): revisit in a followup; some breaking API changes
Expand All @@ -174,6 +158,16 @@ linters:
#- wrapcheck

issues:
exclude-dirs:
- "docs"
- "licenses"
- "pact"
- "ampli"
- ".bin"
- ".github"
- ".vscode"
- "build"

exclude-rules:
- path: _test\.go
linters:
Expand Down
2 changes: 1 addition & 1 deletion application/codeaction/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *CodeActionsService) getQuickFixAction(quickFixGroupables []types.Groupa
// right now we can always group by max semver version, as
// code only has one quickfix available, and iac none at all
var quickFix *snyk.CodeAction
qf, ok := types.MaxSemver()(quickFixGroupables).(snyk.CodeAction)
qf, ok := types.MaxSemver(c.logger)(quickFixGroupables).(snyk.CodeAction)
if !ok {
c.logger.Warn().Msg("grouping quick fix actions failed")
quickFix = nil
Expand Down
1 change: 1 addition & 0 deletions application/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
frameworkLogging "github.com/snyk/go-application-framework/pkg/logging"
"github.com/snyk/go-application-framework/pkg/runtimeinfo"
"github.com/snyk/go-application-framework/pkg/workflow"

"github.com/snyk/snyk-ls/infrastructure/cli/cli_constants"
"github.com/snyk/snyk-ls/infrastructure/cli/filename"
"github.com/snyk/snyk-ls/internal/concurrency"
Expand Down
1 change: 1 addition & 0 deletions application/di/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
codeClient "github.com/snyk/code-client-go"
codeClientHTTP "github.com/snyk/code-client-go/http"
codeClientObservability "github.com/snyk/code-client-go/observability"

"github.com/snyk/snyk-ls/application/codeaction"
"github.com/snyk/snyk-ls/application/config"
appNotification "github.com/snyk/snyk-ls/application/server/notification"
Expand Down
15 changes: 7 additions & 8 deletions application/entrypoint/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
func OnPanicRecover() {
if err := recover(); err != nil {
panickingMsg := "🚨 Panicking 🚨"
fmt.Println(panickingMsg)
fmt.Println(err)
_, _ = fmt.Fprintln(os.Stderr, panickingMsg)
_, _ = fmt.Fprintln(os.Stderr, err)
debug.PrintStack()

c := config.CurrentConfig()
Expand All @@ -31,10 +31,9 @@ func OnPanicRecover() {
}

func PrintLicenseText(output string) {
fmt.Fprintln(os.Stderr, "License information")
fmt.Fprintln(os.Stderr, "Snyk Language Server is licensed under the Apache 2.0 license")
fmt.Fprintln(os.Stderr, "The following dependencies and licenses are used in this project:")
fmt.Fprintln(os.Stderr, strings.ReplaceAll(output, " ", "\n"))
fmt.Fprintln(os.Stderr,
"You can access the detailed license information under https://github.com/snyk/snyk-ls/tree/main/licenses")
_, _ = fmt.Fprintln(os.Stderr, "License information")
_, _ = fmt.Fprintln(os.Stderr, "Snyk Language Server is licensed under the Apache 2.0 license")
_, _ = fmt.Fprintln(os.Stderr, "The following dependencies and licenses are used in this project:")
_, _ = fmt.Fprintln(os.Stderr, strings.ReplaceAll(output, " ", "\n"))
_, _ = fmt.Fprintln(os.Stderr, "You can access the detailed license information under https://github.com/snyk/snyk-ls/tree/main/licenses")
}
1 change: 1 addition & 0 deletions application/server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/creachadair/jrpc2/handler"

"github.com/snyk/go-application-framework/pkg/configuration"

"github.com/snyk/snyk-ls/application/config"
"github.com/snyk/snyk-ls/application/di"
"github.com/snyk/snyk-ls/domain/ide/workspace"
Expand Down
3 changes: 1 addition & 2 deletions application/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package server

import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/snyk/go-application-framework/pkg/configuration"

"github.com/snyk/snyk-ls/internal/types"

"github.com/snyk/snyk-ls/application/config"
Expand All @@ -59,7 +59,6 @@ var sampleSettings = types.Settings{
func keyFoundInEnv(key string) bool {
found := false
env := os.Environ()
fmt.Println(env)
for _, v := range env {
if strings.HasPrefix(v, key+"=") {
found = true
Expand Down
23 changes: 12 additions & 11 deletions application/server/execute_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package server

import (
"context"
"github.com/snyk/snyk-ls/domain/snyk"
"github.com/snyk/snyk-ls/domain/snyk/scanner"
"testing"
"time"

"github.com/snyk/snyk-ls/domain/snyk"
"github.com/snyk/snyk-ls/domain/snyk/scanner"

"github.com/sourcegraph/go-lsp"
"github.com/stretchr/testify/assert"

Expand All @@ -38,33 +39,33 @@ func Test_executeWorkspaceScanCommand_shouldStartWorkspaceScanOnCommandReceipt(t
loc, _ := setupServerWithCustomDI(t, false)
c := config.CurrentConfig()

scanner := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", scanner, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))
s := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", s, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))

params := lsp.ExecuteCommandParams{Command: types.WorkspaceScanCommand}
_, err := loc.Client.Call(ctx, "workspace/executeCommand", params)
if err != nil {
t.Fatal(err)
}
assert.Eventually(t, func() bool {
return scanner.Calls() > 0
return s.Calls() > 0
}, 2*time.Second, time.Millisecond)
}

func Test_executeWorkspaceFolderScanCommand_shouldStartFolderScanOnCommandReceipt(t *testing.T) {
loc, _ := setupServerWithCustomDI(t, false)
c := config.CurrentConfig()

scanner := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", scanner, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))
s := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", s, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))

params := lsp.ExecuteCommandParams{Command: types.WorkspaceFolderScanCommand, Arguments: []any{"dummy"}}
_, err := loc.Client.Call(ctx, "workspace/executeCommand", params)
if err != nil {
t.Fatal(err)
}
assert.Eventually(t, func() bool {
return scanner.Calls() > 0
return s.Calls() > 0
}, 2*time.Second, time.Millisecond)
}

Expand Down Expand Up @@ -105,8 +106,8 @@ func Test_executeWorkspaceScanCommand_shouldAskForTrust(t *testing.T) {
loc, jsonRPCRecorder := setupServerWithCustomDI(t, false)
c := config.CurrentConfig()

scanner := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", scanner, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))
s := &scanner.TestScanner{}
workspace.Get().AddFolder(workspace.NewFolder(c, "dummy", "dummy", s, di.HoverService(), di.ScanNotifier(), di.Notifier(), di.ScanPersister()))
// explicitly enable folder trust which is disabled by default in tests
config.CurrentConfig().SetTrustedFolderFeatureEnabled(true)

Expand All @@ -116,7 +117,7 @@ func Test_executeWorkspaceScanCommand_shouldAskForTrust(t *testing.T) {
t.Fatal(err)
}
assert.Eventually(t, func() bool {
return scanner.Calls() == 0 && checkTrustMessageRequest(jsonRPCRecorder)
return s.Calls() == 0 && checkTrustMessageRequest(jsonRPCRecorder)
}, 2*time.Second, time.Millisecond)
}

Expand Down
6 changes: 4 additions & 2 deletions application/server/notification/scan_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
package notification_test

import (
"testing"

"github.com/stretchr/testify/assert"

notification2 "github.com/snyk/snyk-ls/application/server/notification"
"github.com/snyk/snyk-ls/domain/snyk/scanner"
"github.com/snyk/snyk-ls/internal/notification"
"github.com/snyk/snyk-ls/internal/product"
"github.com/snyk/snyk-ls/internal/testutil"
"github.com/snyk/snyk-ls/internal/types"
"github.com/stretchr/testify/assert"
"testing"
)

type sendMessageTestCase struct {
Expand Down
9 changes: 0 additions & 9 deletions application/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package server

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -156,14 +155,6 @@ func startServer(callBackFn onCallbackFn, jsonRPCRecorder *testutil.JsonRPCRecor
return loc
}

func Test_serverShouldStart(t *testing.T) {
loc, _ := setupServer(t)

si := loc.Server.ServerInfo()

fmt.Println(strings.Join(si.Methods, "\n"))
}

func Test_dummy_shouldNotBeServed(t *testing.T) {
loc, _ := setupServer(t)

Expand Down
3 changes: 2 additions & 1 deletion ast/maven/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package maven

import (
"encoding/xml"
"errors"
"io"
"strings"

Expand Down Expand Up @@ -50,7 +51,7 @@ func (p *Parser) Parse(content string, path string) ast.Tree {
for {
token, err := d.Token()
offset = d.InputOffset()
if token == nil || err == io.EOF {
if token == nil || errors.Is(err, io.EOF) {
// EOF means we're done.
break
} else if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions domain/ide/codelens/codelens.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package codelens
import (
"fmt"

"github.com/rs/zerolog"
sglsp "github.com/sourcegraph/go-lsp"

"github.com/snyk/snyk-ls/application/config"
Expand All @@ -35,6 +36,8 @@ type lensesWithIssueCount struct {
}

func GetFor(filePath string) (lenses []sglsp.CodeLens) {
c := config.CurrentConfig()
logger := c.Logger().With().Str("method", "codelens.GetFor").Str("filePath", filePath).Logger()
f := workspace.Get().GetFolderContaining(filePath)
if f == nil {
return lenses
Expand All @@ -45,7 +48,7 @@ func GetFor(filePath string) (lenses []sglsp.CodeLens) {
// group by range first
lensesByRange := make(map[snyk.Range]*lensesWithIssueCount)
for _, issue := range issues {
if config.CurrentConfig().IsDeltaFindingsEnabled() && !issue.IsNew {
if c.IsDeltaFindingsEnabled() && !issue.IsNew {
continue
}
for _, lens := range issue.CodelensCommands {
Expand All @@ -64,7 +67,7 @@ func GetFor(filePath string) (lenses []sglsp.CodeLens) {
}

for r, commands := range lensesByRange {
lensCommands := getLensCommands(commands)
lensCommands := getLensCommands(commands, logger)
for _, command := range lensCommands {
lens := getCodeLensFromCommand(r, command)
lenses = append(lenses, lens)
Expand All @@ -74,7 +77,7 @@ func GetFor(filePath string) (lenses []sglsp.CodeLens) {
return lenses
}

func getLensCommands(lensesWithIssueCount *lensesWithIssueCount) []types.CommandData {
func getLensCommands(lensesWithIssueCount *lensesWithIssueCount, logger zerolog.Logger) []types.CommandData {
groupableByType := map[types.GroupingType][]types.Groupable{}
for _, groupable := range lensesWithIssueCount.lensCommands {
commands := groupableByType[groupable.GetGroupingType()]
Expand All @@ -90,7 +93,7 @@ func getLensCommands(lensesWithIssueCount *lensesWithIssueCount) []types.Command
if groupingType == types.Quickfix {
// right now we can always group by max semver version, as
// code only has one quickfix available, and iac none at all
qf, ok := types.MaxSemver()(lensCommands).(types.CommandData)
qf, ok := types.MaxSemver(logger)(lensCommands).(types.CommandData)
plural := ""
fixable := lensesWithIssueCount.issueCount
unfixable := lensesWithIssueCount.totalIssues - fixable
Expand Down
Loading

0 comments on commit 0c956c5

Please sign in to comment.