eemeli/yaml

`BAD_INDENT` when using aliases as keys

bwateratmsft opened this issue · 2 comments

Describe the bug
Originally from microsoft/compose-language-service#81. It looks like when aliases are used as keys, the parser thinks that child nodes (i.e. image in the example below) are part of the parent mapping, and considers them indented improperly.

To Reproduce
I was able to reproduce with this text:

x-services:
  - &foo_anchor foo

services:
  *foo_anchor:
    image: alpine

parseDocument() yields this error from Document<T>.errors:

All mapping items must start at the same column at line 6, column 1:

  *foo_anchor:
    image: alpine
^

Expected behaviour
No error was expected. In this case, it's a compose document, and docker-compose config appears to parse it correctly, yielding (with some extra implicit values put in):

services:
  foo:
    image: alpine
    networks: # Implicit value added by docker-compose
      default: null
networks: # Implicit value added by docker-compose
  default:
    name: net6new_default
x-services:
- foo

Versions

  • Environment: VSCode 1.63.2 with Docker extension 1.19.0 installed
  • Electron: 13.5.2
  • Node.js: 14.16.0
  • yaml: 2.0.0-9, also tried 2.0.0-10 with same result

This actually isn't a bug in the implementation, but the spec. In the alias node, the trailing : is being captured as a part of the alias name, rather than acting as a separator between the mapping key & value: https://yaml.org/spec/1.2.2/#71-alias-nodes

Please don't close this bug right away though, as the error should be reported more clearly here; BAD_INDENT is the wrong code for this.

Very interesting, thank you for the help @eemeli!