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

YAML validation issue #75374

Closed
RoboMagus opened this issue Jul 17, 2022 · 19 comments · Fixed by #75510
Closed

YAML validation issue #75374

RoboMagus opened this issue Jul 17, 2022 · 19 comments · Fixed by #75510

Comments

@RoboMagus
Copy link
Contributor

The problem

When writing the following automation and asking HA to validate my config, no warnings or errors show up. Also when reloading the automations, nothing warns me about the incorrect quoting. When reloading, the entire file seems to be just skipped completely, as other automations in the same file will also be missing from the UI.

- id: '1658085239190'
  alias: Config validation test
  description: ''
  trigger:
  - platform: time
    at: 00:02:03
  condition: []
  action:
  - service: script.notify_admin
    data:
      # 3 quotes here are not caught at all!
      title: 'Here's something that does not work...!'
      message: failing
  mode: single

Yes this is user error, but it feels like something that should still be caught by the config validation.

What version of Home Assistant Core has the issue?

2022.7.5

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

Nothing is shows in the logs, also not with automation logging set to debug.

Example YAML snippet

automation:
- id: '1658085239190'
  alias: Config validation test
  description: ''
  trigger:
  - platform: time
    at: 00:02:03
  condition: []
  action:
  - service: script.notify_admin
    data:
      # 3 quotes here are not caught at all!
      title: 'Here's something that does not work...!'
      message: failing
  mode: single

Anything in the logs that might be useful for us?

No response

Additional information

No response

@ghost
Copy link

ghost commented Jul 18, 2022

The YAML loader has been switched over a new C based loader in #73337. For those of us who use primarily YAML to write automations and configure sensors the old pure python based loader may be preferable due to the ease of debugging and graceful handling of errors. Is there a way via configuration.yaml to force the use of the python loader?

@RoboMagus
Copy link
Contributor Author

I was hoping this would be something that could be fixed in Home Assistant, but seeing as this is an external utility that's misbehaving I agree that having the option to select the slower but stable one instead would definitely have my vote.

@ghost
Copy link

ghost commented Jul 19, 2022

Looking at the code it appears the intended behavior is to try the C loader first. If it fails, then use the python loader to get a useful debug message to dump into the logs which is a reasonable approach. CSafeLoader is failing in a manner not triggering the exception.

@RoboMagus
Copy link
Contributor Author

@bdraco, could you weigh in on this?

@ghost
Copy link

ghost commented Jul 20, 2022

Other untriaged issues related to new YAML loader #74806, #75006, #75070

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

@bdraco, could you weigh in on this?

Reproduction is proving difficult on this one but it might be because the examples are pasted into GitHub and they have been modified in some way via that process.

It would be helpful if someone has an example configuration file that has this behavior hasn't been pasted into GitHub and was uploaded as a .txt attachment

@RoboMagus
Copy link
Contributor Author

It would be helpful if someone has an example configuration file that has this behavior hasn't been pasted into GitHub and was uploaded as a .txt attachment

Here ya go:
automations.yaml.txt

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

Thanks. Making progress now

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

Looks like you opened yaml/pyyaml#655 as well

Thats good you can get it to throw. I was worried you wouldn't be able to

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

Oh but it looks like the exception is a usage error not a parser error

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

yaml.parser.ParserError: while parsing a block mapping
  in "automations.yaml", line 11, column 7
did not find expected key
  in "automations.yaml", line 11, column 20
from yaml import load, dump
from yaml import CLoader as Loader, CDumper as Dumper
#from yaml import Loader, Dumper

with open("automations.yaml", "r") as fh:
    print(load(fh, Loader=Loader))

I get a parser error with that

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

>>> from homeassistant.util.yaml import load_yaml
>>> load_yaml("config/automations.yaml")
OrderedDict()

But I get an empty thing in home assistant

@RoboMagus
Copy link
Contributor Author

But I get an empty thing in home assistant

That's indeed what I've been able to find as well when troubleshooting a bit myself. The fact that it simply does not return anything instead of an error.

Based on the testing done for yaml/pyyaml#655, it seems like what's going on in CSafeLoader is that for malformed inputs, the load function does not even exist for the object created.

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

So the object isn't a TextIO in this case its a TextIOWrapper which we don't have in the list of objects to rewind so we can try again

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

needs to change to if isinstance(content, (StringIO, TextIO, TextIOWrapper)):

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

So the test is mocking out the IO which ends up returning StringIO instead of TextIO

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

@RoboMagus fixed in #75510

Thanks for the report

@RoboMagus
Copy link
Contributor Author

Thanks for the great work @bdraco!
I was also going to suggest adding a test case for catching bad yaml, but looking at your PR you already beat me to it ;)

@bdraco
Copy link
Member

bdraco commented Jul 20, 2022

Thanks for the great work @bdraco!

I was also going to suggest adding a test case for catching bad yaml, but looking at your PR you already beat me to it ;)

Well we had a test but there was too much mocking so I added one that is more end to end since the problem was the mocked I/O had the different object class

@github-actions github-actions bot locked and limited conversation to collaborators Aug 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants