sqlfluff / vscode-sqlfluff

An extension to use the sqlfluff linter in vscode.
https://marketplace.visualstudio.com/items?itemName=dorzey.vscode-sqlfluff
MIT License
143 stars 33 forks source link

Unable to run format due to missing flag #65

Open pauheri opened 1 year ago

pauheri commented 1 year ago

When trying to format some documents the extension provides two error boxes:

  1. SQLFluff templating/parsing errors found
  2. Fix aborted due to unparseable template variables. Use --FIX-EVEN-UNPARSABLE' to attempt to fix the SQL anyway

I am currently using it with the dialect redshift and Linter:Run onType, I think that's all the changes I have in the config.

Is there a way to add the flag in the settings json? I haven't found any in the docs.

RobertOstermann commented 1 year ago

You could try setting the sqlfluff.ignoreParsing setting to true. If that doesn't work, try setting the sqlfluff.executablePath to include that flag. For example, sqlfluff.executablePath = "sqlfluff --FIX-EVEN-UNPARSABLE".

Let me know if either of those options works for you, if not I can add a setting to allow you to pass in arguments manually.

pauheri commented 1 year ago

I've just tried both, restarting vscode after each change just to be on the safe side. The sqlfluff.ignoreParsing lead to the same result, changing the executable path to add the flag resulted in the following error The sqlfluff executable was not found. Use the 'Executable Path' setting to configure the location of the executable, or add it to your PATH.. I've tried running the path without the flag and it seems to be ok.

RobertOstermann commented 1 year ago

Try v0.1.4 and use the format.arguments option to add the argument.

pauheri commented 1 year ago

I've updated to v0.1.4 and set the option in the settings.json as "sqlfluff.format.arguments": ["--FIX-EVEN-UNPARSABLE"], . However I am still getting the same error box (Fix aborted due to unparseable template variables. Use --FIX-EVEN-UNPARSABLE' to attempt to fix the SQL anyway). Maybe I have something else not properly set? My other sqlfluff related config is:

    "sqlfluff.rules": [

    ],
    "sqlfluff.executablePath": "/Users/pau/miniconda3/bin/sqlfluff",
    "sqlfluff.excludeRules": [

    ],
    "files.associations": {
        "*.sql": "jinja-sql"
    },
    "sqlfluff.dialect": "redshift",
    "sqlfluff.ignoreParsing" : true,
    "sqlfluff.format.arguments": ["--FIX-EVEN-UNPARSABLE"],
RobertOstermann commented 1 year ago

Can you check the output tab and look at the SQLFluff channel. This will tell you the exact command it is executing when the formatting is taking place. Copy that command and try running it in the terminal and see if the output is the same. If the "--FIX-EVEN-UNPARSABLE flag is not there add it and see if that works on the terminal.

pauheri commented 1 year ago

The output tab does have the flag, it does not have the path to the file it's trying to format (although the file content is in the output tab) and the command finishes with a - :


--------------------Executing Command--------------------

/Users/pau/miniconda3/bin/sqlfluff fix --force --FIX-EVEN-UNPARSABLE --dialect redshift --ignore parsing -

------------------------------------------------------------

However I tried running /Users/pau/miniconda3/bin/sqlfluff fix path_to_file.sql --force --FIX-EVEN-UNPARSABLE --dialect redshift --ignore parsing and it did work, formatting the file (with some unfixable stuff showing a WARNING One fix for L005 not applied, it would re-cause the same error. in the terminal output)

RobertOstermann commented 1 year ago

The command finished with - because I am letting sqlfluff know that it needs to format using stdin content, which I pass in after executing that command. I wonder if it won't work because of the warnings? I'm not sure. I can try to look into this more when I have some time, but that might be a while.

For now, could you try setting "sqlfluff.experimental.format.executeInTerminal": true,. That forces the extension to pass in the file path instead of using stdin, but can come with some drawbacks as editing the file while the formatting is taking place causes an issue, but that is not too big of a deal. I'm hopeful that this would at least do the formatting though.