Skip to content

Commit

Permalink
Avoid exception caused by accidental unloading of core rules (#3857)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Oct 19, 2023
1 parent 899f44c commit b4d4810
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ unindented
uninstallation
unjinja
unlex
unloadable
unnormalized
unskippable
unspaced
Expand Down
6 changes: 6 additions & 0 deletions src/ansiblelint/_internal/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class BaseRule:
link: str = ""
has_dynamic_tags: bool = False
needs_raw_task: bool = False
# Used to mark rules that we will never unload (internal ones)
unloadable: bool = False
# We use _order to sort rules and to ensure that some run before others,
# _order 0 for internal rules
# _order 1 for rules that check that data can be loaded
Expand Down Expand Up @@ -189,6 +191,7 @@ class RuntimeErrorRule(BaseRule):
tags = ["core"]
version_added = "v5.0.0"
_order = 0
unloadable = True


class AnsibleParserErrorRule(BaseRule):
Expand All @@ -200,6 +203,7 @@ class AnsibleParserErrorRule(BaseRule):
tags = ["core"]
version_added = "v5.0.0"
_order = 0
unloadable = True


class LoadingFailureRule(BaseRule):
Expand All @@ -215,6 +219,7 @@ class LoadingFailureRule(BaseRule):
_ids = {
"load-failure[not-found]": "File not found",
}
unloadable = True


class WarningRule(BaseRule):
Expand All @@ -226,3 +231,4 @@ class WarningRule(BaseRule):
tags = ["core", "experimental"]
version_added = "v6.8.0"
_order = 0
unloadable = True
2 changes: 2 additions & 0 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ def filter_rules_with_profile(rule_col: list[BaseRule], profile: str) -> None:
included.add(rule)
extends = PROFILES[extends].get("extends", None)
for rule in rule_col.copy():
if rule.unloadable:
continue
if rule.id not in included:
_logger.debug(
"Unloading %s rule due to not being part of %s profile.",
Expand Down
2 changes: 1 addition & 1 deletion test/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_profile_min() -> None:

filter_rules_with_profile(collection.rules, "min")
assert (
len(collection.rules) == 3
len(collection.rules) == 4
), "Failed to unload rule that is not part of 'min' profile."


Expand Down

0 comments on commit b4d4810

Please sign in to comment.