From 571711ffc3036d6893213fbe23d68f9d73b983ce Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 19 Sep 2024 14:35:57 +0100 Subject: [PATCH] Avoid running application initialization twice (#4335) --- .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