Skip to content

Add analyze_types config option for forbidigo #3802

Closed
@sbrownjc

Description

@sbrownjc

Welcome

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc.).
  • Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.). (https://golangci-lint.run/usage/linters/)

Description of the problem

In v1.4.0 forbidigo added the ability to analyze import types and match regardless of the aliased import name, however this requires a new config flag to be set to true: analyze_types.

forbidigo was updated to 1.4.0 in golangci-lint v1.51.2, but the required flag does not appear to be supported.

Can we add that flag?

Expected output:

main.go:8:2: use of `log.Warn` forbidden because "do not use logrus, instead use slog" (forbidigo)
        log.Warn("Hello")
main.go:8:11: use of `log.Entry.Info` forbidden because "do not use logrus, instead use slog" (forbidigo)
	}).Info("A walrus appears")

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version v1.52.2 built with go1.19.8 from (unknown, mod sum: "h1:FrPElUUI5rrHXg1mQ7KxI1MXPAw5lBVskiz7U7a8a1A=") on (unknown)

Configuration file

$ cat .golangci.yml
---
linters:
  disable-all: true
  enable:
    - forbidigo

linters-settings:
  forbidigo:
    analyze-types: true
    forbid:
      - '{p: "^logrus\\.(Debug|Error|Fatal|Info|Panic|Print|Println|Trace|Warn|Warning)f?$", msg: "do not use logrus, instead use slog"}'
      - '{p: "^\\*?logrus.Entry\\.(Debug|Error|Fatal|Info|Panic|Print|Println|Trace|Warn|Warning)f?$", msg: "do not use logrus, instead use slog"}'

Go environment

$ go version && go env
go version go1.19.8 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.8"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/go/src/github.com/<Company>/<repo>/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1372520807=/tmp/go-build -gno-record-gcc-switches"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /go/src/github.com/<Company>/<repo> /go/src/github.com/<Company> /go/src/github.com /go/src /go / /root] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 1 linters: [forbidigo]    
INFO [loader] Go packages loading at mode 575 (compiled_files|name|types_sizes|deps|exports_file|files|imports) took 20.89445497s 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 20.132798ms 
INFO [linters_context/goanalysis] analyzers took 18.042039ms with top 10 stages: forbidigo: 18.042039ms 
WARN [runner] Can't run linter forbidigo: forbidigo: failed to create linter "forbidigo": parsing as JSON or YAML failed: yaml: unmarshal errors:
  line 1: field pkg not found in type forbidigo.pattern 
INFO [runner] processing took 2.031µs with stages: max_same_issues: 400ns, skip_dirs: 268ns, nolint: 203ns, filename_unadjuster: 124ns, cgo: 118ns, autogenerated_exclude: 93ns, exclude-rules: 92ns, fixer: 92ns, skip_files: 91ns, identifier_marker: 90ns, path_prettifier: 89ns, path_prefixer: 84ns, max_from_linter: 58ns, sort_results: 31ns, diff: 31ns, path_shortener: 31ns, source_code: 30ns, exclude: 27ns, max_per_file_from_linter: 27ns, severity-rules: 26ns, uniq_by_line: 26ns 
INFO [runner] linters took 175.963204ms with stages: forbidigo: 175.941496ms 
ERRO Running error: 1 error occurred:
        * can't run linter forbidigo: forbidigo: failed to create linter "forbidigo": parsing as JSON or YAML failed: yaml: unmarshal errors:
  line 1: field pkg not found in type forbidigo.pattern
 
INFO Memory: 212 samples, avg is 33.8MB, max is 168.6MB 
INFO Execution took 21.096143903s

Code example or link to a public repository

package main

import (
	log "github.com/sirupsen/logrus"
)

func main() {
	log.Warn("Hello")
	log.WithFields(log.Fields{
		"animal": "walrus",
	}).Info("A walrus appears")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions