zls icon indicating copy to clipboard operation
zls copied to clipboard

having multiple modules breaks `@import` resolution

Open thrombe opened this issue 7 months ago • 3 comments

Zig Version

0.16.0

ZLS Version

0.16.0

Client / Code Editor / Extensions

helix

Steps to Reproduce and Observed Behavior

  • clone https://github.com/thrombe/zls
  • git checkout import-bugs
  • cd examples/many_modules
  • open code editor (in my case, helix)
  • trigger goto definition on @cInclude("c.h") in common.zig. it works
  • open root.zig, uncomment the common.zig import
  • restart zls
  • trigger goto definition on @cInclude("c.h") in common.zig. this time, it does not resolve

more detailed instructions and issue explation after my debugging. https://github.com/thrombe/zls/blob/import-bugs/examples/many_modules/README.md

Expected Behavior

@cInclude("c.h") should resolve correctly

Attach ZLS log output

logs after uncommenting the line in root.zig and triggering goto definition on @cInclude("c.h") a few times.

zls.log
info  ( main ): Starting ZLS      0.16.0 @ '/nix/store/gyznl9c2gb73126ricbv96dsd673b5b1-zls/bin/zls'
info  ( main ): Log File:         /home/issac/.cache/zls/zls.log (info)
info  ( main ): Loaded config:    /home/issac/.config/zls.json
info  (server): Client Info:      helix (25.07.1 (a05c151b))
info  (server): added Workspace Folder: file:///home/issac/daata/repos/checkout/code_read/zls/examples/many_modules (4 files)
info  (server): Set config option 'build_on_save_args' to ["-fincremental"]
info  (server): Set config option 'builtin_path' to "/home/issac/.cache/zls/builtin.zig"
info  (server): Set config option 'zig_lib_path' to "/nix/store/19cahh4irrwrj16dl4sbzll0ygn406ap-zig-0.16.0/lib"
info  (server): Set config option 'zig_exe_path' to "/nix/store/19cahh4irrwrj16dl4sbzll0ygn406ap-zig-0.16.0/bin/zig"
info  (server): Set config option 'build_runner_path' to "/home/issac/.cache/zls/build_runner/0536d83f23cda18cf0a02d9cd4e6dce4/build_runner.zig"
info  (server): Set config option 'global_cache_path' to "/home/issac/.cache/zls"
info  (store ): Loaded build file 'file:///home/issac/daata/repos/checkout/code_read/zls/examples/many_modules/build.zig'
info  (store ): Loaded build file 'file:///home/issac/daata/repos/checkout/code_read/zls/build.zig'

After some debugging, i found that DocumentStore.getAssociatedBuildFile returns only the first root_source_file that references a file (like common.zig). here's the relavant code:

But in this setup:

  • common.zig is used by multiple modules
  • each module may have different include paths

ZLS picks one and ignores the others.

So:

  • include paths from the “wrong” module are used
  • c.h can’t be found anymore

Note that compilation works because common.zig import in root.zig is never used

thrombe avatar Apr 18 '26 17:04 thrombe

https://github.com/zigtools/zls/pull/3179

nektro avatar Apr 18 '26 17:04 nektro

In Zig 0.16.0, the @cImport builtin has been deprecated. It has been removed in nightly/master Zig and ZLS. Consider looking into the suggested upgrade path in the release notes.

techatrix avatar Apr 20 '26 11:04 techatrix

I realized yesterday, this bug also applies for @import().

minimal example code: https://github.com/thrombe/zls/tree/import-bugs-2/examples/many_modules i have updated the example code to contain both @import() and @cImport(), and both break when having multiple modules in these conditions.

even if the @cImport() fix is not planned, @import() needs to be fixed. can you please reopen the issue @Techatrix or should a new issue be created for @import()?.

updated repro instructions:

  • clone https://github.com/thrombe/zls
  • git checkout import-bugs-2
  • cd examples/many_modules
  • Open the project in an editor with ZLS
  • Go to src/common.zig
  • Try "go to definition" on c.h or on @import("cbindings")

It fails to resolve.

Now:

  • Uncomment this if condition in build.zig:
    ...
    // if (false) //
    {
    ...
  • Restart ZLS

Now:

  • both c.h and cbindings imports can be resolved
  • go-to-definition starts working

thrombe avatar Apr 20 '26 14:04 thrombe