theme-tools icon indicating copy to clipboard operation
theme-tools copied to clipboard

Formatting `.liquid` files deletes their contents

Open joshistoast opened this issue 9 months ago β€’ 9 comments

Describe the bug

When both the extension itself and formatOnSave is enabled on liquid files, the entire file is wiped on save.

An issue for this existed but was prematurely closed: #327

Source Any .liquid file

Expected behavior

Formatting the file

Actual behavior

Deletes all contents of the file

Debugging information

  • OS: MacOS 14.4.1
  • Theme Check Version: 2.3.2

Additional context

Setting formatOnSave to false prevents further deletions of file contents, but doesn't format any files.

{
  ...
  "[liquid]": {
    "editor.defaultFormatter": "Shopify.theme-check-vscode",
    "editor.formatOnSave": false
  },
  ...
}

joshistoast avatar May 14 '24 15:05 joshistoast

hi @joshistoast , thanks for reporting this issue πŸ™ to confirm, is this happening for all liquid files or just certain ones?? because this doesn't appear to be broken for everyone, this is our current suspicion:

if it’s happening on all liquid files

  • it’s likely an environment issue
  • it can be an issue in the vscode-extension or possibly a conflict with another extension (as mentioned here)

if it’s happening only in some liquid files

  • it’s likely an issue in parser/formatter, then we can get the asset name to reproduce ourselves

lastly if you could share your extension logs, that would also give us more insight about the issue. here's how you can do it in vs code:

getting-logs

thanks in advance for your help as we investigate

mgmanzella avatar May 16 '24 18:05 mgmanzella

@mgmanzella Within my settings.json

"[liquid]": {
  "editor.defaultFormatter": "Shopify.theme-check-vscode",
  "editor.formatOnSave": true
},

https://github.com/Shopify/theme-tools/assets/23065423/8509dc3b-a7ee-494a-abe6-0a3497d6264a

The issue is immediately resolved when setting editor.formatOnSave to false

joshistoast avatar May 16 '24 18:05 joshistoast

appreciate you sharing that but could you please answer the questions in my previous comment, we need this information to understand what's happening. thanks πŸ™

mgmanzella avatar May 16 '24 18:05 mgmanzella

appreciate you sharing that but could you please answer the questions in my previous comment, we need this information to understand what's happening. thanks πŸ™

Apologies, it happens in every liquid file

joshistoast avatar May 16 '24 18:05 joshistoast

ty! this is likely an issue related to your environment. can you also share the vs code extension logs from your editor for language server

an additional workaround you can try is deactivating other extensions and see if the issue still persists, keep us posted tho πŸ™

mgmanzella avatar May 16 '24 19:05 mgmanzella

@mgmanzella language server logs should be in the video on the bottom half of the window.

an additional workaround you can try is deactivating other extensions and see if the issue still persists, keep us posted tho

Did this also but to no avail, but uninstalling the shopify extension in question did solve it.

joshistoast avatar May 16 '24 20:05 joshistoast

language server logs should be in the video on the bottom half of the window.

ahhh you're right my bad, do you mind sending the raw logs from language server?? if you dont feel comfortable doing that publicly, feel free to DM me on the partners slack @Morisa Manzella

Did this also but to no avail, but uninstalling the shopify extension in question did solve it.

right which removes liquid prettier from vs code 😭

we'll investigate thanks again for raising πŸ™

mgmanzella avatar May 16 '24 21:05 mgmanzella

Update, editor update on save set to false and my file is still being wiped.

joshistoast avatar Jun 21 '24 19:06 joshistoast

Raw logs:

[SERVER] Let's roll!
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/data/latest.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/latest.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/data/filters.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/filters.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/data/shopify_system_translations.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/shopify_system_translations.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/manifest_theme_app_extension.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/manifest_theme_app_extension.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/manifest_theme.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/manifest_theme.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/data/tags.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/tags.json
Successfully downloaded latest resource:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/data/objects.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/objects.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/translations.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/translations.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/theme_block_entry.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_block_entry.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/settings.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/settings.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/default_setting_values.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/default_setting_values.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/setting.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/setting.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/section.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/section.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/theme_settings.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_settings.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/theme_block.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_block.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/app_block_entry.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/app_block_entry.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/local_block_entry.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/local_block_entry.json
Successfully downloaded schema:
	https://raw.githubusercontent.com/Shopify/theme-liquid-docs/main/schemas/theme/targetted_block_entry.json
	> /Users/josh/Library/Caches/theme-liquid-docs-nodejs/targetted_block_entry.json
