Skip to content

having multiple modules breaks @import resolution #3183

@thrombe

Description

@thrombe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions