Repository: pre-commit/pre-commit-hooks

Issue #468: Yaml Merge Keys give false positive
Created: 2020-05-06T14:03:49Z
The check-yaml is giving an error for the following valid YAML file.  It is identifying << as a key
It seems Merge Keys are not supported by ruamel.yaml.  Perhaps stop using this yaml package.  I tried to find the source for it.  From PyPI it links to BitBucket where it has been deleted and links to SourceForge which of littered with advertisments.  Also SourceForge has a history of embedding malware.
example.yml
.foo: &foo
  spam:
    - one
    - two

.bar: &bar
  eggs:
    - three
    - four

example:
  <<: *foo
  <<: *bar
The error
Traceback (most recent call last):
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/bin/check-yaml", line 8, in <module>
    sys.exit(main())
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/pre_commit_hooks/check_yaml.py", line 63, in main
    load_fn(f)
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/main.py", line 343, in load
    return constructor.get_single_data()
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/constructor.py", line 113, in get_single_data
    return self.construct_document(node)
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/constructor.py", line 123, in construct_document
    for _dummy in generator:
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/constructor.py", line 723, in construct_yaml_map
    value = self.construct_mapping(node)
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/constructor.py", line 439, in construct_mapping
    self.flatten_mapping(node)
  File "/home/eric/.cache/pre-commit/repofg6bh5oq/py_env-python3.8/lib/python3.8/site-packages/ruamel/yaml/constructor.py", line 396, in flatten_mapping
    raise DuplicateKeyError(*args)
ruamel.yaml.constructor.DuplicateKeyError: while constructing a mapping
  in "example.yml", line 12, column 3
found duplicate key "<<"
  in "example.yml", line 13, column 3

Duplicate issue #681: issue with check-yaml and .gitlab-ci.yml
Created: 2021-10-28T12:44:47Z
First of all, thank you for the excellent work.
I started using check-yaml, and when I tried to use "map merging" as described in the gitlab documentation (https://docs.gitlab.com/ee/ci/yaml/yaml_specific_features.html) I noticed that check-yaml complains about duplicate keys:
Check Yaml...............................................................Failed
- hook id: check-yaml
- exit code: 1

while constructing a mapping
  in ".gitlab-ci.yml", line 39, column 3
found duplicate key "<<"
  in ".gitlab-ci.yml", line 40, column 3

To suppress this check see:
   http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys

Code example triggering this:
.secure-runner: &secure-runner
  tags:
    # This runner is more secure, but it is slower.
    - onetime

.docker-build: &docker-build
  image: docker:20.10.10
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $IMAGE_TAG -f $DOCKERFILE .
    - docker push $IMAGE_TAG

Build production container:
  <<: *secure-runner
  <<: *docker-build
  stage: prod-build
  variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    DOCKERFILE: Dockerfile

I can work around this by using --unsafe, but I believe I do not need the multi-document syntax. Is it also possible to allow more fine grained control over which yaml features are allowed, and which aren't?

Closing comment (2021-10-28T14:53:12Z): dupe #468