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

Avoid exception caused by accidental unloading of core rules #3857

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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