Skip to content

Conversation

@Techatrix
Copy link
Member

ZLS had an inaccurate view of how modules work in the build system. It would previously merge every module in the entire build graph into one single module which can easily lead to incorrect results in any non trivial project that contains multiple modules. This PR will keep track of each individual module so that analysis can differentiate between them just like how the compiler does it.

Some examples of what has been improved:

  • @import("root") now actually figures out in which module a given file is in.
  • auto complete for @import will only show the modules that have been directly added to the module that the current file belongs to
  • @import("some_name") will resolve the correct module if there are multiple modules that have imported different modules with "some_name" as their name.

One issue this PR doesn't fully address is when there are multiple modules with the same root source file that have conflicting definitions. This is only achievable using multiple compilation units because Zig fortunately disallows sharing a root source file within a single compilation unit.

Related PR #2308

@Techatrix Techatrix force-pushed the techatrix/model-build-system branch 2 times, most recently from 725af1e to 31481f3 Compare November 19, 2025 19:41
@dotcarmen
Copy link
Contributor

@import("root") now actually figures out in which module a given file is in.

fyi - it seems that can't really be relied upon

https://gist.github.com/dotcarmen/7f186613f1f839427a839c33306af9dd <- the output is:

hello, world!
exe says hello

what happens when a module is imported by multiple roots? i think it's best to not resolve @import("root") to anything to avoid any footguns

@Techatrix Techatrix force-pushed the techatrix/model-build-system branch from 31481f3 to fa96d31 Compare December 5, 2025 21:33
@Techatrix
Copy link
Member Author

fyi - it seems that can't really be relied upon

https://gist.github.com/dotcarmen/7f186613f1f839427a839c33306af9dd <- the output is:

I turns out that I have misunderstood the semantic of @import("root") as I assumed it would be the root source file a given file is inside but it is root source file of the compilation unit(s) a given file is in. I have updated the PR to handle this which should fix the issue in the example you provided.

what happens when a module is imported by multiple roots?

Resolve it to all of them. ZLS has something called "either types" that can be used to represent a type that can be one of a given set of types but it's unknown which one it will be.

So const root = @import("root") would be internally represented as:

const root = switch (unknown) {
    0 => @import("main.zig"),
    1 => @import("main2.zig"),
};

If main.zig and main2.zig if both root modules that import the file we are in.

complete example: https://gist.github.com/Techatrix/80b625e5965ca6177ff4360713ffded3

@dotcarmen
Copy link
Contributor

dotcarmen commented Dec 9, 2025

i've been running this build pretty successfully, however, as of 0.16.0-dev.1484+d0ba6642b, i'm getting an error about LazyPath.getPath being called during the configure phase

this only occurred after i added b.addTranslateC step

const gop_import = try gop.value_ptr.import_table.map.getOrPut(allocator, name);
// This does not account for the possibility of collisions (i.e. modules with same root source file import different modules under the same name).
if (!gop_import.found_existing) {
gop_import.value_ptr.* = try std.fs.path.resolve(allocator, &.{ cwd, import.root_source_file.?.getPath2(import.owner, null) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the line that results in the aforementioned panic

@Techatrix Techatrix force-pushed the techatrix/model-build-system branch 2 times, most recently from a48ccd0 to d760a85 Compare December 26, 2025 00:28
@Techatrix Techatrix force-pushed the techatrix/model-build-system branch from d760a85 to e813c31 Compare January 5, 2026 08:12
@dotcarmen
Copy link
Contributor

per #2564 (comment):

I have a similar change in #2510 (c8098a4) which has not been merged yet, and has gone out of date again.

would it be helpful if i gave you merge commits @Techatrix? I'm not sure what's blocking merging of this PR, but I've been making manual merge commits against master to have the nice QoL improvements from this PR in my daily usage

if there's anything needed to help push this PR over the line, i'd be happy to help.

Prevent build files in the dependency cache from being analyzed unless a
file in the cache has been manually opened.
@Techatrix
Copy link
Member Author

Techatrix commented Jan 16, 2026

Thanks for the offer to help. I already have locally rebased this PR which should get it ready to be merged.

I wanted to wait until I could investigate some issues related to ziglang/translate-c which is likely what you mentioned here. The issues were already present before and are unrelated to this PR so they don't have to block it. They are tracked in #2572 and #2573.

The change in 4e59770 should at least make the errors about ziglang/translate-c not appear unless you manually navigate to the package in the zig cache.

@Techatrix Techatrix force-pushed the techatrix/model-build-system branch 2 times, most recently from e813c31 to 4e59770 Compare January 16, 2026 15:02
@Techatrix Techatrix merged commit 44b638f into master Jan 16, 2026
12 checks passed
@Techatrix Techatrix deleted the techatrix/model-build-system branch January 16, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants