Skip to content

Commit

Permalink
Avoid running application initialization twice
Browse files Browse the repository at this point in the history
Related: #4219
  • Loading branch information
ssbarnea committed Sep 18, 2024
1 parent 8d9fe1d commit 20bab11
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20bab11

Please sign in to comment.