rhaiscript / lsp

Language server for Rhai.
Apache License 2.0
43 stars 4 forks source link

Should distinguish between `*.d.rhai` and `*.rhai` #77

Open schungx opened 2 years ago

schungx commented 2 years ago

Currently, it seems that files with .d.rhai extension are treated the same as Rhai script files instead of LSP definition files.

This makes, for example, some Rhai code with definition files come back with parse errors. For example, in the definitions API folder:

image image

The solution is to treat *.d.rhai as definition files automatically.

Or require all definition files to have some magic doc comment at the top? For example:

//! @RHAI-DEF-FILE
schungx commented 2 years ago

I wonder why this same thing doesn't happen under the examples/definitions foler:

image

Is it because of the magic .rhai directory?

tamasfe commented 2 years ago

Definitions now always have to start with module (apart from comments/whitespace). The api/definitions are not valid definitions, they are there to be included in Rust code, then module is prepended to them depending on what the user wants to do with them.

tamasfe commented 2 years ago

Also we could silence the errors by placing a Rhai.toml in the root of the project that restricts the LSP to a specific set of rhai files:

[source]
include = [ "scripts/**/*.rhai" ]
schungx commented 2 years ago

Putting in Rhai.toml yields this:

INFO created workspace root=root:///
 INFO LSP server listening transport="stdio"
 INFO initialize: created workspace root=file:///c%3A/Git/rhai
 INFO initialized: found Rhai.toml path="c:/Git/rhai\\Rhai.toml"
 INFO initialized: found files count=39 excluded=0
ERROR initialized: failed to read file error=Access is denied. (os error 5)

There is probably an extra backslash... or should it be a forward slash instead? Windows should read forward-slash paths just fine.

tamasfe commented 2 years ago

The path should be fine like that, it's something else. The error message is not too helpful.

Maybe decreasing the log level could help a bit?

// vscode settings.json
  "rhai.executable.environment": {
    "RUST_LOG": "debug"
  },
schungx commented 2 years ago
DEBUG initialized: found file path="c:/Git/rhai/examples/definitions/.rhai"
ERROR initialized: failed to read file error=Access is denied. (os error 5)
tamasfe commented 2 years ago

I see, I did not check whether the path is a directory, whoops.

schungx commented 2 years ago

Also, I see slightly strange behavior.

Firstly, whenever the LSP is run, it successfully finds undeclared variables.

image

However, when I click open that file in the editor, all errors go away:

image

image

With no definitions, the scope variables bool_state, value etc. should all flag errors.

I'm sure it did originally, but only until the file is open; then it no longer flag those errors.

tamasfe commented 2 years ago

I'm sure it did originally, but only until the file is open; then it no longer flag those errors.

I suspect a panic, what is in the logs?

schungx commented 2 years ago

Nothing in the logs that show a panic...

schungx commented 2 years ago

Restarted VScode, and the problem seems to have gone away -- strange, as I restarted before and it persists.

Now it correctly flags the undeclared variables.

tamasfe commented 2 years ago

There is probably a race condition somewhere in the LSP, I'll look into it.

schungx commented 2 years ago

Restarted VScode, and the problem seems to have gone away -- strange, as I restarted before and it persists.

Just a hunch. The situation disappeared once I excluded the api/definitions from LSP using Rhai.toml.