Skip to content

golangci-lint attempts to follow //line directives into non-Go source #1543

Closed
@kortschak

Description

@kortschak
  • 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).

Please include the following information:

Version of golangci-lint
$ golangci-lint --version
golangci-lint has version 1.33.0 built from b90551c on 2020-11-23T05:15:36Z
Config file
$ cat .golangci.yml
cat: .golangci.yml: No such file or directory
Go environment
$ go version && go env
go version go1.15.3 linux/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/user/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/src/gonum.org/v1/gonum/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build387136233=/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: [./ /home/user/src/gonum.org/v1/gonum/graph/formats/rdf /home/user/src/gonum.org/v1/gonum/graph/formats /home/user/src/gonum.org/v1/gonum/graph /home/user/src/gonum.org/v1/gonum /home/user/src/gonum.org/v1 /home/user/src/gonum.org /home/user/src /home/user /home /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (name|exports_file|deps|files|imports|types_sizes|compiled_files) took 90.370554ms 
INFO [runner/filename_unadjuster] Pre-built 3 adjustments in 11.818516ms 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
WARN [runner] Can't process result by autogenerated_exclude processor: can't filter issue result.Issue{FromLinter:"govet", Text:"unreachable: unreachable code", Severity:"", SourceLines:[]string(nil), Replacement:(*result.Replacement)(nil), Pkg:(*packages.Package)(0xc0009b0120), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"check_actions.rl", Offset:2426, Line:22, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}: failed to get doc of file check_actions.rl: failed to parse file: check_actions.rl:5:1: expected 'package', found '%' 
INFO [runner/max_same_issues] 1/4 issues with text "ineffectual assignment to `cs`" were hidden, use --max-same-issues 
INFO [runner] Issues before processing: 54, after processing: 9 
INFO [runner] Processors filtering stat (out/in): exclude: 54/54, max_same_issues: 9/10, path_shortener: 9/9, severity-rules: 9/9, path_prefixer: 9/9, cgo: 54/54, nolint: 17/48, diff: 10/10, max_per_file_from_linter: 10/10, filename_unadjuster: 54/54, path_prettifier: 54/54, source_code: 9/9, skip_files: 54/54, skip_dirs: 54/54, identifier_marker: 54/54, exclude-rules: 48/54, uniq_by_line: 10/17, max_from_linter: 9/9, sort_results: 9/9 
INFO [runner] processing took 14.709049ms with stages: nolint: 12.836318ms, identifier_marker: 632.914µs, exclude-rules: 611.798µs, autogenerated_exclude: 232.484µs, path_prettifier: 223.304µs, source_code: 100.123µs, max_same_issues: 24.777µs, cgo: 14.318µs, skip_dirs: 13.546µs, filename_unadjuster: 7.625µs, uniq_by_line: 4.045µs, max_from_linter: 3.21µs, path_shortener: 1.89µs, max_per_file_from_linter: 1.102µs, diff: 392ns, sort_results: 282ns, severity-rules: 253ns, exclude: 249ns, skip_files: 220ns, path_prefixer: 199ns 
INFO [runner] linters took 36.234253ms with stages: goanalysis_metalinter: 21.138866ms, unused: 287.998µs 
check.go:353: `checkLang_first_final` is unused (deadcode)
INFO File cache stats: 5 entries of total size 30.8KiB 
const checkLang_first_final int = 4
check.go:354: `checkLang_error` is unused (deadcode)
const checkLang_error int = 0
check.go:356: `checkLang_en_value` is unused (deadcode)
const checkLang_en_value int = 1
check.go:148: ineffectual assignment to `cs` (ineffassign)
		cs = 0
check.go:419: ineffectual assignment to `cs` (ineffassign)
		cs = 0
extract.go:161: ineffectual assignment to `cs` (ineffassign)
		cs = 0
check_actions.rl:22: unreachable: unreachable code (govet)
}%%
extract_actions.rl:84: unreachable: unreachable code (govet)
}%%
parse_actions.rl:85: unreachable: unreachable code (govet)
}%%
INFO Memory: 3 samples, avg is 72.0MB, max is 72.3MB 
INFO Execution took 143.671345ms                  

The generated code is being generated by ragel using ragel include statements and that appears to be causing golangci-lint some confusion. Although there are //nolint directives in the generation description, and so in the final generated code (to some good effect - without them 10 more linting errors are found), the linter follows the line directives into the ragel description, obviously failing to parse the ragel syntax.

In order to confirm this, I have manually removed all of the //line <file_name>.rl:<line_no> directives in the generated code, and then the linting completes successfully with no linting errors reported. This isn't really an option in production and breaks the utility of being able to debug the original generation description source.

golangci-lint should not follow lines into non-Go source files.

This is what the first several lines of one of the generated files looks like.


//line extract.rl:1
// Go code generated by go generate gonum.org/v1/gonum/graph/formats/rdf; DO NOT EDIT.

// Copyright ©2020 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//nolint
package rdf

As an aside, the inclusion of //nolint directives only works at all if they are attached to the package decl of the file, resulting in a "nolint" line being included in the godoc output. Is there a way to prevent linting of generated code (where I obviously can't place //nolint comments throughout the code) that doesn't litter the comments into the godoc?

For example:

$ go doc .
package rdf // import "gonum.org/v1/gonum/graph/formats/rdf"

nolint

Package rdf implements decoding the RDF 1.1 N-Quads line-based plain text
format for encoding an RDF dataset. N-Quad parsing is performed as defined
by http://www.w3.org/TR/n-quads/

nolint

nolint

var ErrInvalid = errors.New("invalid N-Quad") ...
type Decoder struct{ ... }
    func NewDecoder(r io.Reader) *Decoder
type Kind int
    const Invalid Kind = iota ...
type Statement struct{ ... }
    func ParseNQuad(statement string) (*Statement, error)
type Term struct{ ... }
    func NewBlankTerm(label string) (Term, error)
    func NewIRITerm(iri string) (Term, error)
    func NewLiteralTerm(text, qual string) (Term, error)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: cgoRelated to CGO or line directivesbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions