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

Issues in gqlgen resolvers are ignored #4506

Closed
5 tasks done
bnavetta opened this issue Mar 14, 2024 · 6 comments · Fixed by #4507
Closed
5 tasks done

Issues in gqlgen resolvers are ignored #4506

bnavetta opened this issue Mar 14, 2024 · 6 comments · Fixed by #4507
Labels
area: processors enhancement New feature or improvement

Comments

@bnavetta
Copy link

bnavetta commented Mar 14, 2024

Welcome

Description of the problem

I'm using gqlgen to implement a GraphQL server. It autogenerates stubs for GraphQL endpoints, in *.resolver.go files. Even though the stubs are autogenerated, the files have non-autogenerated code.

golangci-lint run ignores issues in these files. Based on the verbose output, I think they're getting filtered out by the autogenerated_exclude processor.

Each resolver file has a comment like this:

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.27

I think that's triggering the rule here, even though the rest of the file isn't autogenerated.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.56.2 built with go1.22.0 from 58a724a on 2024-02-15T12:52:06Z

Configuration

run:
linters:
  enable:
    - gocritic
    - exhaustruct
linters-settings:
  exhaustruct:
    exclude:
      - '.+/graph\.Config$'
      - '.+/graphql\.FieldContext$'
      - '.+/graphql\.Response$'

Go environment

$ go version && go env
go version go1.22.1 darwin/arm64
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/ben/Library/Caches/go-build'
GOENV='/Users/ben/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/ben/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/ben/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.1/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.1/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/ben/src/gqlgen-linting/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/vx/hb7qrv412lnbfgmqn9rpwc200000gn/T/go-build1658647304=/tmp/go-build -gno-record-gcc-switches -fno-common'

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/ben/src/gqlgen-linting /Users/ben/src /Users/ben /Users /]
INFO [config_reader] Used config file .golangci.yaml
INFO [lintersdb] Active 8 linters: [errcheck exhaustruct gocritic gosimple govet ineffassign staticcheck unused]
INFO [loader] Go packages loading at mode 575 (compiled_files|exports_file|name|deps|files|imports|types_sizes) took 144.178708ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 2.938416ms
INFO [linters_context/goanalysis] analyzers took 3.379248363s with top 10 stages: buildir: 2.373889789s, inspect: 125.784248ms, fact_deprecated: 103.66313ms, fact_purity: 100.208455ms, ctrlflow: 96.994997ms, printf: 92.911998ms, nilness: 77.548118ms, SA5012: 77.15329ms, typedness: 68.571036ms, gocritic: 24.120708ms
INFO [runner] Issues before processing: 1, after processing: 0
INFO [runner] Processors filtering stat (out/in): cgo: 1/1, skip_files: 1/1, path_prettifier: 1/1, skip_dirs: 1/1, filename_unadjuster: 1/1, autogenerated_exclude: 0/1
INFO [runner] processing took 61.588µs with stages: autogenerated_exclude: 27.625µs, path_prettifier: 24.625µs, skip_dirs: 4.501µs, cgo: 625ns, nolint: 625ns, max_same_issues: 416ns, max_from_linter: 376ns, fixer: 375ns, severity-rules: 334ns, filename_unadjuster: 333ns, exclude-rules: 292ns, uniq_by_line: 292ns, skip_files: 209ns, exclude: 209ns, identifier_marker: 167ns, source_code: 167ns, path_shortener: 126ns, diff: 125ns, sort_results: 83ns, max_per_file_from_linter: 42ns, path_prefixer: 41ns
INFO [runner] linters took 1.274062792s with stages: goanalysis_metalinter: 1.273950375s
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 16 samples, avg is 212.5MB, max is 366.9MB
INFO Execution took 1.427133584s
Running `exhaustruct` directly:
$ exhaustruct -e '.+/graph\.Config$' -e '.+/graphql\.FieldContext$' -e '.+/graphql\.Response$' ./...
/Users/ben/src/gqlgen-linting/graph/schema.resolvers.go:21:11: model.Todo is missing fields ID, Done, User

A minimal reproducible example or link to a public repository

See https://github.com/bnavetta/gqlgen-linting

The code that should be reported is here: https://github.com/bnavetta/gqlgen-linting/blob/2f18e1ecca7cffa1030a12aa10d5af4e1ce49de4/graph/schema.resolvers.go#L21-L24

Validation

  • Yes, I've included all information above (version, config, etc.).
@bnavetta bnavetta added the bug Something isn't working label Mar 14, 2024
Copy link

boring-cyborg bot commented Mar 14, 2024

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

@ldez
Copy link
Member

ldez commented Mar 14, 2024

Hello,

the comment says that the file is generated, I understand that is partially but the comment applies to the file.

@bnavetta
Copy link
Author

Yeah, sorry - this is maybe more of a feature request than a bug. I'm looking for a way to make sure these *.resolver.go files are linted, despite being partially autogenerated.

Would you be open to a PR that adds a configuration flag to skip autogenerated_exclude, or one that special-cases gqlgen *.resolver.go files?

@ldez ldez added enhancement New feature or improvement and removed bug Something isn't working labels Mar 14, 2024
@ldez
Copy link
Member

ldez commented Mar 14, 2024

Take a look at #4507

@alexandear
Copy link
Member

alexandear commented Mar 14, 2024

@bnavetta While the golangci-lint team is working on a proper fix, I suggest you try the following solution.

A potential workaround is to set omit_gqlgen_file_notice: true in the gqlgen.yml. After this, re-generate your Go files using go run github.com/99designs/gqlgen generate. Now, golangci-lint should display the issue from exhaustruct:

❯ golangci-lint run
graph/schema.resolvers.go:17:11: model.Todo is missing fields ID, Done, User (exhaustruct)
        todo := &model.Todo{
                 ^

You can read about the omit_gqlgen_file_notice option at https://gqlgen.com/config/ or the source code.

@bnavetta
Copy link
Author

Take a look at #4507

This looks great, thanks!

A potential workaround is to set omit_gqlgen_file_notice: true in the gqlgen.yml. After this, re-generate your Go files using go run github.com/99designs/gqlgen generate. Now, golangci-lint should display the issue from exhaustruct:

Ah, thank you - didn't know that was an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: processors enhancement New feature or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants