From 20bab11bd3931ba09d35df1f4bfb7bfab13a37d1 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 18 Sep 2024 16:54:41 +0100 Subject: [PATCH] Avoid running application initialization twice Related: https://github.com/ansible/ansible-lint/issues/4219 --- .pre-commit-config.yaml | 2 +- src/ansiblelint/__main__.py | 5 ++++- src/ansiblelint/app.py | 4 ++++ src/ansiblelint/utils.py | 2 +- test/test_transformer.py | 2 ++ 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c417f4e651..1d24f83c97 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -137,7 +137,7 @@ repos: types: [file, yaml] entry: yamllint --strict - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.3" + rev: "v0.6.5" hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/src/ansiblelint/__main__.py b/src/ansiblelint/__main__.py index ec8edf03a7..76b5fe9d65 100755 --- a/src/ansiblelint/__main__.py +++ b/src/ansiblelint/__main__.py @@ -344,7 +344,10 @@ def main(argv: list[str] | None = None) -> int: console.print(profiles_as_rich()) return 0 - app = get_app(offline=None) # to be sure we use the offline value from settings + app = get_app( + offline=None, + cached=True, + ) # to be sure we use the offline value from settings rules = RulesCollection( options.rulesdirs, profile_name=options.profile, diff --git a/src/ansiblelint/app.py b/src/ansiblelint/app.py index 3568f53823..7074103a39 100644 --- a/src/ansiblelint/app.py +++ b/src/ansiblelint/app.py @@ -407,6 +407,8 @@ def get_app(*, offline: bool | None = None, cached: bool = False) -> App: """Return the application instance, caching the return value.""" # Avoids ever running the app initialization twice if cached argument # is mentioned. + # pylint: disable=global-statement + global _CACHED_APP if cached: if offline is not None: msg = ( @@ -426,6 +428,8 @@ def get_app(*, offline: bool | None = None, cached: bool = False) -> App: options = default_options app = App(options=options) + if cached: + _CACHED_APP = app # Make linter use the cache dir from compat options.cache_dir = app.runtime.cache_dir diff --git a/src/ansiblelint/utils.py b/src/ansiblelint/utils.py index 19c6356291..289dc56099 100644 --- a/src/ansiblelint/utils.py +++ b/src/ansiblelint/utils.py @@ -290,7 +290,7 @@ class HandleChildren: rules: RulesCollection = field(init=True, repr=False) app: App - def include_children( # pylint: disable=too-many-return-statements + def include_children( self, lintable: Lintable, k: str, diff --git a/test/test_transformer.py b/test/test_transformer.py index 51e97d5f4a..2d73fe3d73 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -353,6 +353,8 @@ def report_outcome( monkeypatch.setattr("ansiblelint.__main__.fix", test_fix) monkeypatch.setattr("ansiblelint.app.App", TestApp) + # disable the App() caching because we cannot prevent the initial initialization from happening + monkeypatch.setattr("ansiblelint.app._CACHED_APP", None) main.main() assert fix_called