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

Option to allow the linting of generated files #2864

Open
0xjac opened this issue May 18, 2022 · 2 comments
Open

Option to allow the linting of generated files #2864

0xjac opened this issue May 18, 2022 · 2 comments
Labels
enhancement New feature or improvement

Comments

@0xjac
Copy link

0xjac commented May 18, 2022

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. Unexpected detection of file as autogenerated skipped all linters #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 0xjac added the enhancement New feature or improvement label May 18, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented May 18, 2022

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

@wiegell
Copy link

wiegell commented Jun 22, 2023

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' {} +

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

No branches or pull requests

2 participants