Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module in build.zig is not considered as a BuildConfig packages #1629

Closed
evopen opened this issue Nov 25, 2023 · 4 comments · Fixed by #1738
Closed

module in build.zig is not considered as a BuildConfig packages #1629

evopen opened this issue Nov 25, 2023 · 4 comments · Fixed by #1738
Labels
bug Something isn't working

Comments

@evopen
Copy link

evopen commented Nov 25, 2023

Zig Version

0.12.0-dev.1684+ea4a07701

Zig Language Server Version

38a1998

Steps to Reproduce

I'm using nested structure in a monorepo which contains multiple Zig packages (multiple build.zig.zon and build.zig) and there is a build.zig in the root directory.
A package A in the subdirectory contains a test target with a associated test step and a bare module without a step. Another package B reference A in its zon file and build.zig. Zig builds package B fine.

First I thought I was the build.zig discovery mechanism introduced in #688. The root build.zig does not reference package A. Package A's build.zig does but the build runner fails to find them, because

  1. The package is referenced by the addTest of package A which is named root in build runner and uriAssociatedWithBuild consider uri unmatched.
  2. A bare module added with b.addModule is not deemed as a package in build runner.

ZLS in this case choose the root build.zig as the associated build file for package A and everything falls apart.

A simple workaround in my case would be to change root build.zig to a normal zig file and run with zig run.

But the root cause is the module handling I think. Did I miss something?

Expected Behavior

ZLS finds the correct module when parsing package B.

Actual Behavior

ZLS cannot parse the @import statement resulting in an unknown type.

@evopen evopen added the bug Something isn't working label Nov 25, 2023
@llogick
Copy link
Member

llogick commented Nov 26, 2023

Hi,

Is there a repository I can checkout? I'd like to see the build.zig files and where the expected @import is.

For reference (not saying it's the same) :
here's a package that has no steps and exports a module
https://github.com/ziglibs/known-folders/blob/master/build.zig
zls is able to resolve the import

const known_folders = @import("known-folders");

@evopen
Copy link
Author

evopen commented Nov 27, 2023

Hi, I've created a simple reproduce repo. https://github.com/evopen/zls-issue-1629.
The gist is, in /myapp/src/main.zig, const mylib is unknown type.
If you mv /build.zig to /meow.zig, ZLS resolve mylib in myapp correctly.

My thought is

  1. ZLS did not recognize /mylib/build.zig as a valid build.zig to mylib module, because the module is named root in the build runner which doesn't match the name mylib.
  2. And ZLS resolve myapp's dependency mylib to use /build.zig, because build.zig higher in filesystem tree takes precedence.

In your example, known-folders/build.zig is the sole build.zig in known-folders' filesystem hierarchy. ZLS can only associate this build.zig to the module.

@llogick
Copy link
Member

llogick commented Nov 27, 2023

build.zig discovery logic aside, this seems to be a timing issue - try opening the myapp/build.zig first then (maybe wait a few seconds) the myapp/src/main.zig.

fn createDocument(self: *DocumentStore, uri: Uri, text: [:0]const u8, open: bool) error{OutOfMemory}!Handle {

calls
const build_file = self.getOrLoadBuildFile(build_file_uri) orelse continue;

which calls
fn createBuildFile(self: *DocumentStore, uri: Uri) error{OutOfMemory}!BuildFile {

which queues up a build runner run

zls/src/DocumentStore.zig

Lines 1008 to 1010 in 35351a6

server.job_queue.writeItemAssumeCapacity(.{
.load_build_configuration = try server.allocator.dupe(u8, build_file.uri),
});

which happens after
_ = try build_file.collectBuildConfigPackageUris(self.allocator, &package_uris);

=> package_uris.items.len == 0 in most cases.

@evopen
Copy link
Author

evopen commented Nov 27, 2023

Ok I think you are right. Build Config is read before write. And build file walker walks from root towards the file of interest, I got it wrong. Sorry for drawing the wrong conclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants