Skip to content

Commit

Permalink
Use relative paths in yaml syntax error messages (home-assistant#104084)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Nov 17, 2023
1 parent e5bc255 commit 23ef97f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
25 changes: 19 additions & 6 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/snapshots/test_config.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 23ef97f

Please sign in to comment.