Use Foodcritic to check cookbooks for common problems:
- Best practices
- Common mistakes
Foodcritic looks for lint-like behavior and reports it!
Foodcritic is a static linting tool that analyzes the Ruby code that is authored in a cookbook against a number of rules, and then returns a list of violations. Because Foodcritic is a static linting tool, using it is fast. The code in a cookbook is read, broken down, and then compared to Foodcritic rules. The code is not run (a Chef Infra Client run does not occur). Foodcritic does not validate the intention of a recipe, rather it evaluates the structure of the code, and helps enforce specific behavior, detect portability of recipes, identify potential run-time failures, and spot common anti-patterns.
When Foodcritic returns a violation, this does not automatically mean
the code needs to be changed. It is important to first understand the
intention of the rule before making the changes it suggests. For
FC003 describes a scenario where a recipe uses the
search method in the Chef Infra Language to retrieve data from the Chef Infra
FC003 may suggest that a cookbook will raise an error if
that cookbook is run in a situation where a Chef Infra Server is not
present. Adopting this rule is only necessary when chef-solo is part of
the team’s workflow (because chef-solo does not use a Chef Infra
Foodcritic is run from the command line, typically against a single cookbook and the Ruby files contained within it:
Foodcritic may also be run from the root of an individual cookbook directory:
Foodcritic returns a list, via standard output, that shows the results of the evaluation:
FC003: Check whether you are running with chef server before using server-specific features: ./recipes/ip-logger.rb:1 FC008: Generated cookbook metadata needs updating: ./metadata.rb:2 FC008: Generated cookbook metadata needs updating: ./metadata.rb:3
- States a Foodcritic rule. For example:
- Describes the rule, based on the results of the evaluation. For example:
Check whether you are running with chef server before using server-specific features
- Specifies the file path. For example:
- Specifies the line number. For example:
A Foodcritic evaluation has the following syntax:
RULENUMBER: MESSAGE: FILEPATH:LINENUMBER
FC008: Generated cookbook metadata needs updating: ./metadata.rb:3
A complete list of Foodcritic rules are available on the Foodcritic website.
The following rules for Foodcritic have been developed by the Chef community:
Run the following command to exclude a Foodcritic rule:
foodcritic . --tags ~RULE
For example, to exclude rule
foodcritic . --tags ~FC003
foodcritic command is used to run Foodcritic against one (or more)
This command has the following syntax:
This command has the following options:
Use to specify tags to include or exclude when running Foodcritic.
List the name and description of all rules.
Use to trigger a build failure if any of the specified tags are matched.
Use to specify the chef version to evaluate against instead of Foodcritic’s default.
Specify file with rules to be used or ignored.
Use to specify the path to a cookbook to check
Use to show lines matched against Foodcritic rules, rather than the default summary.
Environment path(s) to check.
Search rubygems for rule files with the path
Use to specify the path to a file that contains additional Foodcritic rules.
Show progress of files being checked.
Role path(s) to check.
Use to specify the path to a file that contains additional grammar used when validating search syntax.
Use to display the version of Foodcritic.
Exclude path(s) from being linted. PATH is relative to the cookbook, not an absolute PATH. Default
For more information about Foodcritic: