aws-cloudformation / cfn-lint

CloudFormation Linter
MIT No Attribution
2.45k stars 592 forks source link

TypeError: object of type 'NoneType' has no len() #3618

Closed kamilturek closed 2 months ago

kamilturek commented 2 months ago

CloudFormation Lint Version

1.10.3

What operating system are you using?

Mac

Describe the bug

When running cfn-lint as a pre-commit hook:

  - repo: https://github.com/aws-cloudformation/cfn-lint
    rev: v1.10.3
    hooks:
      - id: cfn-lint
        files: ^template\.yaml$
        args:
          - --format
          - pretty
          - --ignore-checks
          - W3002
$ pre-commit run --all-files
AWS CloudFormation Linter................................................Failed
- hook id: cfn-lint
- exit code: 1

Traceback (most recent call last):
  File "/Users/k.turek/.cache/pre-commit/repo5l8we76_/py_env-python3.12/bin/cfn-lint", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/k.turek/.cache/pre-commit/repo5l8we76_/py_env-python3.12/lib/python3.12/site-packages/cfnlint/runner.py", line 448, in main
    runner.cli()
  File "/Users/k.turek/.cache/pre-commit/repo5l8we76_/py_env-python3.12/lib/python3.12/site-packages/cfnlint/runner.py", line 439, in cli
    self._cli_output(list(self.run()))
  File "/Users/k.turek/.cache/pre-commit/repo5l8we76_/py_env-python3.12/lib/python3.12/site-packages/cfnlint/runner.py", line 327, in _cli_output
    output = formatter.print_matches(matches, self.rules, config=self.config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/k.turek/.cache/pre-commit/repo5l8we76_/py_env-python3.12/lib/python3.12/site-packages/cfnlint/formatters/pretty.py", line 31, in print_matches
    f"Cfn-lint scanned {colored(len(config.templates), color.bold_reset)} templates against "
                                ^^^^^^^^^^^^^^^^^^^^^
TypeError: object of type 'NoneType' has no len()

Expected behavior

Do not crash

Reproduction template

Description: Anything
kddejong commented 2 months ago

I think this is because how parameters are created when doing the pre-commit.

args:
- --ignore-checks
- W3002
- --format
- pretty

or

args:
- --format
- pretty

This seems to be related to adding this to the end

- --ignore-checks
- W3002

Looks like there is workaround to add one mroe item:

args:
- --format
- pretty
- --ignore-checks
- W3002
- --template

but I also added a PR to fix this issue as it is an issue when doing a pipe of the template. This should also fix your issue but it may depend on how pre-commit is building the parameters.