claylo / yaml-include

Valid, modular YAML documents with js-yaml. Seriously.
ISC License
39 stars 17 forks source link

Referencing included aliases result in unidentified alias #5

Closed johannes-z closed 4 years ago

johannes-z commented 7 years ago

Consider this file structure:

# in include1.yml
props:
  - &key
    key: value
# in main.yml
name: main

include1: !!inc/file include1.yml

properties:
  - *key

It will throw a YAMLException with: unidentified alias "key", because the key isn't even included.

claylo commented 7 years ago

This would be handy if it worked -- unfortunately, it won't at this point without some modding on js-yaml itself.

yaml-include doesn't concatenate all the included files and then process them as the final document. Instead, it processes each file and builds an object with the result. Since each file is a new yaml.load() call, and since js-yaml does not support persisting anchors between file loads, aliases in separate files are considered unidentified.

I've been thinking of ways around this. One might be a special !!inc/alias type of inclusion, which would load the anchors and then expose an object where the values could be retrieved by key in all subsequently included files.

Another would be to extend js-yaml and overwrite the loader functionality to force the exposure of found anchors across subsequent files. Sort of a stateful loader.

A third way would be simply making an aliases object part of configuration, and passing those into each loaded yaml doc. The problem with this and the !!inc/alias approach is that it forces an unnatural (from the yaml perspective) definition of anchors. YAML 1.2 as-is allows the anchor declaration anywhere for first use, so long as it appears before the second use in the stream.

The second approach I'm describing here could support that, but it feels like the hairiest approach. I'll continue kicking it around and see if some easy solution presents itself. 😄