Loaded resource from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/manifest_theme.json
Loaded resource from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/manifest_theme_app_extension.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/translations.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_block.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_settings.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/section.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/settings.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/setting.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/default_setting_values.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/app_block_entry.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/theme_block_entry.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/targetted_block_entry.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/local_block_entry.json
Loaded schema from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/translations.json
Loaded resource from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/filters.json
Loaded resource from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/shopify_system_translations.json
Loaded resource from /Users/josh/Library/Caches/theme-liquid-docs-nodejs/tags.json

joshistoast avatar Jun 21 '24 19:06 joshistoast

More context: https://github.com/Shopify/theme-tools/issues/586#issuecomment-2476780957

Maybe it's WSL related? How is this happening? We're supposed to not do anything when there's an error. Why would prettier.format return the empty string without error?

charlespwd avatar Nov 15 '24 16:11 charlespwd

@charlespwd Thanks for moving this here! Happy to give more context if necessary.

jpallard6120 avatar Nov 15 '24 21:11 jpallard6120

Just found out this morning we don't support prettier.config.js (#619) with ESM in them and that the error logs from the formatter are output to the "Window" group in the Output pane. The behaviour for that is slightly different: the plugin doesn't do anything.

For any of you who gets the empty string, could you tell me if you find something fishy in the Window group of the Output pane?

Image

If you delete your prettier config file, does the problem go away?

charlespwd avatar Nov 26 '24 14:11 charlespwd

I tried deleting .prettierrc, but it just removed the ability to format through Shopify Theme Check.

I don't have anything super weird in the window group, apart from these lines maybe?

2024-11-26 14:37:05.548 [warning] [backup tracker] suspended, ignoring unregister event vscode-remote://wsl%2Bubuntu-22.04/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/package.json 

Funnily enough, I had an actual error within a test of a file with a syntax error, so it means Theme Check does get the data correctly:

2024-11-26 14:32:37.977 [error] [Extension Host] LiquidHTMLParsingError: Line 74, col 22: expected not an identifierCharacter, "}}", "%}", "-}}", or "-%}"
  72 |         {%- unless is_month -%}
  73 |         {{ parent_tag | strip }},
> 74 |         {%- endunless- %}
     |                      ^
  75 |       {%- endfor -%}
  76 |      {%- endcapture -%}
  77 |       {% for article in blog.articles offset: 1 %}
	at toCST (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:46922:15)
	at toLiquidHtmlCST (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:46892:12)
	at toLiquidHtmlAST (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:47633:51)
	at Object.parse (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:49408:53)
	at Object.parse (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/index.js:7515:23)
	at coreFormat (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/index.js:8829:18)
	at formatWithCursor2 (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/index.js:9021:18)
	at /home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/index.js:38183:12
	at Object.format (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/prettier/index.js:38197:12)
	at nodePrettierFormat (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:19014:21)
	at async LiquidFormatter.toTextEdit (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:18833:27)
	at async LiquidFormatter.provideDocumentFormattingEdits (/home/wake/.vscode-server/extensions/shopify.theme-check-vscode-3.1.0/dist/node/extension.js:18823:32)
	at async g$.provideDocumentFormattingEdits (file:///home/wake/.vscode-server/bin/f1a4fb101478ce6ec82fe9627c43efbf9e98c813/out/vs/workbench/api/node/extensionHostProcess.js:137:134090)

On another file without the syntax error, the file is wiped as usual, and nothing is logged in the Window group.

jpallard6120 avatar Nov 26 '24 19:11 jpallard6120

On another file without the syntax error, the file is wiped as usual, and nothing is logged in the Window group.

Makes sense. Would only happen if textEdit sent an empty string as a TextEdit...

This is definitely some ghostbusters kind of issue.

So...

  • we got that parsing blocks pretty-printing if it errors out (therefore parsing happens...)
  • we got that somehow we print an empty string (or is it invalid text position?)

I wonder...

  • Encoding maybe (not utf8?)?
  • Windows/linux newlines? (\r\n?)

charlespwd avatar Nov 26 '24 20:11 charlespwd