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

Don't lint generated code #165

Closed
alan-knight opened this issue Jan 7, 2016 · 30 comments
Closed

Don't lint generated code #165

alan-knight opened this issue Jan 7, 2016 · 30 comments
Labels
type-enhancement A request for a change that isn't a bug

Comments

@alan-knight
Copy link

I see too many issues with the linter complaining about Intl generated code. The people seeing the problems can't really do anything about it, and in some cases it's a lot of work just to satisfy the linter. For example, it complains about the library names in generated message files, which it wants to follow a convention including their whole directory structure. Since the generator doesn't even know the directory structure this is painful. Fortunately I think I can just remove it entirely, but in general I'd rather these files could be skipped.

@bwilkerson
Copy link
Member

Is there a way for the linter to know that these are generated files? It seems like it would be best if we didn't do any unnecessary analysis of generated files (including generating errors and warnings).

@alan-knight
Copy link
Author

Probably not in this case, as it long predates source_gen and such. Is there a way I could tell the linter they're generated. There is a big "DO NOT EDIT. This is generated", etc. at the top. I'd be happy to add something more machine-intelligible.

@zoechi
Copy link

zoechi commented Jan 7, 2016

.analysis_options allows to exclude files #139

analyzer:
  exclude: 
    - '**/*_generated.dart'

No idea of the for of the patter is correct.

@bwilkerson
Copy link
Member

The .analysis_options file is probably the best way. I'd like to avoid having to open and read files in order to determine whether we need to analyze them. Does the analysis options file solve the problem?

@alan-knight
Copy link
Author

The issue with the .analysis_options is that each user has to do it. As the author of a library that generates code I can't do anything except put it in a README that no one reads.

@bwilkerson
Copy link
Member

Yep. But the problem with using something embedded in the file is that it hurt the analyzer's performance. I don't know that there's a good solution. At least if you could generate all of these files into the same directory that every other generator uses then users would only have to add one "exclude" entry (or we could make it part of the package structure convention).

@zoechi
Copy link

zoechi commented Jan 7, 2016

I wouldn't mind configuring that in .analysis_options

@alan-knight
Copy link
Author

Presumably opening a file and then knowing that you can ignore the rest of its contents is actually a performance improvement over reading and analyzing the whole file.

@bwilkerson
Copy link
Member

But not over not reading the file.

@alan-knight
Copy link
Author

But imposing the burden on the user of maintaining a bunch of entries there.

Does analysis options include excluding files for some purposes but not others? In this case users probably don't want to be told about style issues in code they don't control. But they also don't want calls into the generated code to be shown as errors, which I presume is what would happen if the analyzer just ignored those files completely.

@donny-dont
Copy link

Having done codegen I personally think the library should generate code that passes everything but the flutter rules. Those are just a bit too extreme for my liking.

That being said I would be okay if there was a file naming convention for generated files that implied they were generated like foo.g.dart.

@zoechi
Copy link

zoechi commented Jan 8, 2016

I also don't know what effect excluding in .analysis_options has. Does it just not report warning or does in skip analysing them entirely. I would still want auto-completion.

@bwilkerson
Copy link
Member

... a file naming convention ...

I would also be fine with a file naming convention.

I also don't know what effect excluding in .analysis_options has.

It effectively says "treat this file as if it were not in this directory at all", which means that it will only be analyzed if it is referenced by some other file that is being analyzed, and only to the extent that is necessary to fully analyze the files that are not excluded. It won't generate error or warnings, and it might not do other pieces of analysis that are not required (such as evaluating constant expressions).

@zoechi
Copy link

zoechi commented Jan 8, 2016

I t effectively says "treat this file as ...

sgtm

@donny-dont
Copy link

@kevmoo source_gen uses a .g.dart right?

@kevmoo
Copy link
Member

kevmoo commented Jan 8, 2016

@donny-dont Yes, but it's not an approved convention.

I'd rather we standardize on a code comment at the top of a file to flag this

@donny-dont
Copy link

Code comment would also be okay with me. Thanks @kevmoo I figured you should probably be around for this conversation.

@bwilkerson
Copy link
Member

@kevmoo

I'd rather we standardize on a code comment at the top of a file to flag this.

Why? It's much faster to look at the name of the file than it is to read the content of the file.

@zoechi
Copy link

zoechi commented Jan 8, 2016

I also think a file name conversation would be better. It works with all tools not only with the analyzer.

@TobiasBales
Copy link

Did anything happen after this point?

@zoechi
Copy link

zoechi commented Jan 21, 2018

The build package added support for adding headers to generated code
That allows to add // ignore_for_file: linter_rule to generated files
That solves it for me

@TobiasBales
Copy link

That sounds great, do you have any pointers on how to add that?
Because googling quickly I didn't find anything

@zoechi
Copy link

zoechi commented Jan 22, 2018

@zoechi
Copy link

zoechi commented Aug 3, 2018

Exclude doesn't work for me
See also #25551, #29430

@westracer
Copy link

Can we get a solution for this, please? It's been 4 and a half years (!). The "exclude" option still doesn't work. My linter settings are pretty strict. I'm getting errors (not even warnings) because of someone else's code generator problems and I can't ignore them. This is very frustrating.

image

Getting this to work would be nice:

  exclude:
    - "**/*.g.dart"

@zoechi
Copy link

zoechi commented Sep 21, 2020

Try "dart analyze" instead of "dartanalyzer". Worked for me for the same issue (in beta channel, not sure this is available in stable already)

@pq pq added the type-enhancement A request for a change that isn't a bug label Dec 23, 2020
@5Gears0Chill
Copy link

This worked for me

in anaylsis_options.yaml

analyzer:
  exclude:
    - '**/*.g.dart'

full analysis options looks like this: (btw dont judge me for how i placed all the rules)

# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

analyzer:
  exclude:
    - '**/*.g.dart'

linter:
  # The lint rules applied to this project can be customized in the
  # section below to disable rules from the `package:flutter_lints/flutter.yaml`
  # included above or to enable additional rules. A list of all available lints
  # and their documentation is published at
  # https://dart-lang.github.io/linter/lints/index.html.
  #
  # Instead of disabling a lint rule for the entire project in the
  # section below, it can also be suppressed for a single line of code
  # or a specific dart file by using the `// ignore: name_of_lint` and
  # `// ignore_for_file: name_of_lint` syntax on the line or in the file
  # producing the lint.
  rules:
    # Style rules 
    camel_case_types: true
    library_names : true
    avoid_catches_without_on_clauses: false
    avoid_catching_errors: true
    avoid_empty_else: true
    unnecessary_brace_in_string_interps: true
    avoid_redundant_argument_values: true
    leading_newlines_in_multiline_strings: true
    directives_ordering: true
    # Formatting
    prefer_single_quotes: true
    lines_longer_than_80_chars: true
    curly_braces_in_flow_control_structures: true
    avoid_print: true
    always_specify_types: false
    sort_constructors_first: true
    always_use_package_imports: true
    avoid_dynamic_calls: true
    # Doc comments
    slash_for_doc_comments: true
    require_trailing_commas: true

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

@srawlins
Copy link
Member

srawlins commented Jun 8, 2022

The authors of code generators can now suppress all lint in a generated file with // ignore_for_file: type=lint: https://dart.dev/guides/language/analysis-options#suppressing-rules-for-a-file

@srawlins
Copy link
Member

I'll go ahead and close this issue with that solution.

@Nikzed
Copy link

Nikzed commented Sep 27, 2023

.analysis_options allows to exclude files #139

analyzer:
  exclude: 
    - '**/*_generated.dart'

No idea of the for of the patter is correct.

lovely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests