Skip to content

Commit

Permalink
Allow linter to capture and display initialization warnings (#4504)
Browse files Browse the repository at this point in the history
Co-authored-by: Abhinav Anand <abanand@redhat.com>
  • Loading branch information
ssbarnea and abhikdps authored Jan 30, 2025
1 parent be8cf91 commit 3be4baf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ test/schemas/node_modules
node_modules
.direnv
uv.lock
.ansible
9 changes: 7 additions & 2 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,16 @@ def fix(runtime_options: Options, result: LintResult, rules: RulesCollection) ->
# pylint: disable=too-many-locals,too-many-statements
def main(argv: list[str] | None = None) -> int:
"""Linter CLI entry point."""
must_exit = False
# alter PATH if needed (venv support)
path_inject(argv[0] if argv and argv[0] else "")

if argv is None: # pragma: no cover
argv = sys.argv

with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter(action="ignore")
# do not use "ignore" as we will miss to collect them
warnings.simplefilter(action="default")

cache_dir_lock = initialize_options(argv[1:])

Expand All @@ -297,7 +299,7 @@ def main(argv: list[str] | None = None) -> int:
if msg:
console.print(msg)
support_banner()
sys.exit(0)
must_exit = True
else:
support_banner()

Expand All @@ -310,6 +312,9 @@ def main(argv: list[str] | None = None) -> int:
for warn in warns:
_logger.warning(str(warn.message))
warnings.resetwarnings()

if must_exit:
sys.exit(0)
# checks if we have `ANSIBLE_LINT_SKIP_SCHEMA_UPDATE` set to bypass schema
# update. Also skip if in offline mode.
# env var set to skip schema refresh
Expand Down

0 comments on commit 3be4baf

Please sign in to comment.