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 app reinitialization during syntax-check #3563

Merged
merged 2 commits into from
Jun 15, 2023
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
20 changes: 15 additions & 5 deletions src/ansiblelint/rules/syntax_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Any

from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
from ansiblelint.app import get_app
from ansiblelint.app import App, get_app
from ansiblelint.config import options
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
Expand Down Expand Up @@ -73,6 +73,7 @@ class AnsibleSyntaxCheckRule(AnsibleLintRule):
# pylint: disable=too-many-locals
def _get_ansible_syntax_check_matches(
lintable: Lintable,
app: App,
) -> list[MatchError]:
"""Run ansible syntax check and return a list of MatchError(s)."""
default_rule: BaseRule = AnsibleSyntaxCheckRule()
Expand Down Expand Up @@ -118,7 +119,7 @@ def _get_ansible_syntax_check_matches(
# To reduce noisy warnings like
# CryptographyDeprecationWarning: Blowfish has been deprecated
# https://github.com/paramiko/paramiko/issues/2038
env = get_app().runtime.environ.copy()
env = app.runtime.environ.copy()
env["PYTHONWARNINGS"] = "ignore"

run = subprocess.run(
Expand Down Expand Up @@ -205,7 +206,10 @@ def test_get_ansible_syntax_check_matches() -> None:
kind="playbook",
)
# pylint: disable=protected-access
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(lintable)
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(
lintable,
app=get_app(),
)
assert result[0].lineno == 4
assert result[0].column == 7
assert (
Expand All @@ -221,7 +225,10 @@ def test_empty_playbook() -> None:
"""Validate detection of empty-playbook."""
lintable = Lintable("examples/playbooks/empty_playbook.yml", kind="playbook")
# pylint: disable=protected-access
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(lintable)
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(
lintable,
app=get_app(),
)
assert result[0].lineno == 1
# We internally convert absolute paths returned by ansible into paths
# relative to current directory.
Expand All @@ -239,7 +246,10 @@ def test_extra_vars_passed_to_command(config_options: Any) -> None:
lintable = Lintable("examples/playbooks/extra_vars.yml", kind="playbook")

# pylint: disable=protected-access
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(lintable)
result = AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches(
lintable,
app=get_app(),
)

assert not result

Expand Down
6 changes: 5 additions & 1 deletion src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ansiblelint.skip_utils
import ansiblelint.utils
from ansiblelint._internal.rules import LoadingFailureRule, WarningRule
from ansiblelint.app import get_app
from ansiblelint.constants import States
from ansiblelint.errors import LintWarning, MatchError, WarnSource
from ansiblelint.file_utils import Lintable, expand_dirs_in_lintables
Expand Down Expand Up @@ -207,10 +208,13 @@ def _run(self) -> list[MatchError]:
)

# -- phase 1 : syntax check in parallel --
app = get_app(offline=None)

def worker(lintable: Lintable) -> list[MatchError]:
# pylint: disable=protected-access
return AnsibleSyntaxCheckRule._get_ansible_syntax_check_matches( # noqa: SLF001
lintable,
lintable=lintable,
app=app,
)

for lintable in self.lintables:
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ansible-lint-config": {
"etag": "b5caa5405047dad89bb9fb419a17d8a67750f3a7ecdbabe16e0eb1897d316c5a",
"etag": "0ec39ba1ca9c20aea463f7f536c6903c88288f47c1b2b2b3d53b527c293f8cc3",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible-lint-config.json"
},
"ansible-navigator-config": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/molecule.json"
},
"playbook": {
"etag": "9f6baaa61cc2bfaf32d00af12ef6dd0e8ca3dd3bb94581cabb66a92795d8f4e6",
"etag": "acbd5edfc66279f8c3f6f8a99d0874669a254983ace5e4a2cce6105489ab3e21",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/playbook.json"
},
"requirements": {
Expand Down