vscode-sqlfluff icon indicating copy to clipboard operation
vscode-sqlfluff copied to clipboard

`Format Document` or `SQLFluff: Fix` overwrites file contents with sqlfluff logs

Open MiConnell opened this issue 1 year ago • 4 comments

when running the extension from the command palette the contents of the file get overwritten with logs and no formatting is applied. this does not happen when running from the command line and in the below example no fixable violations are found.

installed sqlfluff version is 3.0.3

➜ sqlfluff --version                                                
sqlfluff, version 3.0.3

installed extension version is 3.0.0

sqlfluff
v3.0.0

https://github.com/sqlfluff/vscode-sqlfluff/assets/14168559/8c27a050-9516-4a18-9695-ff813d52a173

MiConnell avatar Mar 27 '24 00:03 MiConnell

It looks like you are using the dbt templater here. Have you enabled the executeInTerminal option in the settings?

"sqlfluff.experimental.format.executeInTerminal": true,

Take note of the formatOnSave warning:

Determines if the sqlfluff fix command overwrites the file contents instead of this extension. You should not change the file contents while formatting is occurring if this is enabled. May lead to problems if editor.formatOnSave = true. This allows formatting to work when the templater is set to dbt. This can help solve Mojibake issues.

keraion avatar Mar 30 '24 03:03 keraion

Regardless of settings a code formatter should never overwrite the contents of a file with log output right? This looks similar to #126

MiConnell avatar Apr 02 '24 15:04 MiConnell

Ideally yes, however, the current implementation of this extension's default mode is to take the output from stdout and replace the content with that output. When using the dbt templater, dbt prints additional information to stdout which may be picked up from the formatter's output. The executeInTerminal option runs fix on the file directly and reloads the content from the fixed file.

keraion avatar Apr 02 '24 16:04 keraion

ok makes sense, thanks. that did fix the issue. should this just default to true for dbt templates? is that even possible?

MiConnell avatar Apr 02 '24 17:04 MiConnell