golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

Option to allow the linting of generated files

Open 0xjac opened this issue 3 years ago • 2 comments

Your feature request related to a problem? Please describe.

I have some Go files generated from a template I control. Some mistakes in the template can result in sub-optimal Go code being generated. For example a struct can be packed poorly and fieldalignment (from govet) would catch it.

Many linters in the bugs category could be useful on generated files which depends on changes made by contributors within the project.

Describe the solution you'd like.

A configuration options to include or exclude all or some generated files as part of the set of files being linted.

Something like:

run:
    # Include generated files or not.
    # If disabled, specific generated files can still be skipped by adding them 
    # to skip-dirs or skip-files.
    # Default: false
    generated: true

The run options skip-dirs-use-default, skip-dirs, skip-files would still be applied such that after including generated files, some specific generated files (for example those generated from another tool) can still be explicitly ignored.

Most tools which generate Go files will either put them in a specific directory or will add a prefix in the name. E.g. stringer will add a _string prefix so running stringer on myfile.go will produce myfile_string.go which is easy to ignore with skip-files by adding `".+_string.go".

The issues options exclude, exclude-rules and include should also be applied such that some rules can be disabled for generated files that are linted.

TL;DR this would be similar to the run option tests but for generated files instead of test files.

Describe alternatives you've considered.

  1. Removing the "generated" comment from my generated files but that doesn't work as other tools rely on those comments to identify generated files.
  2. Make a script to run all the linters I want on the generated files. But this require the installation of multiple individual linters and having to re-configure them. It is essentially a poor-man's implementation of golangci-lint, so what's the point...

Additional context.

  1. The new generated option should default to false to ignore generated files and preserve the current default behavior.
  2. Some people are also having issues with false positive when detecting generated files (c.f. #2254).
  3. Some linters also have their own options which can be toggled to handle generated files—like exhaustive and gosec—and make the overall golangci-lint configuration easier when generated files are included. Those options are currently ignored!

0xjac avatar May 18 '22 11:05 0xjac

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

boring-cyborg[bot] avatar May 18 '22 11:05 boring-cyborg[bot]

Workaround script that can be run after code generation (careful, sed arguments are platform specific, this one is for macOS):

# files with the "Code generated" comment up top are not linted by golangci-lint.

generated_folder=$1

find "$generated_folder" -type f -name '*.go' -exec sed -i '' -e 's/Code generated/C!ode generated/g' {} +

wiegell avatar Jun 22 '23 20:06 wiegell