danielperna84 / hass-configurator

Configuration UI for Home Assistant
MIT License
306 stars 168 forks source link

Users reporting issues with Home Assistant File Editor "unknown tag" error for the "!include" values #226

Closed loganmarchione closed 1 year ago

loganmarchione commented 2 years ago

See this issue for full details.

After updating to Home Assistant File Editor 5.4.0, which is based on hass-configurator 0.5.0, users (myself included) are reporting issues with "unknown tag" error for the "!include" values. Again, see the other issue for logs and screenshots.

danielbrunt57 commented 2 years ago

!include within homeassistant: section being flagged:

image

image

Other !include's are fine.

danielperna84 commented 2 years ago

This must be a result of updating the js-yaml dependency. There has been no change to relevant code: https://github.com/danielperna84/hass-configurator/compare/0.4.1...0.5.0

Here is the part where such exceptions are handled, and include is still present.

Does anybody have an idea why we're having this issue?

danielbrunt57 commented 2 years ago

More testing... It's always and only the first !(include|secret|env_var|input) in the file that gets flagged and the message is from js-yaml.min.js.

danielperna84 commented 2 years ago

It's only the first instance, because it's causing js-yaml to fail at loading the whole content of the file. As a result, no linting at all will be done, as the yaml can't be parsed.

I don't get why this is happening though. Executing the code manually properly replaces all occurrences of the keywords, and thus succeeds to load the yaml eventually.

danielperna84 commented 2 years ago

Actually, I have come to believe that it's not js-yaml that is causing the problem. js-yaml is only used for the icon next to the filename that's either a green check mark or a red exclamation mark.

The linting error we're seeing in the editor seems to be a feature of the upgraded Ace Editor.

danielbrunt57 commented 2 years ago

If I introduce some other error like bad indentation, I first see the ("check_circle") with ("grey-text") when lint is queued followed by ("cursor-pointer red-text") with the error line flagged by the X in red box. Fixing the error shows ("check_circle") ("grey-text") followed by ("green-text") which would indicate the lint test passed but yet the first !(include|secret|env_var|input) is flagged with X in red box.

danielbrunt57 commented 2 years ago

The linting error we're seeing in the editor seems to be a feature of the upgraded Ace Editor.

Ah!

danielperna84 commented 2 years ago

The error can be hidden manually via the JS console with the statement: editor.getSession().setAnnotations([]);

The question remains how we can either hide the error automatically, or turn off linting in Ace without also breaking the syntax highlighting or other language-specific features.

loganmarchione commented 2 years ago

The linting error we're seeing in the editor seems to be a feature of the upgraded Ace Editor.

Can you rollback the editor version to verify? Is this something we need to report upstream to Ace?

danielperna84 commented 2 years ago

I am able to disable linting by disabling the workers (editor.session.setOption("useWorker", false);). This keeps syntax highlighting intact, which is the main feature we want in the editor. Back then Ace didn't have yaml-linting, but now it does. Hence we now get the error.

This is not a problem with Ace Editor, as the keywords are custom to Home Assistant. I have yet to find a way if it's possible to configure Ace in such a way, that the internal linting also ignores certain keywords. The implementation we already had in older version were kind of a hack (replacing bad keywords with allowed ones on the fly). This may not be possible with Ace. So disabling linting altogether probably is the only thing we can do.

danielbrunt57 commented 2 years ago

Might this be relevant or provide insight as to how to permit the 4 HA custom tags in js-yaml?

danielperna84 commented 2 years ago

With the configurator loading external files isn't that easy and requires some effort. To get rid of the bug in the short term I went with disabling the linting within Ace. That shouldn't be a problem for anyone just working with yaml files, as the old yaml-linting is working as designed. Hence I already release a bugfix release. Right now I'm in the process of creating the PR to also update the add-on.

If someone want's to get the native linting back, they're free to create a PR here with the required changes. So to me this issue can be closed as soon as the add-on-PR has been merged.

Edit:

Here's the PR for the add-on: https://github.com/home-assistant/addons/pull/2657

danielbrunt57 commented 2 years ago

Another answer I found is https://stackoverflow.com/questions/40977113/js-yaml-issue-with-tags: Take a look at examples/custom_types.js in the js-yaml repo. The solution is to create a new jsyaml.Type that tells js-yaml to treat !Status values as mappings, like so:

let StatusYamlType = new jsyaml.Type('!Status', { kind: 'mapping' }); Then you need to create a new Schema that includes the StatusYamlType type:

let STATUS_SCHEMA = jsyaml.Schema.create([ StatusYamlType ]); Finally, pass the new STATUS_SCHEMA to jsyaml.safeLoad via the schema option:

jsyaml.safeLoad(doc, { schema: STATUS_SCHEMA });

danielbrunt57 commented 2 years ago

Disregard the previous suggestion as this would have to be done inside the ACE editor...