PabloRMira / sql_formatter

A Python based SQL formatter
https://pablormira.github.io/sql_formatter/
Apache License 2.0
43 stars 10 forks source link

NPE when formatting multiline comments #174

Open erlendvollset opened 2 years ago

erlendvollset commented 2 years ago

Describe the bug

Running the formatter (v0.6.2) with pre-commit fails with an NPE:

Traceback (most recent call last):
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/bin/sql-formatter", line 8, in <module>
    sys.exit(format_sql_files_cli())
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/format_file.py", line 204, in format_sql_files_cli
    format_sql_files(files=args.files, recursive=args.recursive, max_len=args.max_line_length)
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/format_file.py", line 168, in format_sql_files
    exit_codes.append(format_sql_file(file, max_len=max_len))
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/format_file.py", line 117, in format_sql_file
    formatted_file = format_sql_commands(sql_commands, max_len=max_len)
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/format_file.py", line 43, in format_sql_commands
    formatted_split_s2 = [
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/format_file.py", line 44, in <listcomp>
    "\n\n\n" + format_sql(sp, semicolon=True, max_len=max_len).strip()
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/core.py", line 466, in format_sql
    s = format_simple_sql(s, semicolon=semicolon, max_len=max_len)  # basic query formatting
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/core.py", line 457, in format_simple_sql
    s = format_multiline_comments(s)  # format multline comments
  File "/Users/erlendvollset/.cache/pre-commit/repoy6iw_xck/py_env-python3/lib/python3.10/site-packages/sql_formatter/core.py", line 426, in format_multiline_comments
    indentation = re.search(r"\/\*", sp).start() + 3
AttributeError: 'NoneType' object has no attribute 'start'

Expected behavior

No error. Or some better error message indicating which part of my (valid) sql is the problem.

To reproduce

I have failed to create a minimal reproducible example.

imitevm commented 5 months ago

Caused by single-line comment inside a multi-line one. Example:

from sql_formatter.core import format_sql

format_sql("""
select 1
/*
--
*/
""")