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

Hide stacktrace when loading invalid yaml #3844

Merged
merged 8 commits into from
Oct 16, 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
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 832
PYTEST_REQPASS: 833
steps:
- uses: actions/checkout@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repos:
examples/other/some.j2.yaml|
examples/playbooks/collections/.*|
examples/playbooks/example.yml|
examples/playbooks/invalid-transform.yml|
examples/playbooks/multiline-brackets.*|
examples/playbooks/templates/not-valid.yaml|
examples/playbooks/vars/empty.transformed.yml|
Expand Down
11 changes: 11 additions & 0 deletions examples/playbooks/invalid-transform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# yamllint disable-file
---
- name: Test
hosts: localhost
gather_facts: false

tasks:
- name: Print hello message
ansible.builtin.debug:
msg: "Hello!"
register: vm_output
4 changes: 4 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Module 'ruamel.yaml' does not explicitly export attribute 'YAML'; implicit reexport disabled
# To make the type checkers happy, we import from ruamel.yaml.main instead.
from ruamel.yaml.main import YAML
from ruamel.yaml.parser import ParserError
from ruamel.yaml.scalarint import ScalarInt
from yamllint.config import YamlLintConfig

Expand Down Expand Up @@ -953,6 +954,9 @@ def loads(self, stream: str) -> Any:
data = self.load(stream=text)
except ComposerError:
data = self.load_all(stream=text)
except ParserError:
data = None
_logger.error("Invalid yaml, verify the file contents and try again.")
if preamble_comment is not None and isinstance(
data,
(CommentedMap, CommentedSeq),
Expand Down
7 changes: 7 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ def fixture_runner_result(
False,
id="github-workflow",
),
pytest.param(
"examples/playbooks/invalid-transform.yml",
1,
False,
True,
id="invalid_transform",
),
),
)
@mock.patch.dict(os.environ, {"ANSIBLE_LINT_WRITE_TMP": "1"}, clear=True)
Expand Down