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

Minor build runners update #1581

Merged
merged 2 commits into from
Nov 5, 2023
Merged

Conversation

llogick
Copy link
Contributor

@llogick llogick commented Nov 5, 2023

0.11.0

  • Fix getPath() was called on a GeneratedFile that wasn't built yet.
  • Add missing cache.hash.addBytes(builtin.zig_version_string);

0.11.0, master:

  • Resolve mod imports in build.zigs
  • Show completions for mod imports in build.zigs
As already known, anything that depends on a generated path cannot be resolved.. unless:
diff --git a/src/build_runner/0.11.0.zig b/src/build_runner/0.11.0.zig
index 27ad44c..bf35faa 100644
--- a/src/build_runner/0.11.0.zig
+++ b/src/build_runner/0.11.0.zig
@@ -313,6 +313,18 @@ fn getPkgConfigIncludes(
     } else |err| return err;
 }
 
+fn reify(step: *Build.Step) anyerror!void {
+    var progress: std.Progress = .{};
+    const main_progress_node = progress.start("", 0);
+    defer main_progress_node.end();
+
+    for (step.dependencies.items) |unknown_step| {
+        try reify(unknown_step);
+    }
+
+    try step.make(main_progress_node);
+}
+
 // TODO: Having a copy of this is not very nice
 const copied_from_zig = struct {
     /// Copied from `std.Build.LazyPath.getPath2` and massaged a bit.
@@ -320,7 +332,19 @@ const copied_from_zig = struct {
         switch (path) {
             .path => |p| return builder.pathFromRoot(p),
             .cwd_relative => |p| return pathFromCwd(builder, p),
-            .generated => |gen| return builder.pathFromRoot(gen.path orelse return null),
+            .generated => |gen| {
+                if (gen.path) |gen_path|
+                    return builder.pathFromRoot(gen_path)
+                else {
+                    for (gen.step.owner.top_level_steps.values()) |tls| {
+                        reify(&tls.step) catch return null;
+                    }
+                    if (gen.path) |gen_path|
+                        return builder.pathFromRoot(gen_path)
+                    else
+                        return null;
+                }
+            },
And in the case of `microzig`, where `microzig` is a dep of an app, but imports `chip` which is a module of `app`
diff --git a/src/DocumentStore.zig b/src/DocumentStore.zig
index 5fc212a..50b03ea 100644
--- a/src/DocumentStore.zig
+++ b/src/DocumentStore.zig
@@ -1403,14 +1403,17 @@ pub fn uriFromImportStr(self: *DocumentStore, allocator: std.mem.Allocator, hand
         }
         return null;
     } else if (!std.mem.endsWith(u8, import_str, ".zig")) {
-        if (handle.associated_build_file) |build_file_uri| blk: {
-            const build_file = self.getBuildFile(build_file_uri).?;
-            const build_config = build_file.tryLockConfig() orelse break :blk;
-            defer build_file.unlockConfig();
-
-            for (build_config.packages) |pkg| {
-                if (std.mem.eql(u8, import_str, pkg.name)) {
-                    return try URI.fromPath(allocator, pkg.path);
+        // lock?
+        for (self.handles.values()) |h| {
+            if (h.associated_build_file) |build_file_uri| blk: {
+                const build_file = self.getBuildFile(build_file_uri).?;
+                const build_config = build_file.tryLockConfig() orelse break :blk;
+                defer build_file.unlockConfig();
+
+                for (build_config.packages) |pkg| {
+                    if (std.mem.eql(u8, import_str, pkg.name)) {
+                        return try URI.fromPath(allocator, pkg.path);
+                    }
                 }
             }
         } else if (isBuildFile(handle.uri)) blk: {

0.11.0
- Fix `getPath() was called on a GeneratedFile that wasn't built yet.`
- Add missing `cache.hash.addBytes(builtin.zig_version_string);`

0.11.0, master:
- Resolve mod imports in `build.zig`s
- Show completions for mod imports in `build.zig`s
@llogick llogick force-pushed the nullptrdevs/minor_build_runners_update branch from b7b083d to ed24149 Compare November 5, 2023 02:56
@Techatrix
Copy link
Member

As already known, anything that depends on a generated path cannot be resolved.. unless:

I think it would be good idea to run generated steps in certain cases, even though I am unsure why the code you showed is calling reify on every top level step of the build file. Isn't that even worse that running the install step, since it could also include other steps like tests?

@llogick
Copy link
Contributor Author

llogick commented Nov 5, 2023

even though I am unsure why the code you showed is calling reify on every top level step of the build file. Isn't that even worse that running the install step, since it could also include other steps like tests?

Hey, that's why I run these by you :)

The diffs were mostly showcase/proof of concept. I did try making just that step but it wasn't enough so I went top level.
The second example was also a broadstroke "solution", but it can be done in a stricter way (I think).
Most microzig apps follow this pattern

const microzig = @import("microzig");
const peripherals = microzig.chip.peripherals;

This, I believe, goes through analysis.getFieldAccessType, so if we store original (app's) handleconst orig_handle = handle; at the beginning of it,
and then (a few iterations later) pass it to analyser.store.uriFromImportStr as a fallback handle or do a second call to it with orig_handle it should work.
The only "downside" I can think of is issues being filed because go to def won't work within microzig.zig at const inner = @import("chip");.

I think it would be good idea to run generated steps in certain cases

I'll look into refining it

@llogick llogick merged commit 2f9cb7c into master Nov 5, 2023
10 checks passed
@llogick llogick deleted the nullptrdevs/minor_build_runners_update branch November 5, 2023 21:04
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.

2 participants