Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid running application initialization twice #4335

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading