Use relative paths in yaml syntax error messages (#104084)

This commit is contained in:
Erik Montnemery 2023-11-17 23:00:23 +01:00 committed by GitHub
parent e5bc25523e
commit 23ef97f774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 11 deletions

View File

@ -18,6 +18,7 @@ from urllib.parse import urlparse
from awesomeversion import AwesomeVersion
import voluptuous as vol
from voluptuous.humanize import MAX_VALIDATION_ERROR_ITEM_LENGTH
from yaml.error import MarkedYAMLError
from . import auth
from .auth import mfa_modules as auth_mfa_modules, providers as auth_providers
@ -393,12 +394,24 @@ async def async_hass_config_yaml(hass: HomeAssistant) -> dict:
secrets = Secrets(Path(hass.config.config_dir))
# Not using async_add_executor_job because this is an internal method.
config = await hass.loop.run_in_executor(
None,
load_yaml_config_file,
hass.config.path(YAML_CONFIG_FILE),
secrets,
)
try:
config = await hass.loop.run_in_executor(
None,
load_yaml_config_file,
hass.config.path(YAML_CONFIG_FILE),
secrets,
)
except HomeAssistantError as ex:
if not (base_ex := ex.__cause__) or not isinstance(base_ex, MarkedYAMLError):
raise
# Rewrite path to offending YAML file to be relative the hass config dir
if base_ex.context_mark and base_ex.context_mark.name:
base_ex.context_mark.name = _relpath(hass, base_ex.context_mark.name)
if base_ex.problem_mark and base_ex.problem_mark.name:
base_ex.problem_mark.name = _relpath(hass, base_ex.problem_mark.name)
raise
core_config = config.get(CONF_CORE, {})
await merge_packages_config(hass, config, core_config.get(CONF_PACKAGES, {}))
return config

View File

@ -349,7 +349,7 @@
# name: test_yaml_error[basic]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic/configuration.yaml", line 4, column 14
in "configuration.yaml", line 4, column 14
'''
# ---
# name: test_yaml_error[basic].1
@ -363,7 +363,7 @@
# name: test_yaml_error[basic_include]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic_include/integrations/iot_domain.yaml", line 3, column 12
in "integrations/iot_domain.yaml", line 3, column 12
'''
# ---
# name: test_yaml_error[basic_include].1
@ -377,7 +377,7 @@
# name: test_yaml_error[include_dir_list]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_list/iot_domain/iot_domain_1.yaml", line 3, column 10
in "iot_domain/iot_domain_1.yaml", line 3, column 10
'''
# ---
# name: test_yaml_error[include_dir_list].1
@ -391,7 +391,7 @@
# name: test_yaml_error[include_dir_merge_list]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_merge_list/iot_domain/iot_domain_1.yaml", line 3, column 12
in "iot_domain/iot_domain_1.yaml", line 3, column 12
'''
# ---
# name: test_yaml_error[include_dir_merge_list].1
@ -405,7 +405,7 @@
# name: test_yaml_error[packages_include_dir_named]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/packages_include_dir_named/integrations/adr_0007_1.yaml", line 4, column 9
in "integrations/adr_0007_1.yaml", line 4, column 9
'''
# ---
# name: test_yaml_error[packages_include_dir_named].1