Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when formatting multiline comments #174

Open
erlendvollset opened this issue Jun 27, 2022 · 1 comment
Open

NPE when formatting multiline comments #174

erlendvollset opened this issue Jun 27, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@erlendvollset
Copy link

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.

@erlendvollset erlendvollset added the bug Something isn't working label Jun 27, 2022
@imitevm
Copy link

imitevm commented Jun 6, 2024

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

from sql_formatter.core import format_sql

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants