Skip to content

Commit

Permalink
Improve output with broken multiline playbooks
Browse files Browse the repository at this point in the history
Fixes: #4492
  • Loading branch information
ssbarnea committed Jan 30, 2025
1 parent d427f59 commit 042569e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:
- name: Install tox
run: |
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade "tox>=4.0.0" "tox-uv>=1.16.0" uv
python3 -m pip install --upgrade "tox>=4.0.0" "tox-uv>=1.20.2" uv
- name: Log installed dists
run: python3 -m pip freeze --all
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ def is_owned_by_ansible(self) -> bool:
def failed(self) -> bool:
"""Return true if we already found syntax-check errors on this file."""
return any(
match.rule.id in ("syntax-check", "load-failure") for match in self.matches
match.rule.id in ("syntax-check", "load-failure", "internal-error")
for match in self.matches
)

@property
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
yaml = [yaml]

for play in yaml:
# Bug #849
if play is None:
# Bug #849 and #4492
if play is None or not hasattr(play, "get"):
continue

if self.id in play.get(SKIPPED_RULES_KEY, ()):
Expand Down
18 changes: 13 additions & 5 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,12 @@ def worker(lintable: Lintable) -> list[MatchError]:
# do our processing only when ansible syntax check passed in order
# to avoid causing runtime exceptions. Our processing is not as
# resilient to be able process garbage.
matches.extend(self._emit_matches(files))
matches.extend(
self._emit_matches([file for file in files if not file.failed()])
)

# remove duplicates from files list
files = [value for n, value in enumerate(files) if value not in files[:n]]
files = list(dict.fromkeys(files))

for file in self.lintables:
if file in self.checked_files or not file.kind or file.failed():
Expand Down Expand Up @@ -433,6 +435,7 @@ def _get_ansible_syntax_check_matches(
f"Unexpected error code {run.returncode} from "
f"execution of: {' '.join(cmd)}"
)
filename.failed()
results.append(
MatchError(
message=message,
Expand Down Expand Up @@ -462,6 +465,8 @@ def _emit_matches(self, files: list[Lintable]) -> Generator[MatchError, None, No
while visited != self.lintables:
for lintable in self.lintables - visited:
visited.add(lintable)
if lintable.failed():
continue
if not lintable.path.exists():
continue
try:
Expand Down Expand Up @@ -499,9 +504,12 @@ def find_children(self, lintable: Lintable) -> list[Lintable]:
try:
playbook_ds = ansiblelint.utils.parse_yaml_from_file(str(lintable.path))
except AnsibleError as exc:
msg = f"Loading {lintable.filename} caused an {type(exc).__name__} exception: {exc}, file was ignored."
_logger.exception(msg)
return []
raise MatchError(
lintable=lintable, rule=self.rules["load-failure"]
) from exc
# msg = f"Loading {lintable.filename} caused an {type(exc).__name__} exception: {exc}, file was ignored."
# _logger.exception(msg)
# return []
results = []
# playbook_ds can be an AnsibleUnicode string, which we consider invalid
if isinstance(playbook_ds, str):
Expand Down
3 changes: 3 additions & 0 deletions src/ansiblelint/skip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,8 @@ def is_nested_task(task: dict[str, Any]) -> bool:
# Cannot really trust the input
if isinstance(task, str):
return False
# https://github.com/ansible/ansible-lint/issues/4492
if not hasattr(task, "get"):
return False

return any(task.get(key) for key in NESTED_TASK_KEYS)

0 comments on commit 042569e

Please sign in to comment.