Skip to content

Commit 8340266

Browse files
authored
Avoid reporting matcherrors against cwd (#1440)
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.
1 parent f985799 commit 8340266

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

docs/usage.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ the following:
100100
If playbooks include other playbooks, or tasks, or handlers or roles, these
101101
are also handled:
102102

103-
.. command-output:: ansible-lint -p examples/playbooks/include.yml
103+
.. command-output:: ansible-lint --offline -p examples/playbooks/include.yml
104104
:cwd: ..
105105
:returncode: 2
106106
:nostderr:

src/ansiblelint/errors.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Exceptions and error representations."""
22
import functools
3-
import os
43
from typing import Any, Optional, Union
54

65
from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
@@ -47,13 +46,12 @@ def __init__(
4746
self.linenumber = linenumber
4847
self.column = column
4948
self.details = details
49+
self.filename = ""
5050
if filename:
5151
if isinstance(filename, Lintable):
5252
self.filename = normpath(str(filename.path))
5353
else:
5454
self.filename = normpath(filename)
55-
else:
56-
self.filename = os.getcwd()
5755
self.rule = rule
5856
self.ignored = False # If set it will be displayed but not counted as failure
5957
# This can be used by rules that can report multiple errors type, so

src/ansiblelint/formatters/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _format_path(self, path: Union[str, Path]) -> str:
4343
if isinstance(path, Path):
4444
path = str(path) # Drop when Python 3.5 is no longer supported
4545

46-
if not self._base_dir:
46+
if not self._base_dir or not path:
4747
return path
4848
# Use os.path.relpath 'cause Path.relative_to() misbehaves
4949
return os.path.relpath(path, start=self._base_dir)

src/ansiblelint/rules/MetaMainHasInfoRule.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,4 @@ def matchplay(self, file: Lintable, data: "odict[str, Any]") -> List[MatchError]
7979
self.create_matcherror(message=err, filename=file)
8080
for err in _galaxy_info_errors_itr(galaxy_info)
8181
]
82-
83-
return [self.create_matcherror(message="No 'galaxy_info' found")]
82+
return [self.create_matcherror(message="No 'galaxy_info' found", filename=file)]

src/ansiblelint/runner.py

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def is_excluded(self, file_path: str) -> bool:
8484

8585
# Exclusions should be evaluated only using absolute paths in order
8686
# to work correctly.
87+
if not file_path:
88+
return False
89+
8790
abs_path = os.path.abspath(file_path)
8891
_file_path = Path(file_path)
8992

@@ -165,6 +168,8 @@ def _emit_matches(self, files: List[Lintable]) -> Generator[MatchError, None, No
165168
self.lintables.add(child)
166169
files.append(child)
167170
except MatchError as e:
171+
if not e.filename:
172+
e.filename = str(lintable.path)
168173
e.rule = LoadingFailureRule()
169174
yield e
170175
except AttributeError:

0 commit comments

Comments
 (0)