Rescript in neovim

Hi,

I’m trying rescript in neovim for the first time. I’ve got some problem with the error reporting.

First I didn’t have any error reporting, other than syntax errors. Then, I found that enabling these options helped:

incrementalTypechecking = { enabled = true },
codeAnalyzer = { enabled = true },

Now when a file has an error (a type error), it will get reported as expected.
So I update my code to fix the error. But now the error is still reported, and it will never go until I restart the editor.
In other words the errors are not cleared even after being fixed.

Also, it seems to not report more than one error per file. Only the first type error is reported.

I installed it via Mason (rescriptls), and below is the exact config I put in lspconfig for rescript:

        rescriptls = {
          settings = {
            rescript = {
              -- opt-in experimental type-checking
              incrementalTypechecking = { enabled = true },
              -- deeper semantic analysis
              codeAnalyzer = { enabled = true },
            },
          },
        },

I haven’t found much resource on how to set it up well. Any help would be welcome!
Thanks

Paging some Neovim users @aspeddro @shulhi @dkirchhof @DCKT @CarlOlson

1 Like

Hey,

since language server version 1.62 or 1.63 you have to track for file changes by your own.
So you can add the capabilities field to the lsp config:

capabilities = {
    workspace = {
        didChangeWatchedFiles = {
            dynamicRegistration = true,
        },
    },
}

After adding this config, the lsp will update the errors after saving files.

If you use nvim-lspconfig, I added this information to their wiki (nvim-lspconfig/doc/configs.md at master · neovim/nvim-lspconfig · GitHub) and will add the config as default after the release of rescript 12.

2 Likes

@dkirchhof thanks for your help. Unfortunately the issue is still here after I enabled dynamicRegistration. I still need to restart the LSP to see things changing.

I also noticed that it’s not working well if I don’t start rescript compiler in watch mode rescript -w. It seems that the lsp used the build directory to detect issue rather than the code itself.

the lsp is version 1.66.0…

that’s what I’ve got in my config so far:

        rescriptls = {
          cmd = {
            "node",
            "/home/xxx/.local/share/nvim/mason/packages/rescript-language-server/node_modules/@rescript/language-server/out/cli.js",
            "--stdio",
          },
          capabilities = {
            workspace = {
              didChangeWatchedFiles = {
                dynamicRegistration = true,
              },
            },
          },
          settings = {
            rescript = {
              -- codeLens = true,
              incrementalTypechecking = {
                acrossFiles = true,
                enabled = true,
              },
              -- codeAnalyzer = { enabled = true },
            },
          },
        },

Your assumption is right, the language server doesn’t inspect the code, it uses the compiler output to check for errors and warnings.
So you have to start neovim in the root directory (where the rescript.json file is located) and the compiler in watch mode (rescript watch in the newest alpha / beta versions).

Your config looks similar to mine. Don’t know, if the settings field is correct?!
Here is my config (language server 1.66.0, installed as global node module):

rescriptls = {
    cmd = { 'rescript-language-server', '--stdio' },
    init_options = {
        extensionConfiguration = {
            askToStartBuild = false,
            incrementalTypechecking = {
                enabled = true,
            },
        },
    },
    capabilities = {
        workspace = {
            didChangeWatchedFiles = {
                dynamicRegistration = true,
            },
        },
    },
}
2 Likes

@dkirchhof thanks for sharing your configuration. It’s still not working for me. So there is definitely something off. I’ll have to upgrade my neovim and lazyvim to latest, but that’s a breaking upgrade. I’ll do that later when I have enough time to migrate everything.

I’ll keep this thread up my sleeve and let you whether it worked.

I’ve just come across this issue myself after coming back to some rescript code after not touching it for a few months. Tried some of the changes above but no luck so far. Don’t suppose you ended up finding something that worked for you?

Hi @popstarfreas

No, not yet. I haven’t had to time for testing the rescript setup more in depth. That’s a pity that there is some friction when getting started with the language because I’d really like to work with it.

If ever you figure out, please let me know

I looked a bit closer into my issue. I made sure I added the capabilities to the config as per previous replies, but that by itself wasn’t working.

Checking an older version of the server, the previous file watching (1.62.0) was working fine for me, but after the move to using neovim for file watching, it seems that neovim wasn’t sending any changes to the watched files for some reason.

I looked at the LSP info and I saw a recommendation to install inotify-tools for better performance on watching folders. For some reason, installing that package and seeing the file watch backend change to inotify, it seems to have fixed the file watching and I can see the language server receiving notifications about those changes now. For which I also see the errors properly updating. No idea why the previous backend wasn’t working for me.

Perhaps this info will help you.

1 Like

@popstarfreas thanks for the heads up! I’ll definitely try that. It’s a bit surprising given other LSPs for other languages are working well!

I’m glad to hear that it works for you now. Will be a big help for other neovim users.
I have installed the inotify-tools as well. I’m sure that this was the missing puzzle piece.

1 Like