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

output of wsl and golangci-lint are different #142

Closed
tliefheid opened this issue Jan 19, 2024 · 2 comments
Closed

output of wsl and golangci-lint are different #142

tliefheid opened this issue Jan 19, 2024 · 2 comments

Comments

@tliefheid
Copy link

when using below example something funny is happening.

when running wsl -v ./... i get the following output:

/main.go:16:2: if statements should only be cuddled with assignments
/main.go:19:2: assignments should only be cuddled with other assignments
/main.go:33:3: if statements should only be cuddled with assignments
/main.go:36:3: expressions should not be cuddled with blocks

which is exactly what i expected,
but then when i run golangci-lint run -v ./... i get the following findings:

INFO [config_reader] Config search paths: [redacted] 
INFO [config_reader] Used config file .golangci.yaml 
INFO [lintersdb] Active 23 linters: [asciicheck bodyclose dogsled errcheck errname exhaustive forcetypeassert funlen gofmt gosec gosimple govet ineffassign loggercheck nestif perfsprint prealloc staticcheck unconvert unused whitespace wsl zerologlint] 
INFO [loader] Go packages loading at mode 575 (exports_file|types_sizes|compiled_files|deps|files|imports|name) took 848.350583ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 2.223833ms 
INFO [linters_context/goanalysis] analyzers took 0s with no stages 
INFO [runner] fixer took 0s with no stages        
INFO [runner] Processors filtering stat (out/in): identifier_marker: 3/3, exclude: 3/3, max_same_issues: 3/3, path_shortener: 3/3, fixer: 3/3, filename_unadjuster: 3/3, path_prettifier: 3/3, exclude-rules: 3/3, uniq_by_line: 3/3, diff: 3/3, source_code: 3/3, severity-rules: 3/3, cgo: 3/3, autogenerated_exclude: 3/3, max_per_file_from_linter: 3/3, path_prefixer: 3/3, sort_results: 3/3, skip_files: 3/3, skip_dirs: 3/3, nolint: 3/3, max_from_linter: 3/3 
INFO [runner] processing took 588.421µs with stages: path_prettifier: 288.583µs, autogenerated_exclude: 121.875µs, nolint: 73.501µs, identifier_marker: 46.875µs, source_code: 20.208µs, fixer: 10.584µs, exclude-rules: 8.458µs, skip_dirs: 6µs, exclude: 3.667µs, severity-rules: 2.917µs, uniq_by_line: 1.5µs, cgo: 1.042µs, max_same_issues: 959ns, path_shortener: 750ns, filename_unadjuster: 417ns, skip_files: 375ns, sort_results: 209ns, max_per_file_from_linter: 208ns, diff: 125ns, path_prefixer: 84ns, max_from_linter: 84ns 
INFO [runner] linters took 165.552292ms with stages: goanalysis_metalinter: 164.9085ms 
wsl/bug/main.go:24:21: Error return value of `http.ListenAndServe` is not checked (errcheck)
        http.ListenAndServe("127.0.0.1:8080", r)
                           ^
wsl/bug/main.go:16:2: if statements should only be cuddled with assignments (wsl)
        if x > 3 {
        ^
wsl/bug/main.go:19:2: assignments should only be cuddled with other assignments (wsl)
        r := chi.NewRouter()
        ^
INFO File cache stats: 1 entries of total size 807B 
INFO Memory: 12 samples, avg is 25.4MB, max is 35.7MB 
INFO Execution took 1.035146625s        

which only finds the first 2 that wsl itself is also finding. I originally thought that wsl ignored the anonymous handler func and then i did a little more digging and found it actually found it, but the golangci-linter didn't. I wonder why this is, maybe i did something wrong, but i would really like the get all the findings highlighted in vscode

package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi"
	"github.com/rs/zerolog/log"
)

func main() {
	x := 2
	log.Info().Int("x", x).Msg("print x")
	x = 3
	log.Info().Int("x", x).Msg("print x")
	if x > 3 {
		log.Info().Int("x", x).Msg("print x")
	}
	r := chi.NewRouter()
	r.Route("/", func(r chi.Router) {
		r.Use(MyHandler)
		r.Get("/", func(w http.ResponseWriter, r *http.Request) {})
	})
	http.ListenAndServe("127.0.0.1:8080", r)
}

func MyHandler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		x := 2
		log.Info().Int("x", x).Msg("print x")
		x = 3
		log.Info().Int("x", x).Msg("print x")
		if x > 3 {
			log.Info().Int("x", x).Msg("print x")
		}
		fmt.Printf("r.URL.String(): %v\n", r.URL.String())
		next.ServeHTTP(w, r)
	})
}

image

@bombsimon
Copy link
Owner

Hi! Thanks for the report!

This is because golangci-lint is running an old version of wsl (v3.4.0) but this was fixed since. It's actually been bumped and merged in golangci/golangci-lint#4215 but no new release of golangci-lint has happened. This will be fixed in the next release.

If you build golangci-lint from master this will work if you want a solution before a new release.

› go build -o ~/tmp/golangci-lint ./cmd/golangci-lint

› ~/tmp/golangci-lint run --no-config --disable-all --enable wsl
main.go:36:3: expressions should not be cuddled with blocks (wsl)
                fmt.Printf("r.URL.String(): %v\n", r.URL.String())
                ^
main.go:16:2: if statements should only be cuddled with assignments (wsl)
        if x > 3 {
        ^
main.go:19:2: assignments should only be cuddled with other assignments (wsl)
        r := chi.NewRouter()
        ^
main.go:33:3: if statements should only be cuddled with assignments (wsl)
                if x > 3 {
                ^

@tliefheid
Copy link
Author

great stuff @bombsimon, thanks for the awesome linter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants