Skip to content

Commit

Permalink
Avoid reporting matcherrors against cwd (#1440)
Browse files Browse the repository at this point in the history
Removes the default use of cwd  for filename for matcherrors raised
during parsing. In some particular cases the errors would endup
without a valid filename, making hard to find the problem.
  • Loading branch information
ssbarnea authored Mar 7, 2021
1 parent f985799 commit 8340266
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ the following:
If playbooks include other playbooks, or tasks, or handlers or roles, these
are also handled:

.. command-output:: ansible-lint -p examples/playbooks/include.yml
.. command-output:: ansible-lint --offline -p examples/playbooks/include.yml
:cwd: ..
:returncode: 2
:nostderr:
Expand Down
4 changes: 1 addition & 3 deletions src/ansiblelint/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Exceptions and error representations."""
import functools
import os
from typing import Any, Optional, Union

from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
Expand Down Expand Up @@ -47,13 +46,12 @@ def __init__(
self.linenumber = linenumber
self.column = column
self.details = details
self.filename = ""
if filename:
if isinstance(filename, Lintable):
self.filename = normpath(str(filename.path))
else:
self.filename = normpath(filename)
else:
self.filename = os.getcwd()
self.rule = rule
self.ignored = False # If set it will be displayed but not counted as failure
# This can be used by rules that can report multiple errors type, so
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _format_path(self, path: Union[str, Path]) -> str:
if isinstance(path, Path):
path = str(path) # Drop when Python 3.5 is no longer supported

if not self._base_dir:
if not self._base_dir or not path:
return path
# Use os.path.relpath 'cause Path.relative_to() misbehaves
return os.path.relpath(path, start=self._base_dir)
Expand Down
3 changes: 1 addition & 2 deletions src/ansiblelint/rules/MetaMainHasInfoRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,4 @@ def matchplay(self, file: Lintable, data: "odict[str, Any]") -> List[MatchError]
self.create_matcherror(message=err, filename=file)
for err in _galaxy_info_errors_itr(galaxy_info)
]

return [self.create_matcherror(message="No 'galaxy_info' found")]
return [self.create_matcherror(message="No 'galaxy_info' found", filename=file)]
5 changes: 5 additions & 0 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def is_excluded(self, file_path: str) -> bool:

# Exclusions should be evaluated only using absolute paths in order
# to work correctly.
if not file_path:
return False

abs_path = os.path.abspath(file_path)
_file_path = Path(file_path)

Expand Down Expand Up @@ -165,6 +168,8 @@ def _emit_matches(self, files: List[Lintable]) -> Generator[MatchError, None, No
self.lintables.add(child)
files.append(child)
except MatchError as e:
if not e.filename:
e.filename = str(lintable.path)
e.rule = LoadingFailureRule()
yield e
except AttributeError:
Expand Down

0 comments on commit 8340266

Please sign in to comment.