Skip to content

Commit

Permalink
Avoid app reinitialization during syntax-check
Browse files Browse the repository at this point in the history
Fixes: #3560
  • Loading branch information
ssbarnea committed Jun 15, 2023
1 parent 9fc3a61 commit f734d28
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/ansiblelint/rules/syntax_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
from typing import Any

from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
from ansiblelint.app import get_app
from ansiblelint.config import options
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.logger import timed_info
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.text import strip_ansi_escape

from ansiblelint.app import App, get_app


@dataclass
class KnownError:
Expand Down Expand Up @@ -73,6 +74,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 +120,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 +207,9 @@ 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,9 @@ 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 +245,9 @@ 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

0 comments on commit f734d28

Please sign in to comment.