Zls not working with imported raylib

I cannot get auto complete to work for raylib using this method. Zls works fine for zig but it does not give me anything for the included c headers. I have tried both in nvmim and vscode, does this still work for you?

1 Like

Welcome to ziggit @Eirik!

Overall zls isn’t really an issue related to the zigraylib project (I think it just may be an issue with cImports with certain versions / configurations of zls in general), if the zls doesn’t provide completions, but the project compiles, it is most likely some issue with how zls is setup. So for more extended trouble shooting of zls, I moved this to a separate topic.


What is your zls and zig version?
My guess would be that it is the zls release, which seems to give you only partial functionality (probably because it is old / still 0.11). I think cloning zls and following the instructions in the readme to build it work best, if you are configuring it for neovim.

For vscode it should just download the recent versions and build them automatically, I don’t know whether those may be broken sometimes, if there maybe is a bug on their dev branch. I also don’t know whether there is some command to trigger a reinstall, probably there is, but you have to look that up. I rarely use (vs)code.

For neovim I am using LazyVim here is my zls config.
lua/plugins/zig.lua:

return {
  {
    "neovim/nvim-lspconfig",
    init = function()
      require("lspconfig")["zls"].setup({
        settings = {
          zls = {
            path = "/home/sze/development/workspace/zig/active/current/zig",
            Zls = {
              path = "/home/sze/development/workspace/zig/active/zls/zig-out/bin/zls",
              enableAutofix = true,
              enable_snippets = true,
              enable_ast_check_diagnostics = true,
              enable_autofix = true,
              enable_import_embedfile_argument_completions = true,
              warn_style = true,
              enable_semantic_tokens = true,
              enable_inlay_hints = true,
              inlay_hints_hide_redundant_param_names = true,
              inlay_hints_hide_redundant_param_names_last_token = true,
              operator_completions = true,
              include_at_in_builtins = true,
              max_detail_length = 1048576,
            },
          },
        },
      })
    end,
  },
}

The first path is the path to my current zig executable. (Actually current is a symlink so that I can switch zig versions by changing one symlink, I am on linux)
The second path is the path to the zls executable (which I rebuilt when I change zig versions).
I also have a zls config ~/.config/zls.json:

{
    "$schema": "https://raw.githubusercontent.com/zigtools/zls/master/schema.json",
    "zig_exe_path": "/home/sze/development/workspace/zig/active/current/zig",
    "enable_snippets": true,
    "enable_ast_check_diagnostics": true,
    "enable_autofix": true,
    "enable_import_embedfile_argument_completions": true,
    "warn_style": true,
    "enable_semantic_tokens": true,
    "enable_inlay_hints": true,
    "inlay_hints_hide_redundant_param_names": true,
    "inlay_hints_hide_redundant_param_names_last_token": true,
    "operator_completions": true,
    "include_at_in_builtins": true,
    "max_detail_length": 1048576
}

But I think that is only relevant when I use zls from some editor that doesn’t configure zls directly.

1 Like

Hello, I started learning zig last weekend and ran into this same problem.

I manage to solve the issue and it is not a ZLS fault. There are some useful information that is not present in the main thread that I had to discover myself.

First, Zig’s APIs tend to break/change due to the fast development/iteration. So, to get Raylib working you have to have a compatible Zig version that you can find on their build.zig. On today’s date the supported/tested version is zig 0.12.0-dev.3632+7fb5a0b18 and I’m running the last release zig 0.12.0-dev.3666+a2b834e8c.

To have a working ZLS you just have to clone their repo and build it using zig build -Doptimize=ReleaseSafe then you put the binary, found in zig-out/bin/zls, into your path.

Now the important part. Grab the Raylib’s commit you want to use, for example the latest one 4491ff04262d11cf8f66e1bd6145c1a8d30bb503 and use zig fetch to compose your build.zig.zon, like: zig fetch https://github.com/raysan5/raylib/archive/4491ff04262d11cf8f66e1bd6145c1a8d30bb503.tar.gz --save=raylib. This way zig will generate the correct hash for you.

Finally, for reasons unknown to me, some files don’t get update in zig’s global cache (edit: if you have already tried other commit), resulting in ZLS not providing completion. But if you just delete the folder’s content and rebuild your project, the completion will start working. On windows the global cache is in C:\Users\USERNAME\AppData\Local\zig.

1 Like

enableAutofix and enable_autofix – are these really both present?
… at least in the zls.json it’s the latter. Is the former for nvim?

My guess is that enableAutofix doesn’t exist and gets ignored in the nvim lspconfig.

Thanks for the suggestions guys, appreciate it. I tried everything but still no go, seems like zls can’t resolve the headers in raylib.zig. Might be a Windows issue? I can compile and run the project fine with zig build.

image

E:\dev\zig\game [master ≡ +1 ~2 -0 !]> zig version
0.12.0-dev.3653+e45bdc6bd
E:\dev\zig\game [master ≡ +1 ~2 -0 !]> zls --version
0.12.0-dev.513+172c8f2

Can you post your build.zig?

This seems to be a recent regression. It stopped working a few days ago for me with no changes to my project except updating zig & zls. Zig compiles the project just fine but zls cannot find the header files. Issue is being tracked here: https://github.com/zigtools/zls/issues/1851

2 Likes

Whenever ZLS stops working, I usually first check the output of the language server process to see if it has anything useful to say. In VS Code, you can do this by bringing up the Output pane and selecting “Zig Language Server” in the drop-down to its top right. Different editors likely expose some similar means of accessing the output or logs. Usually, there will be some kind of compile error in there, which might either point to your build.zig script (suggesting a problem in your code) or ZLS’s custom build runner (suggesting a mismatch between the ZLS and Zig compiler versions you’re using).

1 Like

I didn’t find anything useful in the lsp logs. I am using build.zig similar to the example here zigraylib/build.zig at main · SimonLSchlee/zigraylib · GitHub.
What I suspect is that its missing a .AddIncludePath for the raylib source. But when importing from a dependency I dont know what the path is.

I actually managed to get zls working with raylib by using a submodule instead of the package manager. Working example here: GitHub - everystone/raylib-zig-template