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 get_app more than once #4115

Merged
merged 1 commit into from
Apr 17, 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
14 changes: 13 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


_logger = logging.getLogger(__package__)
_CACHED_APP = None


class App:
Expand Down Expand Up @@ -386,8 +387,19 @@


@lru_cache
def get_app(*, offline: bool | None = None) -> App:
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.
if cached:
if offline is not None:
msg = (

Check warning on line 396 in src/ansiblelint/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/app.py#L396

Added line #L396 was not covered by tests
"get_app should never be called with other arguments when cached=True."
)
raise RuntimeError(msg)

Check warning on line 399 in src/ansiblelint/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/app.py#L399

Added line #L399 was not covered by tests
if cached and _CACHED_APP is not None:
return _CACHED_APP

Check warning on line 401 in src/ansiblelint/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/app.py#L401

Added line #L401 was not covered by tests

if offline is None:
offline = default_options.offline

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def __init__( # pylint: disable=too-many-arguments
else:
self.options = options
self.profile = []
self.app = app or get_app(offline=True)
self.app = app or get_app(cached=True)

if profile_name:
self.profile = PROFILES[profile_name]
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def uncook(value: str, *, implicit: bool = False) -> str:

except jinja2.exceptions.TemplateSyntaxError as exc:
return "", str(exc.message), "invalid"
# pylint: disable=c-extension-no-member
except (NotImplementedError, black.parsing.InvalidInput) as exc:
# black is not able to recognize all valid jinja2 templates, so we
# just ignore InvalidInput errors.
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _run(self) -> list[MatchError]:

# -- phase 1 : syntax check in parallel --
if not self.skip_ansible_syntax_check:
app = get_app(offline=True)
app = get_app(cached=True)

def worker(lintable: Lintable) -> list[MatchError]:
return self._get_ansible_syntax_check_matches(
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def _rolepath(basedir: str, role: str) -> str | None:
path_dwim(basedir, os.path.join("..", role)),
]

for loc in get_app(offline=True).runtime.config.default_roles_path:
for loc in get_app(cached=True).runtime.config.default_roles_path:
loc = os.path.expanduser(loc)
possible_paths.append(path_dwim(loc, role))

Expand Down
Loading