For some reason ZLS fails to find conditional headers that Zig has no trouble with?
Zig Version
0.12.0-dev.3609+ac21ade66
Zig Language Server Version
4e01c08f558ea07462aaa7b71d2a24f86f47a855
Client / Code Editor / Extensions
Helix
Steps to Reproduce and Observed Behavior
I've ported a few C libraries' build system to Zig for my own projects, and one of them let you enable a feature by passing a build option, and this would add header files to be installed.
The library in question: https://git.sr.ht/~tusf/libdom
The option in question is whether or not to build with libhubbub, an HTML parser. If you ran zig build -Dhubbub it would compile an executable with the proper parsing code, and the zig-out/include directory would include dom/bindings/hubbub.
In my build.zig file, I have:
const dom = b.dependency("libdom", .{ .hubbub = true });
However, when I used this package in a project, ZLS seems to have trouble with headers located here. Given this example:
pub const c = @cImport({
@cInclude("dom/dom.h");
@cInclude("dom/bindings/hubbub/parser.h");
});
pub fn main() u8 {
// Just a test to make sure the above is actually evaluated by Zig.
return c.DOM_HUBBUB_DOM;
}
This compiles just fine, and the return value is just fine:
> ./zig-out/bin/test
> echo $?
3
However, ZLS refuses to completion for c, and marks the whole thing with an error: `'dom/bindings/hubbub/parser.h' file not found (cImport)
Expected Behavior
I expect to get completions for the contents of c, and for the cImport to not fail.
Relevant log output
No response
I am unable to build this project:
Fetch Packages [10/9] libhubbub... /home/techatrix/.cache/zig/p/122051ea687b4220961d4c1fa43b9b5b3c6bb73c10d8408473cc9e7acb5ede999dbf/build.zig.zon:12:21: error: hash mismatch: manifest declares 12206a4a94fccc8bb322d36fe55e38a73273c8f964318ddaf2165e7e072d79c43109 but the fetched package has 12207e7044b4f64f71ea1b0781df70dc02af0a49190d6ceaa430cc7fb2c4e5a8e78d
.hash = "12206a4a94fccc8bb322d36fe55e38a73273c8f964318ddaf2165e7e072d79c43109",
When I use the expected hash I get a new error:
install
└─ install zig-testspace
└─ zig build-exe zig-testspace Debug native
└─ zig build-lib dom Debug native
└─ WriteFile failure
error: unable to open source directory '/home/techatrix/.cache/zig/p/12207e7044b4f64f71ea1b0781df70dc02af0a49190d6ceaa430cc7fb2c4e5a8e78d/include': FileNotFound
install
└─ install zig-testspace
└─ zig build-exe zig-testspace Debug native
└─ zig build-lib dom Debug native
└─ zig build-lib wapcaplet Debug native 1 errors
error: unable to check cache: stat file '/home/techatrix/.cache/zig/p/12207e7044b4f64f71ea1b0781df70dc02af0a49190d6ceaa430cc7fb2c4e5a8e78d/src/libwapcaplet.c' failed: FileNotFound
error: the following command failed with 1 compilation errors:
/nix/store/2jazjwxdb0i7i7p878vck9z0yldayz5w-zig-0.12.0-dev.3652+a59ad719d/bin/zig build-lib -cflags -std=c99 -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_ALIGNED=__attribute__((aligned)) -DSTMTEXPR=1 -Wall -W -Wundef -Wpointer-arith -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Werror -- /home/techatrix/.cache/zig/p/12207e7044b4f64f71ea1b0781df70dc02af0a49190d6ceaa430cc7fb2c4e5a8e78d/src/libwapcaplet.c -ODebug -I /home/techatrix/.cache/zig/p/12207e7044b4f64f71ea1b0781df70dc02af0a49190d6ceaa430cc7fb2c4e5a8e78d/include -Mroot -lc --cache-dir /home/techatrix/zig-testspace/zig-cache --global-cache-dir /home/techatrix/.cache/zig --name wapcaplet -static --listen=-
Build Summary: 7/13 steps succeeded; 2 failed (disable with --summary none)
install transitive failure
└─ install zig-testspace transitive failure
└─ zig build-exe zig-testspace Debug native transitive failure
└─ zig build-lib dom Debug native transitive failure
├─ zig build-lib wapcaplet Debug native 1 errors
└─ WriteFile failure
error: the following build command failed with exit code 1:
/home/techatrix/zig-testspace/zig-cache/o/32179b0942d05292b14153ed68af30f4/build /nix/store/2jazjwxdb0i7i7p878vck9z0yldayz5w-zig-0.12.0-dev.3652+a59ad719d/bin/zig /home/techatrix/zig-testspace /home/techatrix/zig-testspace/zig-cache /home/techatrix/.cache/zig --seed 0x88f32fab -Z1e4c1c9bf5d90137
AFAICT, everything in https://source.netsurf-browser.org/libwapcaplet.git/snapshot/libwapcaplet-release/0.4.3.tar.gz in inside a libwapcaplet-release/0.4.3/ subfolder but the build.zig calls wcl.path("include") instead of wcl.path("libwapcaplet-release/0.4.3/include"). The line I am referring to is here. That file also references a api/main.zig which doesn't exist.
After trying to fix the paths, I end up here:
/home/techatrix/zig-testspace/zig-cache/o/d4460a11815e291e1d976ce3a778b089/dom/core/string.h:13:10: error: 'libwapcaplet/libwapcaplet.h' file not found
For me it's happening the same with raylib. Im using nvim-lspconfig. It's weird because in vscode works well...
Same issue here with raylib on Windows 10
E:\dev\zig\game [master ≡ +1 ~3 -0 !]> zig version
0.12.0-dev.3653+e45bdc6bd
E:\dev\zig\game [master ≡ +1 ~3 -0 !]> zls --version
0.12.0-dev.513+172c8f2
Getting 'raylib.h' file not found, while zig compiles fine.
Please open a new issue about raylib and provide all necessary information so that anyone else can try and reproduce the issue.
Same here on MacOS, but with ffmpeg, it feels more, that it is about libs that are linked with pkg-config, and are not in system libs.. ZLS: 0.13.0-dev.48+af9401b ZIG: 0.13.0-dev.351+64ef45eb0
Simpliest main file:
const std = @import("std");
const avformat = @cImport({
@cInclude("libavformat/avformat.h");
});
pub fn main() !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("FFmpeg version: {}\n", .{avformat.avformat_version()});
try bw.flush();
}
In build file i am using: exe.linkSystemLibrary("libavformat"); which links it with pkg-config. How does zls knows about pkg-config stuff and what is going on in build file?
I have a similar issue, nothing after c. is getting completions. Works fine in VSCode but not i NeoVim (nvim-lspconfig).
https://github.com/user-attachments/assets/5a9d207d-63a1-4f5a-8d7f-96c743e7ce71
I have a project that can demonstrate this https://github.com/seemsindie/zig-c-interop-example
I have a similar issue, nothing after
c.is getting completions. Works fine in VSCode but not i NeoVim (nvim-lspconfig).
If it works in VS Code but not in neovim then it is most likely an issue with your setup. I've tested your project with VS Code and Neovim (nvim-lspconfig + nvim-cmp) and it worked for me.
No response from @TUSF so I will be closing this. For anyone who is experiencing problems with cImport and found this issue, please open a new Issue and provide all necessary information so that anyone else can try and reproduce the issue.