Skip to content

build: add -Dexample=<name> to build/run a single example - #504

Merged
lalinsky merged 1 commit into
mainfrom
build/single-example-filter
Jun 22, 2026
Merged

lalinsky merged 1 commit into
mainfrom
build/single-example-filter

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Adds a -Dexample=<name> option that limits the examples step to one example by name and adds a run step for it:

zig build examples -Dexample=hello-world

Without the option the step builds all examples as before. Handy for iterating on a single example (build + run in one step) instead of building the whole set.

Limits the examples step to one example by name and adds a run step for it, e.g. `zig build examples -Dexample=hello-world`. Without the option the step builds all examples as before.
@coderabbitai

coderabbitai Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

build.zig gets a -Dexample=<name> option (only_example). The examples loop skips executables whose names don't match when the option is set, and a run step dependency is wired only for the matched example. The single-threaded hello-world-single-threaded build and install step is guarded behind only_example == null.

Changes

Example build filtering

Layer / File(s) Summary
-Dexample option, loop filter, and conditional wiring
build.zig
Declares only_example build option, adds a name-match check to skip non-selected examples in the loop, wires a run step dependency when a specific example is targeted, and wraps the single-threaded hello-world-single-threaded install so it only runs when no example filter is active.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

A flag you pass, -Dexample=foo,
No need to build the other forty-two.
Single-threaded guard sits tight,
Null check keeps the install right.
⚙️ One option, less noise, more light.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a -Dexample= build option to build/run a single example.
Description check ✅ Passed The description clearly explains the new feature, provides usage examples, and contrasts with existing behavior when the option is omitted.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch build/single-example-filter

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
build.zig (1)

82-100: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Fail fast when -Dexample doesn’t match any known example.

With current filtering (Line 82), zig build examples -Dexample=<typo> can succeed without building or running anything. That’s a silent no-op and hard to debug in CI/local workflows.

Suggested fix
 const examples_step = b.step("examples", "Build examples");
 const only_example = b.option([]const u8, "example", "Build/run only this example by name");
+var matched_example = false;

 // Create example executables
 for (examples) |example| {
-    if (only_example) |name| if (!std.mem.eql(u8, name, example.name)) continue;
+    if (only_example) |name| {
+        if (!std.mem.eql(u8, name, example.name)) continue;
+        matched_example = true;
+    }
     const exe = b.addExecutable(.{
         .name = example.name,
         .root_module = b.createModule(.{
             .root_source_file = b.path(example.file),
             .target = target,
             .optimize = optimize,
         }),
     });
     exe.root_module.addImport("zio", zio);

     const install_exe = b.addInstallArtifact(exe, .{});
     examples_step.dependOn(&install_exe.step);

     if (only_example != null) {
         const run = b.addRunArtifact(exe);
         examples_step.dependOn(&run.step);
     }
 }
+
+if (only_example) |name| {
+    if (!matched_example) {
+        `@panic`(b.fmt("Unknown example '{s}'", .{name}));
+    }
+}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@build.zig` around lines 82 - 100, When the only_example filter is provided
(via -Dexample), the current code silently skips all examples if none match the
specified name, causing the build to succeed without any indication of failure.
To fix this, add a flag to track whether a matching example was found during the
loop iteration. After the loop completes, check if only_example was specified
but no example matched (flag is still false), and if so, call std.debug.print or
similar to log an error message indicating the example was not found, then
return an error or call std.process.exit(1) to fail the build.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@build.zig`:
- Line 76: The comment on line 76 contains a CLI hint that references an example
name `-Dexample=wake-latency` which does not exist in the examples table defined
on lines 56-73. Review the actual example names in the examples table and
replace `wake-latency` with a valid example name that actually exists in the
table to ensure the comment provides accurate guidance to users.

---

Outside diff comments:
In `@build.zig`:
- Around line 82-100: When the only_example filter is provided (via -Dexample),
the current code silently skips all examples if none match the specified name,
causing the build to succeed without any indication of failure. To fix this, add
a flag to track whether a matching example was found during the loop iteration.
After the loop completes, check if only_example was specified but no example
matched (flag is still false), and if so, call std.debug.print or similar to log
an error message indicating the example was not found, then return an error or
call std.process.exit(1) to fail the build.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ad8b0382-2285-4874-b689-1a5dec3b2d1a

📥 Commits

Reviewing files that changed from the base of the PR and between c1f7286 and 6dd578b.

📒 Files selected for processing (1)
  • build.zig

Comment thread build.zig

// Create examples step
// Create examples step. -Dexample=<name> limits it to a single example and
// adds a run step for it (e.g. `zig build examples -Dexample=wake-latency`).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix the CLI hint example name in the comment.

Line 76 shows -Dexample=wake-latency, but wake-latency is not in the examples table (Lines 56-73). This will mislead users.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@build.zig` at line 76, The comment on line 76 contains a CLI hint that
references an example name `-Dexample=wake-latency` which does not exist in the
examples table defined on lines 56-73. Review the actual example names in the
examples table and replace `wake-latency` with a valid example name that
actually exists in the table to ensure the comment provides accurate guidance to
users.

@lalinsky
lalinsky merged commit 3d293f0 into main Jun 22, 2026
28 checks passed
@lalinsky
lalinsky deleted the build/single-example-filter branch June 22, 2026 09:42
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.

1 participant