adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.83k stars 269 forks source link

Bug: pre-commit Expected a Config map but got a list #551

Closed Ryanf55 closed 1 year ago

Ryanf55 commented 1 year ago

Description

I'm trying to use the yamllint pre-commit hook, but the example is invalid.

Versions

pre-commit version: 3.1.1 yamllint version: 1.29.0 Host OS: Ubuntu 20.04

Supporting Data

My pre -commit config file (copied from example here)

- repo: https://github.com/adrienverge/yamllint.git
  rev: v1.17.0
  hooks:
    - id: yamllint
      args: [-c=/path/to/.yamllint]

Error when running:

$ pre-commit run
An error has occurred: InvalidConfigError: 
==> File .pre-commit-config.yaml
=====> Expected a Config map but got a list
Check the log at /home/ubuntu/.cache/pre-commit/pre-commit.log

Logs

version information

pre-commit version: 3.1.1
git --version: git version 2.25.1
sys.version:
    3.8.10 (default, Mar 13 2023, 10:26:41) 
    [GCC 9.4.0]
sys.executable: /usr/bin/python3
os.name: posix
sys.platform: linux

error information

An error has occurred: InvalidConfigError: 
==> File .pre-commit-config.yaml
=====> Expected a Config map but got a list
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pre_commit/error_handler.py", line 73, in error_handler
    yield
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pre_commit/main.py", line 394, in main
    return run(args.config, store, args)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pre_commit/commands/run.py", line 417, in run
    config = load_config(config_file)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 411, in load_from_filename
    return apply_defaults(data, schema)
  File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 43, in reraise_as
    raise tp(e).with_traceback(tb) from None
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 40, in reraise_as
    yield
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 411, in load_from_filename
    return apply_defaults(data, schema)
  File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 34, in validate_context
    raise ValidationError(e, ctx=msg).with_traceback(tb) from None
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 31, in validate_context
    yield
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 410, in load_from_filename
    validate(data, schema)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 376, in validate
    schema.check(v)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/cfgv.py", line 233, in check
    raise ValidationError(
pre_commit.clientlib.InvalidConfigError: 
==> File .pre-commit-config.yaml
=====> Expected a Config map but got a list
andrewimeson commented 1 year ago

The main problem is that your pre-commit file is incomplete, it looks like this:

- repo: https://github.com/adrienverge/yamllint.git
  rev: v1.17.0
  hooks:
    - id: yamllint
      args: [-c=/path/to/.yamllint]

but it should look like this

repos:
  - repo: https://github.com/adrienverge/yamllint.git
    rev: v1.17.0
    hooks:
      - id: yamllint
        args: [-c=/path/to/.yamllint]

A couple other things:

  1. You list your yamllint version as 1.29.0 - that is probably what is in your global installation (i.e. in your path), but the pre-commit configuration you are using installs its own version of yamllint which is set at v1.17.0 in your example. Once you correct the example, you can run pre-commit autoupdate to update all your hooks to their latest version.
  2. You have the args list set to the default example, which points to a nonexistent file. Unless you have a nonstandard location for the config file, you can just omit that.

Your desired config might be

---
repos:
  - repo: https://github.com/adrienverge/yamllint.git
    rev: v1.29.0
    hooks:
      - id: yamllint
andrewimeson commented 1 year ago

also note that the error you shared is exclusively from pre-commit erroring out on parsing the configuration, not from erroring out loading the yamllint hook. pre-commit used to support specifying hooks at the top level, but deprecated that at some point. My older version of pre-commit at least threw a warning, but using your version it just errors out.

[WARNING] normalizing pre-commit configuration to a top-level map.  support for top level list will be removed in a future version.  run: `pre-commit migrate-config` to automatically fix this.

I think our docs could use an update.

Ryanf55 commented 1 year ago

Got it. I upgraded. You can use close this once the docs are updated.

I did try running it, but it's not agreeing with running yamllint in CLI. Should I file another issue, or am I using it incorrectly? The files are staged.

New pre-commit config:

 ---
repos:
  - repo: https://github.com/adrienverge/yamllint.git
    rev: v1.29.0
    hooks:
      - id: yamllint

Example file:

a: b
c: d
$ yamllint .

./foo.yaml
  1:1       warning  missing document start "---"  (document-start)
  2:5       error    no new line character at the end of file  (new-line-at-end-of-file)

  $ pre-commit run
yamllint.................................................................Passed
andrewimeson commented 1 year ago

@Ryanf55 I can reproduce that issue as well. Can you split this to a new issue though? The initial issue will be resolved by the docs change PR.