Closed mrz1836 closed 6 months ago
I can give more examples of the nolint
not working. I have also adjusted spaces, comment formats and nothing seems to work.
Hello,
I don't reproduce your problem:
package main
import (
"bytes"
"os"
)
type ServerConn struct{}
func (c ServerConn) Stor(path string, buffer *bytes.Buffer) error {
return nil
}
// UploadFileToFTP uploads a file to an FTP server
func UploadFileToFTP(client *ServerConn, filePath, targetPath string) error {
contents, err := os.ReadFile(filePath) //nolint:gosec // This is a local file
if err != nil {
return err
}
return client.Stor(targetPath, bytes.NewBuffer(contents))
}
$ golangci-lint run
WARN [config_reader] The configuration option `run.skip-files` is deprecated, please use `issues.exclude-files`.
WARN [config_reader] The configuration option `run.skip-dirs` is deprecated, please use `issues.exclude-dirs`.
WARN [config_reader] The configuration option `output.format` is deprecated, please use `output.formats`
WARN [config_reader] The configuration option `govet.check-shadowing` is deprecated. Please enable `shadow` instead, if you are not using `enable-all`.
main.go:8:6: exported: exported type ServerConn should have comment or be unexported (revive)
type ServerConn struct{}
^
main.go:10:1: exported: exported method ServerConn.Stor should have comment or be unexported (revive)
func (c ServerConn) Stor(path string, buffer *bytes.Buffer) error {
^
main.go:1:1: package-comments: should have a package comment (revive)
package main
^
I found some strange issues with contextcheck
and after I disabled that it worked.
If I leave contextcheck
enabled I see these warnings:
WARN [runner] Can't process result by autogenerated_exclude processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `NewWriteLock$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0x140013fb080), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner] Can't process result by nolint processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `NewWriteLock$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0x140013fb080), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner/source_code] Failed to get line 0 for file : failed to get file lines cache: can't get file bytes from cache: can't read file : open : no such file or directory
Also my code base is private, and lots of code, so i'm sorry about the example but it's probably somewhere else that is affecting it
I disabled contextcheck
and everything runs well with that disabled.
Can you provide a reproducible example? (a link to a public repo can be useful)
Can you provide an extract of the code related to NewWriteLock$1
?
This is the function that it is referencing, but I cannot spot the issue.
package config
import (
"context"
"fmt"
"github.com/mrz1836/go-cachestore"
)
// NewWriteLock will take care of creating a lock and defer
func NewWriteLock(ctx context.Context, lockKey string, cacheStore cachestore.LockService, ttl int64) (func(), error) {
if ttl <= 0 {
ttl = DefaultCacheLockTTL
}
secret, err := cacheStore.WriteLock(ctx, lockKey, ttl)
return func() {
// context is not set, since the req could be canceled, but unlocking should never be stopped
_, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
}, err
}
Do you have a caller example of NewWriteLock
?
I'm able to reproduce the problem: it's a bug inside contextcheck
package config
import (
"context"
"github.com/mrz1836/go-cachestore"
)
const DefaultCacheLockTTL = 0
// NewWriteLock will take care of creating a lock and defer
func NewWriteLock(ctx context.Context, lockKey string, cacheStore cachestore.LockService, ttl int64) (func(), error) {
if ttl <= 0 {
ttl = DefaultCacheLockTTL
}
secret, err := cacheStore.WriteLock(ctx, lockKey, ttl)
return func() {
// context is not set, since the req could be canceled, but unlocking should never be stopped
_, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
}, err
}
func Foo() {
lock, err := NewWriteLock(context.Background(), "", nil, 12)
if err != nil {
return
}
lock()
}
$ golangci-lint run
WARN [config_reader] The configuration option `run.skip-files` is deprecated, please use `issues.exclude-files`.
WARN [config_reader] The configuration option `run.skip-dirs` is deprecated, please use `issues.exclude-dirs`.
WARN [config_reader] The configuration option `output.format` is deprecated, please use `output.formats`
WARN [config_reader] The configuration option `govet.check-shadowing` is deprecated. Please enable `shadow` instead, if you are not using `enable-all`.
WARN [runner] Can't process result by autogenerated_exclude processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `NewWriteLock$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0xc0012e6f00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner] Can't process result by nolint processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `NewWriteLock$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0xc0012e6f00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner/source_code] Failed to get line 0 for file : failed to get file lines cache: can't get file bytes from cache: can't read file : open : no such file or directory
main.go:1:1: package-comments: should have a package comment (revive)
package config
^
main.go:9:7: exported: exported const DefaultCacheLockTTL should have comment or be unexported (revive)
const DefaultCacheLockTTL = 0
^
main.go:23:1: exported: exported function Foo should have comment or be unexported (revive)
func Foo() {
^
:0: Function `NewWriteLock$1` should pass the context parameter (contextcheck)
I changed this line and the warning issue went away:
From: _, _ = cacheStore.ReleaseLock(context.Background(), lockKey, secret)
To: _, _ = cacheStore.ReleaseLock(ctx, lockKey, secret)
So I got the warning to go away, but it looks like the contextcheck
is choking on the warning, which then has other errors for some reason that affected the nolint
directives for some reason
Can you open an issue on https://github.com/kkHAIKE/contextcheck/issues?
I will create a fix inside golangci-lint about reports without filepath.
I have same issue after upgrade to version 1.57.0. No one //nolint:...
worked. And I have same error with contextcheck
at beginning. And all perfect works if disable contextcheck
.
Error:
WARN [runner] Can't process result by autogenerated_exclude processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `FullLogger$1$1$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0xc0013b8c00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner] Can't process result by nolint processor: can't filter issue result.Issue{FromLinter:"contextcheck", Text:"Function `FullLogger$1$1$1` should pass the context parameter", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0xc0013b8c00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"", Offset:0, Line:0, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: no file path for issue
WARN [runner/source_code] Failed to get line 0 for file : failed to get file lines cache: can't get file bytes from cache: can't read file : open : no such file or directory
:0: Function `FullLogger$1$1$1` should pass the context parameter (contextcheck)
FullLogger implementation
package middleware
import (...)
type dbWriter func(context.Context, *model.Log) error
func FullLogger(l logrus.FieldLogger, db dbWriter) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
...
go func(writer dbWriter, log *model.Log, reqID string) {...}(db, &model.Log{...}, requestID)
...
next.ServeHTTP(w, r)
})
}
}
Call FullLogger:
package main
import (
localMiddleware ".../middleware"
"github.com/go-chi/chi/v5"
)
func main() {
r := chi.NewRouter()
r.Use(localMiddleware.FullLogger(logger, srv.Log.Create))
}
Implementation of srv.Log.Create
:
func (srv *Service) Create(ctx context.Context, log *model.Log) error {
...
return srv.db.Create(ctx, log)
}
With version 1.56.2 all works fine.
I will provide a fix in a few minutes.
The fix: #4552
I also created a fix for contextcheck
: https://github.com/kkHAIKE/contextcheck/pull/22
I created a new bugfix release: https://github.com/golangci/golangci-lint/releases/tag/v1.57.1
Sponsoring is a good way to sustain open source maintainers: sponsor me
Welcome
Description of the problem
Upgraded my golangci-lint via brew to the latest version. I use this in 20+ projects and have used it for a long time. After upgrading and running, several nolint directives are not working.
Version of golangci-lint
Configuration
Go environment
Verbose output of running
A minimal reproducible example or link to a public repository
Validation