Repository navigation
build: add -Dexample=<name> to build/run a single example - #504
Conversation
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.
📝 WalkthroughWalkthrough
ChangesExample build filtering
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 winFail fast when
-Dexampledoesn’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
|
|
||
| // 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`). |
There was a problem hiding this comment.
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.
Adds a
-Dexample=<name>option that limits theexamplesstep to one example by name and adds a run step for it: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.