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

Taplo lsp abandons formating and diagnostic if it cannot find a schema #580

Closed
ndtoan96 opened this issue Mar 17, 2024 · 11 comments
Closed

Comments

@ndtoan96
Copy link

I use taplo with the helix editor. Upon opening a toml file, there's only one message at the top: "this document has been excluded". I guess because it cannot find the schema. That is fine. But I cannot perform formatting for the document, and even if there's toml syntax errors, the taplo lsp does not give any warning.

I cross-checked with VSCode "even better toml" extension and everything works fine in VSCode. What causes the difference in behavior even though both editors use the same taplo lsp?

@JounQin
Copy link
Collaborator

JounQin commented Mar 17, 2024

Did you try this config option?

https://taplo.tamasfe.dev/configuration/file.html#schema

@ndtoan96
Copy link
Author

ndtoan96 commented Mar 17, 2024

Did you try this config option?

https://taplo.tamasfe.dev/configuration/file.html#schema

My file does not have any schema. The point is formatting and basic toml syntax diagnosis (something like brackets not matching...) shouldn't need a schema.

@JounQin
Copy link
Collaborator

JounQin commented Mar 18, 2024

Schema will be enabled with filename patterns by default. And why helix editor's behaviour should be related to taplo?

@ndtoan96
Copy link
Author

I still don't get your point. So, I will describe my use case in more detail.

I use helix to edit a random toml file. It has the below content:

[config_a]
name_1="value1"
name_2        =     4

helix does support taplo. I check it with Cargo.toml and it works fine. But for this random toml file, as you can guess from the content, it's just a random file with no schema, and I cannot perform formatting in helix (using :fmt command). However, I open the same file in VSCode with Even Better Toml extension installed, press Shift-Alt-F and I can format the file.

What's more, I intentionally create a syntax error like below

[config_a]
name_1="value1"
name_2        =
4

I open the file in helix, and there's no error message. I open the file in VSCode, and it says there's an error.

I hope now you can understand what I'm trying to say. Even both editors use the same taplo lsp, the helix one cannot work without a schema, while the VSCode still works without a schema. What is the difference? How can I make the helix one behaves like the VSCode one?

@JounQin
Copy link
Collaborator

JounQin commented Mar 18, 2024

I don't get your point neither, why didn't you post the issue to helix instead? That editor is out of our control?

@ndtoan96
Copy link
Author

ndtoan96 commented Mar 18, 2024

My gut told me this is an issue on taplo side.

From my understanding, in LSP, to do formatting, the editor sends a request along with the current code to the LSP server, and the server sends a response back with the formatted code. Since the LSP logic in helix is implemented generally for all languages (it does not have a toml plugin, it doesn't even have plugin system yet), I can't imagine helix fails to send the request for toml alone. Which means it's highly likely that the server side, taplo in this case, somehow refutes the request or fails to send the response.

@JounQin
Copy link
Collaborator

JounQin commented Mar 18, 2024

It requires https://github.com/helix-editor/helix's contributors to debug in depth then.

As you found, it worked for VSCode extension, so you still need to reproduce without helix to approve that's something wrong with taplo's side.

The point is always: why it's taplo's issue instead of helix's, so if you can reproduce it with a failing test case, that would be great.

@ndtoan96
Copy link
Author

Found the problem. I got suggestion when looking into the nvim-lspconfig for taplo. For the lsp to work, we need to configure the lsp root. Both VSCode and NeoVim already set the root for taplo in their plugins. Meanwhile helix is just barebone without any config and it considers a git project as root. That's why a random file from a random place is ignored.

@ndtoan96
Copy link
Author

ndtoan96 commented Mar 18, 2024

LSP root config issue.

For those who got the same problem, I was able to fix this by adding this config in languages.toml

[[language]]
name = "toml"
roots = ["."]

@GitMurf
Copy link

GitMurf commented Sep 19, 2024

FYI here is how to solve the problem in Neovim: https://www.reddit.com/r/neovim/comments/1fkprp5/how_to_properly_setup_lspconfig_for_toml_files/

The key is that by default, Taplo looks for a git repository so if you are not within one, you will get the this document has been excluded response. Hope this helps!

@Rudxain
Copy link

Rudxain commented Oct 8, 2024

Unfortunately roots = ["."] only works if PWD is a parent (direct or not) of the TOML file.

Example:

cd ~/Desktop/
# doc excluded
hx ~/.config/helix/languages.toml
cd ~/
# works!
hx .config/helix/languages.toml

Rudxain added a commit to Rudxain/dotfiles that referenced this issue Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants