fix: handle non-mapping YAML root in YamlConfigurationFileParser#1386
Open
smuchow1962 wants to merge 1 commit into
Open
fix: handle non-mapping YAML root in YamlConfigurationFileParser#1386smuchow1962 wants to merge 1 commit into
smuchow1962 wants to merge 1 commit into
Conversation
AddYamlAppConfig feeds every *.y* file under the app-config folder through
YamlConfigurationFileParser. The parser guarded only the empty-document case
and then cast the root node unconditionally to YamlMappingNode:
var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
Home Assistant !include targets such as automations.yaml have a sequence root
(automations.yaml is literally "[]"), so the cast throws
InvalidCastException: YamlSequenceNode -> YamlMappingNode inside
HostBuilder.Build() -> InitializeAppConfiguration() at config-build time,
before the host starts.
Guard the root-node type the same way the empty-document case is already
guarded: a non-mapping root carries no key/value config, so it contributes no
data instead of throwing. Adds regression tests for sequence-root,
scalar-root, and empty YAML files.
FrankBakkerNl
approved these changes
Jun 3, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1386 +/- ##
====================================
- Coverage 82% 82% -1%
====================================
Files 202 202
Lines 3941 3941
Branches 444 445 +1
====================================
- Hits 3240 3239 -1
- Misses 531 532 +1
Partials 170 170
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The host crashes at config-build time when an app-config folder contains a YAML file whose root is not a mapping.
I hit this running a real config: a Home Assistant
!includetarget likeautomations.yamlwhose content is a sequence root ([]) gets picked up byAddYamlAppConfig, and the parser casts the root unconditionally to a mapping. That throwsInvalidCastExceptionfrom insideHostBuilder.Build(), before the host (and any logging) is up, so the failure is opaque.Repro
An app-config directory containing a YAML file with a sequence root:
AddYamlAppConfigfeeds it throughYamlConfigurationFileParser, and(YamlMappingNode)RootNodethrows duringHostBuilder.Build().Root cause
The parser guards only the zero-document case, then casts any root unconditionally to
YamlMappingNode. A sequence root or a scalar root is a valid YAML document, so the cast throws instead of being handled.Fix
A pattern-match guard. The configuration model is key/value pairs, so only a mapping root carries config data. A non-mapping root carries no key/value config, so it contributes nothing instead of throwing. This mirrors the existing empty-file guard right above it.
Tests
Added regression cases to
ConfigTests:[]) — theautomations.yamlcaseEach asserts the built configuration contributes no data instead of throwing.
Notes
Found by running a real config, not synthetically. Frank asked for the fix to be sent upstream.
I couldn't apply the
pr: bugfixlabel from a fork — a maintainer may want to add it.