cheshirekow / cmake_format

Source code formatter for cmake listfiles.
GNU General Public License v3.0
949 stars 104 forks source link

[cmake-lint] crash with empty file #290

Open WenbinHou opened 2 years ago

WenbinHou commented 2 years ago

Thank you for these great tools! We have encountered a little problem with it, and it's quite easy to reproduce: cmake-lint crashes with empty file (default config)

$ touch empty.cmake
$ cmake-lint empty.cmake 
ERROR An internal error occured. Please consider filing a bug report at github.com/cheshirekow/cmakelang/issues
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/__main__.py", line 185, in main
    return inner_main()
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/__main__.py", line 168, in inner_main
    process_file(cfg, local_ctx, intext)
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/__main__.py", line 47, in process_file
    checker.check_parse_tree(parse_tree)
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/basic_checker.py", line 825, in check_parse_tree
    self.check_tree(node)
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/basic_checker.py", line 806, in check_tree
    self.check_body(node)
  File "/usr/local/lib/python3.9/site-packages/cmakelang/lint/basic_checker.py", line 546, in check_body
    lineno = node.get_tokens()[-1].get_location().line
IndexError: list index out of range

cmake-lint version: 0.6.13 (installed from pip)

WenbinHou commented 2 years ago

If the file contains a newline (echo >empty.cmake), cmake-lint won't crash.

patzm commented 1 year ago

worked for us, thanks @WenbinHou. Adding a more descriptive error message or even tolerating that would be very helpful 💡.